Skip to content

Commit 5bff00d

Browse files
authored
Merge pull request #31 from MetabobProject/per-per-state-ui-changes
Fix: Per Project State and UI changes
2 parents 798c3bd + 4499c88 commit 5bff00d

File tree

14 files changed

+289
-82
lines changed

14 files changed

+289
-82
lines changed

ext-src/commands/DetailDocument.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export function activateDetailSuggestionCommand(context: vscode.ExtensionContext
1616
vuln: Problem;
1717
jobId: string;
1818
}) => {
19+
const currentWorkSpaceFolder = Utils.getRootFolderName();
1920
const key = `${args.path}@@${args.id}`;
2021
const setAnalyzeState = new Analyze(context);
2122
const analyzeStateValue = new Analyze(context).get()?.value;
@@ -78,6 +79,13 @@ export function activateDetailSuggestionCommand(context: vscode.ExtensionContext
7879
type: 'CURRENT_FILE',
7980
data: { ...documentMetaData.editor.document },
8081
});
82+
83+
getExtensionEventEmitter().fire({
84+
type: 'CURRENT_PROJECT',
85+
data: {
86+
name: currentWorkSpaceFolder
87+
},
88+
});
8189
}, 500);
8290

8391
await setAnalyzeState.set(copiedAnalyzeValue);

ext-src/commands/DiscardSuggestion.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export function activateDiscardCommand(context: vscode.ExtensionContext): void {
1313
const command = CONSTANTS.discardSuggestionCommand;
1414

1515
const commandHandler = async (args: DiscardCommandHandler) => {
16-
let isDecorationsApplied = false;
16+
const currentWorkSpaceFolder = Utils.getRootFolderName();
1717
const analyzeState = new Analyze(context);
1818
const problems = analyzeState.get()?.value;
1919

@@ -66,7 +66,7 @@ export function activateDiscardCommand(context: vscode.ExtensionContext): void {
6666
}
6767

6868
if (isUserOnProblemFile) {
69-
isDecorationsApplied = Utils.decorateCurrentEditorWithHighlights(
69+
Utils.decorateCurrentEditorWithHighlights(
7070
results,
7171
documentMetaData.editor,
7272
);
@@ -87,6 +87,13 @@ export function activateDiscardCommand(context: vscode.ExtensionContext): void {
8787
data: { ...documentMetaData.editor.document },
8888
});
8989

90+
getExtensionEventEmitter().fire({
91+
type: 'CURRENT_PROJECT',
92+
data: {
93+
name: currentWorkSpaceFolder
94+
},
95+
});
96+
9097
await Promise.allSettled([
9198
analyzeState.set(copyProblems),
9299
feedbackService.discardSuggestion(payload, session),

ext-src/events.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export interface AnalysisEvents {
99
| 'No_Editor_Detected'
1010
| 'FIX_SUGGESTION'
1111
| 'CURRENT_FILE'
12+
| 'CURRENT_PROJECT'
1213
| 'INIT_DATA_UPON_NEW_FILE_OPEN';
1314
data: any;
1415
}

ext-src/extension.ts

Lines changed: 93 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,8 @@ import {
2525
import { Analyze } from './state';
2626
import { Problem } from './types';
2727

28-
let previousEditor: vscode.TextEditor | undefined = undefined;
29-
3028
export function activate(context: vscode.ExtensionContext): void {
29+
let previousEditor: vscode.TextEditor | undefined = undefined;
3130
bootstrapExtensionEventEmitter();
3231
debugChannel.show(true);
3332
debugChannel.appendLine('Activating Metabob Extension...');
@@ -129,6 +128,7 @@ export function activate(context: vscode.ExtensionContext): void {
129128
context.subscriptions.push(
130129
vscode.workspace.onDidSaveTextDocument(document => {
131130
let savedDocumentMetaData = Util.extractMetaDataFromDocument(document);
131+
const currentWorkSpaceFolder = Util.getRootFolderName();
132132
if (!savedDocumentMetaData.fileName) return;
133133

134134
let fileName: string = savedDocumentMetaData.fileName;
@@ -166,11 +166,19 @@ export function activate(context: vscode.ExtensionContext): void {
166166
type: 'CURRENT_FILE',
167167
data: { ...document },
168168
});
169+
170+
extensionEventEmitter.fire({
171+
type: 'CURRENT_PROJECT',
172+
data: {
173+
name: currentWorkSpaceFolder,
174+
},
175+
});
169176
}),
170177
);
171178

172179
context.subscriptions.push(
173180
vscode.workspace.onDidCloseTextDocument(() => {
181+
const currentWorkSpaceFolder = Util.getRootFolderName();
174182
const editor = vscode.window.activeTextEditor;
175183
if (!editor || !editor.document) {
176184
extensionEventEmitter.fire({
@@ -196,24 +204,48 @@ export function activate(context: vscode.ExtensionContext): void {
196204
type: 'CURRENT_FILE',
197205
data: { ...editor.document },
198206
});
207+
extensionEventEmitter.fire({
208+
type: 'CURRENT_PROJECT',
209+
data: {
210+
name: currentWorkSpaceFolder,
211+
},
212+
});
199213
}
200214
}),
201215
);
202216

203217
context.subscriptions.push(
204218
vscode.workspace.onDidOpenTextDocument((e: vscode.TextDocument) => {
219+
const currentWorkSpaceFolder = Util.getRootFolderName();
205220
const documentMetaData = Util.extractMetaDataFromDocument(e);
206-
if (!documentMetaData.fileName) return
221+
if (!documentMetaData.fileName) {
222+
debugChannel.appendLine(
223+
'onDidOpenTextDocument: fileName is undefined. ' +
224+
'\n' +
225+
documentMetaData.relativePath +
226+
'\n' +
227+
documentMetaData.filePath,
228+
);
229+
return;
230+
}
207231

208232
const analyzeState = new Analyze(context);
209233
const analyzeValue = analyzeState.get()?.value;
210-
if (!analyzeValue) return;
234+
if (!analyzeValue) {
235+
debugChannel.appendLine('onDidOpenTextDocument: analyzeValue is undefined');
236+
237+
return;
238+
}
211239

212240
const results: Problem[] | undefined = Util.getCurrentEditorProblems(
213241
analyzeValue,
214242
documentMetaData.fileName,
215243
);
216-
if (!results) return;
244+
if (!results) {
245+
debugChannel.appendLine('onDidOpenTextDocument: results is undefined');
246+
247+
return;
248+
}
217249

218250
if (results.length === 0) {
219251
extensionEventEmitter.fire({
@@ -233,6 +265,17 @@ export function activate(context: vscode.ExtensionContext): void {
233265
type: 'CURRENT_FILE',
234266
data: { ...e },
235267
});
268+
269+
extensionEventEmitter.fire({
270+
type: 'CURRENT_PROJECT',
271+
data: {
272+
name: currentWorkSpaceFolder,
273+
},
274+
});
275+
debugChannel.appendLine(
276+
'onDidOpenTextDocument: results have zero length. ' + documentMetaData.fileName,
277+
);
278+
236279
return;
237280
}
238281

@@ -259,25 +302,46 @@ export function activate(context: vscode.ExtensionContext): void {
259302
type: 'Analysis_Completed',
260303
data: { shouldResetRecomendation: true, shouldMoveToAnalyzePage: true, ...analyzeValue },
261304
});
305+
306+
extensionEventEmitter.fire({
307+
type: 'CURRENT_PROJECT',
308+
data: {
309+
name: currentWorkSpaceFolder,
310+
},
311+
});
262312
}),
263313
);
264314

265315
context.subscriptions.push(
266316
vscode.window.onDidChangeActiveTextEditor((e: vscode.TextEditor | undefined) => {
267-
if (!e) return;
317+
if (!e) {
318+
debugChannel.appendLine('onDidChangeActiveTextEditor: e is undefined');
319+
return;
320+
}
268321
const { fileName } = Util.extractMetaDataFromDocument(e.document);
269-
if (!fileName) return;
322+
if (!fileName) {
323+
debugChannel.appendLine('onDidChangeActiveTextEditor: fileName is undefined');
324+
return;
325+
}
326+
327+
const currentWorkSpaceFolder = Util.getRootFolderName();
270328

271329
if (previousEditor) {
272330
previousEditor.setDecorations(decorationType, []);
273331
}
274332

275333
const analyzeState = new Analyze(context);
276334
const analyzeValue = analyzeState.get()?.value;
277-
if (!analyzeValue) return;
335+
if (!analyzeValue) {
336+
debugChannel.appendLine('onDidChangeActiveTextEditor: analyzeValue is undefined');
337+
return;
338+
}
278339

279340
const results: Problem[] | undefined = Util.getCurrentEditorProblems(analyzeValue, fileName);
280-
if (!results) return;
341+
if (!results) {
342+
debugChannel.appendLine('onDidChangeActiveTextEditor: results is undefined');
343+
return;
344+
}
281345

282346
if (results.length === 0) {
283347
extensionEventEmitter.fire({
@@ -296,6 +360,19 @@ export function activate(context: vscode.ExtensionContext): void {
296360
type: 'CURRENT_FILE',
297361
data: { ...e.document },
298362
});
363+
364+
extensionEventEmitter.fire({
365+
type: 'CURRENT_PROJECT',
366+
data: {
367+
name: currentWorkSpaceFolder,
368+
},
369+
});
370+
371+
debugChannel.appendLine(
372+
'onDidChangeActiveTextEditor: results array has zero length. ' + fileName,
373+
);
374+
375+
previousEditor = e;
299376
return;
300377
}
301378

@@ -318,6 +395,13 @@ export function activate(context: vscode.ExtensionContext): void {
318395
data: { ...e.document },
319396
});
320397

398+
extensionEventEmitter.fire({
399+
type: 'CURRENT_PROJECT',
400+
data: {
401+
name: currentWorkSpaceFolder,
402+
},
403+
});
404+
321405
previousEditor = e;
322406
}),
323407
);
@@ -340,5 +424,4 @@ export function deactivate(): void {
340424
debugChannel.dispose();
341425
decorationType.dispose();
342426
disposeExtensionEventEmitter();
343-
previousEditor = undefined;
344427
}

ext-src/helpers/HandleDocumentAnalyze.ts

Lines changed: 52 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,20 @@ export const handleDocumentAnalyze = async (
3131
jobId?: string,
3232
suppressRateLimitErrors = false,
3333
) => {
34+
const currentWorkSpaceFolder = Util.getRootFolderName();
3435
const editor = vscode.window.activeTextEditor;
3536
if (!editor || editor.document.fileName !== metaDataDocument.filePath) {
3637
getExtensionEventEmitter().fire({
3738
type: 'Analysis_Error',
3839
data: '',
3940
});
4041

42+
getExtensionEventEmitter().fire({
43+
type: 'CURRENT_PROJECT',
44+
data: {
45+
name: currentWorkSpaceFolder
46+
},
47+
});
4148
return failedResponseReturn;
4249
}
4350

@@ -58,6 +65,12 @@ export const handleDocumentAnalyze = async (
5865
type: 'Analysis_Error',
5966
data: '',
6067
});
68+
getExtensionEventEmitter().fire({
69+
type: 'CURRENT_PROJECT',
70+
data: {
71+
name: currentWorkSpaceFolder
72+
},
73+
});
6174
vscode.window.showErrorMessage(CONSTANTS.analyzeCommandTimeoutMessage);
6275
}
6376

@@ -67,6 +80,12 @@ export const handleDocumentAnalyze = async (
6780
type: 'Analysis_Error',
6881
data: '',
6982
});
83+
getExtensionEventEmitter().fire({
84+
type: 'CURRENT_PROJECT',
85+
data: {
86+
name: currentWorkSpaceFolder
87+
},
88+
});
7089
vscode.window.showErrorMessage(CONSTANTS.analyzeCommandErrorMessage);
7190

7291
return failedResponseReturn;
@@ -83,6 +102,12 @@ export const handleDocumentAnalyze = async (
83102
type: 'Analysis_Error',
84103
data: '',
85104
});
105+
getExtensionEventEmitter().fire({
106+
type: 'CURRENT_PROJECT',
107+
data: {
108+
name: currentWorkSpaceFolder
109+
},
110+
});
86111
vscode.window.showErrorMessage(CONSTANTS.analyzeCommandErrorMessage);
87112

88113
return failedResponseReturn;
@@ -115,7 +140,7 @@ export const handleDocumentAnalyze = async (
115140
}
116141
return true;
117142
})
118-
.filter((vulnerability) => {
143+
.filter(vulnerability => {
119144
const { endLine, startLine } = vulnerability;
120145
const range = new vscode.Range(
121146
startLine - 1,
@@ -124,11 +149,14 @@ export const handleDocumentAnalyze = async (
124149
documentMetaData.editor.document.lineAt(endLine - 1).text.length,
125150
);
126151

127-
const text = documentMetaData.editor.document.getText(range).replace("\n", "").replace("\t", "")
152+
const text = documentMetaData.editor.document
153+
.getText(range)
154+
.replace('\n', '')
155+
.replace('\t', '');
128156
if (text.length === 0 || text === '' || text === ' ') {
129-
return false
157+
return false;
130158
}
131-
return true
159+
return true;
132160
})
133161
.forEach(problem => {
134162
const key = `${problem.path}@@${problem.id}`;
@@ -139,6 +167,7 @@ export const handleDocumentAnalyze = async (
139167
isDiscarded: problem.discarded,
140168
isEndorsed: problem.endorsed,
141169
isViewed: false,
170+
fullFilePath: currentWorkSpaceFolder,
142171
};
143172
results[key] = { ...analyzeMetaData };
144173
});
@@ -149,21 +178,36 @@ export const handleDocumentAnalyze = async (
149178
type: 'Analysis_Error',
150179
data: '',
151180
});
181+
getExtensionEventEmitter().fire({
182+
type: 'CURRENT_PROJECT',
183+
data: {
184+
name: currentWorkSpaceFolder
185+
},
186+
});
152187
vscode.window.showErrorMessage(CONSTANTS.analyzeCommandErrorMessage);
153188

154189
return failedResponseReturn;
155190
}
156191

157-
Util.decorateCurrentEditorWithHighlights(
158-
problems,
159-
documentMetaData.editor,
160-
);
192+
const path = problems.map(item => item.path);
193+
194+
const isUserOnValidEditor = path.includes(documentMetaData.fileName);
195+
if (isUserOnValidEditor) {
196+
Util.decorateCurrentEditorWithHighlights(problems, documentMetaData.editor);
197+
}
161198

162199
await analyzeState.set(results);
163200
getExtensionEventEmitter().fire({
164201
type: 'Analysis_Completed',
165202
data: { shouldResetRecomendation: true, shouldMoveToAnalyzePage: true, ...results },
166203
});
167204

205+
getExtensionEventEmitter().fire({
206+
type: 'CURRENT_PROJECT',
207+
data: {
208+
name: currentWorkSpaceFolder
209+
},
210+
});
211+
168212
return verifiedResponse;
169213
};

0 commit comments

Comments
 (0)