Skip to content

Commit 798c3bd

Browse files
authored
Merge pull request #30 from MetabobProject/sprint-3-improvements
Sprint 3 improvements
2 parents 056c300 + e22f2a8 commit 798c3bd

36 files changed

+1082
-746
lines changed

.vscode/launch.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
],
1414
"sourceMaps": true,
1515
"outFiles": [
16-
"${workspaceFolder}/build/**/*.js",
16+
"${workspaceRoot}/ext-src/**/*.ts",
1717
"!**/node_modules/**"
1818
]
1919
}

ext-src/commands/AnalyzeDocument.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import CONSTANTS from '../constants';
77
import Util from '../utils';
88
import { getExtensionEventEmitter } from '../events';
99

10+
1011
export function activateAnalyzeCommand(context: vscode.ExtensionContext): void {
1112
const command = CONSTANTS.analyzeDocumentCommand;
1213

@@ -54,8 +55,9 @@ export function activateAnalyzeCommand(context: vscode.ExtensionContext): void {
5455

5556
debugChannel.appendLine(`Metabob: Starting Analysis for ${documentMetaData.filePath}`);
5657

58+
5759
Util.withProgress<SubmitRepresentationResponse>(
58-
handleDocumentAnalyze(documentMetaData, sessionToken, analyzeState),
60+
handleDocumentAnalyze(documentMetaData, sessionToken, analyzeState, context),
5961
CONSTANTS.analyzeCommandProgressMessage,
6062
).then(response => {
6163
if (response.status === 'pending' || response.status === 'running') {
@@ -69,11 +71,9 @@ export function activateAnalyzeCommand(context: vscode.ExtensionContext): void {
6971
}
7072

7173
Util.withProgress<SubmitRepresentationResponse>(
72-
handleDocumentAnalyze(documentMetaData, sessionToken, analyzeState, inflightJobId),
74+
handleDocumentAnalyze(documentMetaData, sessionToken, analyzeState, context, inflightJobId,),
7375
CONSTANTS.analyzeCommandQueueMessage,
7476
).then(response => {
75-
debugChannel.appendLine(response.status);
76-
7777
switch (response.status) {
7878
case 'failed':
7979
case 'complete': {

ext-src/commands/DetailDocument.ts

Lines changed: 57 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import * as vscode from 'vscode';
22
import CONSTANTS from '../constants';
33
import _debug from '../debug';
4-
import { CurrentQuestion, CurrentQuestionState } from '../state';
4+
import { Analyze, CurrentQuestion, CurrentQuestionState, Session } from '../state';
55
import { Problem } from '../types';
66
import { getExtensionEventEmitter } from '../events';
7+
import { FeedbackSuggestionPayload, feedbackService } from '../services';
8+
import Utils from '../utils';
79

810
export function activateDetailSuggestionCommand(context: vscode.ExtensionContext): void {
911
const command = CONSTANTS.showDetailSuggestionCommand;
@@ -14,14 +16,41 @@ export function activateDetailSuggestionCommand(context: vscode.ExtensionContext
1416
vuln: Problem;
1517
jobId: string;
1618
}) => {
19+
const key = `${args.path}@@${args.id}`;
20+
const setAnalyzeState = new Analyze(context);
21+
const analyzeStateValue = new Analyze(context).get()?.value;
22+
const sessionToken = new Session(context).get()?.value;
23+
const extensionEventEmitter = getExtensionEventEmitter();
24+
1725
_debug.appendLine(`Detail initiated for ${args.path} with Problem ${args.id} `);
18-
const currentQuestionState = new CurrentQuestion(context);
19-
const payload: CurrentQuestionState = {
20-
path: args.path,
21-
id: args.id,
22-
vuln: args.vuln,
23-
isFix: false,
24-
isReset: false,
26+
27+
const documentMetaData = Utils.getFileNameFromCurrentEditor();
28+
29+
if (!documentMetaData) {
30+
vscode.window.showErrorMessage(CONSTANTS.editorNotSelectorError);
31+
return
32+
}
33+
34+
if (!sessionToken || !analyzeStateValue) {
35+
extensionEventEmitter.fire({
36+
type: 'CURRENT_FILE',
37+
data: { ...documentMetaData.editor.document },
38+
});
39+
vscode.window.showErrorMessage(CONSTANTS.editorNotSelectorError);
40+
return;
41+
}
42+
43+
vscode.commands.executeCommand('recommendation-panel-webview.focus');
44+
45+
const copiedAnalyzeValue = { ...analyzeStateValue };
46+
copiedAnalyzeValue[key].isDiscarded = copiedAnalyzeValue[key].isDiscarded || false;
47+
copiedAnalyzeValue[key].isEndorsed = copiedAnalyzeValue[key].isEndorsed || false;
48+
copiedAnalyzeValue[key].isViewed = true;
49+
50+
const readSuggestionPayload: FeedbackSuggestionPayload = {
51+
problemId: args.id,
52+
discarded: copiedAnalyzeValue[key].isDiscarded || false,
53+
endorsed: copiedAnalyzeValue[key].isEndorsed || false,
2554
};
2655

2756
setTimeout(() => {
@@ -35,10 +64,27 @@ export function activateDetailSuggestionCommand(context: vscode.ExtensionContext
3564
isReset: false,
3665
},
3766
});
38-
}, 2000);
3967

40-
await currentQuestionState.set(payload);
41-
vscode.commands.executeCommand('recommendation-panel-webview.focus');
68+
getExtensionEventEmitter().fire({
69+
type: 'Analysis_Completed',
70+
data: {
71+
shouldResetRecomendation: true,
72+
shouldMoveToAnalyzePage: false,
73+
...copiedAnalyzeValue,
74+
},
75+
});
76+
77+
extensionEventEmitter.fire({
78+
type: 'CURRENT_FILE',
79+
data: { ...documentMetaData.editor.document },
80+
});
81+
}, 500);
82+
83+
await setAnalyzeState.set(copiedAnalyzeValue);
84+
85+
await Promise.allSettled([
86+
feedbackService.readSuggestion(readSuggestionPayload, sessionToken),
87+
]);
4288
};
4389

4490
context.subscriptions.push(vscode.commands.registerCommand(command, commandHandler));

ext-src/commands/DiscardSuggestion.ts

Lines changed: 55 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import * as vscode from 'vscode';
2-
import { CurrentQuestion, Analyze, Session, AnalyseMetaData } from '../state';
3-
import { GenerateDecorations, decorationType } from '../helpers/GenerateDecorations';
2+
import { CurrentQuestion, Analyze, Session } from '../state';
43
import { FeedbackSuggestionPayload, feedbackService } from '../services';
4+
import { getExtensionEventEmitter } from '../events';
55
import CONSTANTS from '../constants';
66
import Utils from '../utils';
7+
import { Problem } from '../types';
78
import _debug from '../debug';
89

910
export type DiscardCommandHandler = { id: string; path: string };
@@ -12,39 +13,38 @@ export function activateDiscardCommand(context: vscode.ExtensionContext): void {
1213
const command = CONSTANTS.discardSuggestionCommand;
1314

1415
const commandHandler = async (args: DiscardCommandHandler) => {
15-
const { id: problemId, path } = args;
16-
const key = `${path}@@${problemId}`;
17-
18-
const editor = vscode.window.activeTextEditor;
19-
if (!editor) {
20-
vscode.window.showErrorMessage(CONSTANTS.editorNotSelectorError);
16+
let isDecorationsApplied = false;
17+
const analyzeState = new Analyze(context);
18+
const problems = analyzeState.get()?.value;
2119

20+
if (!problems) {
21+
_debug?.appendLine('Metabob: Problems is undefined in Discard Suggestion');
22+
vscode.window.showErrorMessage(CONSTANTS.discardCommandErrorMessage);
2223
return;
2324
}
2425

25-
if (!Utils.isValidDocument(editor.document)) {
26-
_debug?.appendLine(CONSTANTS.editorSelectedIsInvalid);
27-
28-
return;
29-
}
26+
const currentQuestion = new CurrentQuestion(context);
27+
const extensionEventEmitter = getExtensionEventEmitter();
28+
const { id: problemId, path } = args;
29+
const key = `${path}@@${problemId}`;
3030

3131
const session = new Session(context).get()?.value;
3232
if (!session) {
3333
_debug?.appendLine('Metabob: Session is undefined in Discard Suggestion');
34-
34+
vscode.window.showErrorMessage(CONSTANTS.discardCommandErrorMessage);
3535
return;
3636
}
3737

38-
const analyzeState = new Analyze(context);
39-
const currentQuestion = new CurrentQuestion(context);
40-
41-
const problems = analyzeState.get()?.value;
42-
if (!problems) {
43-
_debug?.appendLine('Metabob: Problems is undefined in Discard Suggestion');
44-
38+
// verifying that in-fact user viewing problem.path file.
39+
const documentMetaData = Utils.getFileNameFromCurrentEditor();
40+
if (!documentMetaData) {
41+
vscode.window.showErrorMessage(CONSTANTS.editorNotSelectorError);
4542
return;
4643
}
4744

45+
const filename: string | undefined = documentMetaData.fileName;
46+
const isUserOnProblemFile: boolean = filename === path;
47+
4848
const copyProblems = { ...problems };
4949

5050
const payload: FeedbackSuggestionPayload = {
@@ -53,36 +53,47 @@ export function activateDiscardCommand(context: vscode.ExtensionContext): void {
5353
endorsed: false,
5454
};
5555

56-
try {
57-
await feedbackService.discardSuggestion(payload, session);
58-
} catch (err: any) {
59-
_debug?.appendLine(err.message);
56+
// Updating the state with discarded problem values.
57+
copyProblems[key].isDiscarded = true;
58+
copyProblems[key].isEndorsed = false;
59+
copyProblems[key].isViewed = true;
60+
61+
// Filtering the problem that are not not supported by vscode. Line range should be in range [0, lineNumber].
62+
const results: Problem[] | undefined = Utils.getCurrentEditorProblems(copyProblems, path);
63+
if (!results) {
64+
vscode.window.showErrorMessage(CONSTANTS.discardCommandErrorMessage);
65+
return;
6066
}
6167

62-
copyProblems[key].isDiscarded = true;
68+
if (isUserOnProblemFile) {
69+
isDecorationsApplied = Utils.decorateCurrentEditorWithHighlights(
70+
results,
71+
documentMetaData.editor,
72+
);
73+
}
6374

64-
try {
65-
analyzeState.set(copyProblems).then(() => {
66-
const results: AnalyseMetaData[] = [];
75+
extensionEventEmitter.fire({
76+
type: 'onDiscardSuggestionClicked:Success',
77+
data: {},
78+
});
6779

68-
for (const [, value] of Object.entries(copyProblems)) {
69-
if (!value.isDiscarded) {
70-
results.push(value);
71-
}
72-
}
80+
getExtensionEventEmitter().fire({
81+
type: 'Analysis_Completed',
82+
data: { shouldResetRecomendation: true, shouldMoveToAnalyzePage: true, ...copyProblems },
83+
});
7384

74-
const { decorations } = GenerateDecorations(results, editor);
75-
editor.setDecorations(decorationType, []);
76-
editor.setDecorations(decorationType, decorations);
77-
currentQuestion.clear();
85+
extensionEventEmitter.fire({
86+
type: 'CURRENT_FILE',
87+
data: { ...documentMetaData.editor.document },
88+
});
7889

79-
return;
80-
});
81-
} catch (error: any) {
82-
_debug.appendLine(error);
90+
await Promise.allSettled([
91+
analyzeState.set(copyProblems),
92+
feedbackService.discardSuggestion(payload, session),
93+
feedbackService.readSuggestion(payload, session),
94+
]);
8395

84-
return;
85-
}
96+
currentQuestion.clear();
8697
};
8798

8899
context.subscriptions.push(vscode.commands.registerCommand(command, commandHandler));

ext-src/commands/EndorseSuggestion.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import * as vscode from 'vscode';
22
import CONSTANTS from '../constants';
33
import { feedbackService, FeedbackSuggestionPayload } from '../services';
4-
import { Session } from '../state';
4+
import { Analyze, Session } from '../state';
5+
import { getExtensionEventEmitter } from '../events';
56

67
export type EndorseCommandHandler = { id: string; path: string };
78

@@ -12,17 +13,37 @@ export function activateEndorseCommand(
1213
const command = CONSTANTS.endorseSuggestionCommand;
1314

1415
const commandHandler = async (args: EndorseCommandHandler) => {
15-
const { id: problemId } = args;
16+
const { id: problemId, path } = args;
17+
const key = `${path}@@${problemId}`;
18+
19+
const extensionEventEmitter = getExtensionEventEmitter();
20+
21+
const analyzeState = new Analyze(context);
22+
const analyzeStateValue = analyzeState.get()?.value;
23+
if (!analyzeStateValue) return;
24+
1625
const sessionToken = new Session(context).get()?.value;
1726
if (!sessionToken) return;
1827

28+
const copyProblems = { ...analyzeStateValue };
29+
30+
copyProblems[key].isDiscarded = false;
31+
copyProblems[key].isEndorsed = true;
32+
copyProblems[key].isViewed = true;
33+
1934
const payload: FeedbackSuggestionPayload = {
2035
problemId,
2136
discarded: false,
2237
endorsed: true,
2338
};
2439
try {
2540
await feedbackService.endorseSuggestion(payload, sessionToken);
41+
await feedbackService.readSuggestion(payload, sessionToken);
42+
await analyzeState.set(copyProblems);
43+
extensionEventEmitter.fire({
44+
type: 'Analysis_Completed',
45+
data: { shouldResetRecomendation: false, shouldMoveToAnalyzePage: false, ...copyProblems },
46+
});
2647
} catch {
2748
_debug?.appendLine(`Metabob: Error Endorsing Problem With ${args.id}`);
2849
vscode.window.showErrorMessage(CONSTANTS.endorseCommandErrorMessage);

ext-src/commands/FixSuggestion.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export function activateFixSuggestionCommand(context: vscode.ExtensionContext):
3737
},
3838
});
3939
} catch (error) {
40-
debugChannel.appendLine(`Metabob: Error while `);
40+
debugChannel.appendLine(`Metabob: Error while fixing suggestion ${JSON.stringify(error)}`);
4141
}
4242

4343
vscode.commands.executeCommand('recommendation-panel-webview.focus');

ext-src/commands/FocusRecommendation.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
11
import * as vscode from 'vscode';
2-
import { CurrentQuestion } from '../state/CurrentQuestion';
32
import { Problem } from '../types';
43
import _debug from '../debug';
54
import { getExtensionEventEmitter } from '../events';
5+
import { Analyze } from '../state';
66

77
type FocusCommandHandler = { path: string; id: string; vuln: Problem; jobId: string };
88
export function activateFocusRecommendCommand(context: vscode.ExtensionContext): void {
99
const command = 'metabob.focusRecommend';
1010

1111
const commandHandler = async (args: FocusCommandHandler) => {
1212
const { id, vuln, path } = args;
13+
const key = `${path}@@${id}`;
1314
_debug?.appendLine(`Current Detection Set for ${path} with Problem ${id} `);
1415

15-
const currentQuestionState = new CurrentQuestion(context);
16-
const currentQuestion = currentQuestionState.get()?.value;
17-
if (!currentQuestion) return;
16+
const analyzeState = new Analyze(context);
17+
const analyzeStateValue = analyzeState.get()?.value;
18+
if (!analyzeStateValue) return;
1819

19-
currentQuestionState.set({
20-
path,
21-
id,
22-
vuln,
23-
isFix: false,
24-
isReset: true,
25-
});
20+
const copyProblems = { ...analyzeStateValue };
21+
22+
copyProblems[key].isDiscarded = false;
23+
copyProblems[key].isEndorsed = false;
24+
copyProblems[key].isViewed = true;
2625

26+
await analyzeState.set({ ...copyProblems })
2727
getExtensionEventEmitter().fire({
2828
type: 'FIX_SUGGESTION',
2929
data: {

ext-src/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ const CONSTANTS = {
2222
analyzeCommandQueueMessage: 'Metabob: Document is Queued',
2323
analyzeCommandErrorMessage: 'Metabob: Error Analyzing the Document',
2424
analyzeCommandTimeoutMessage: 'Metabob: Looks like the server is overloaded, please try again later',
25+
applyRecommendationEror: 'Metabob: Could not apply recommendation to file',
2526

2627
// Error Constants
2728
editorNotSelectorError: 'Metabob: kindly select an editor to perform this command',

0 commit comments

Comments
 (0)