Skip to content

Commit

Permalink
Merge pull request #31 from MetabobProject/per-per-state-ui-changes
Browse files Browse the repository at this point in the history
Fix: Per Project State and UI changes
  • Loading branch information
AviGopal authored Feb 22, 2024
2 parents 798c3bd + 4499c88 commit 5bff00d
Show file tree
Hide file tree
Showing 14 changed files with 289 additions and 82 deletions.
8 changes: 8 additions & 0 deletions ext-src/commands/DetailDocument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export function activateDetailSuggestionCommand(context: vscode.ExtensionContext
vuln: Problem;
jobId: string;
}) => {
const currentWorkSpaceFolder = Utils.getRootFolderName();
const key = `${args.path}@@${args.id}`;
const setAnalyzeState = new Analyze(context);
const analyzeStateValue = new Analyze(context).get()?.value;
Expand Down Expand Up @@ -78,6 +79,13 @@ export function activateDetailSuggestionCommand(context: vscode.ExtensionContext
type: 'CURRENT_FILE',
data: { ...documentMetaData.editor.document },
});

getExtensionEventEmitter().fire({
type: 'CURRENT_PROJECT',
data: {
name: currentWorkSpaceFolder
},
});
}, 500);

await setAnalyzeState.set(copiedAnalyzeValue);
Expand Down
11 changes: 9 additions & 2 deletions ext-src/commands/DiscardSuggestion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export function activateDiscardCommand(context: vscode.ExtensionContext): void {
const command = CONSTANTS.discardSuggestionCommand;

const commandHandler = async (args: DiscardCommandHandler) => {
let isDecorationsApplied = false;
const currentWorkSpaceFolder = Utils.getRootFolderName();
const analyzeState = new Analyze(context);
const problems = analyzeState.get()?.value;

Expand Down Expand Up @@ -66,7 +66,7 @@ export function activateDiscardCommand(context: vscode.ExtensionContext): void {
}

if (isUserOnProblemFile) {
isDecorationsApplied = Utils.decorateCurrentEditorWithHighlights(
Utils.decorateCurrentEditorWithHighlights(
results,
documentMetaData.editor,
);
Expand All @@ -87,6 +87,13 @@ export function activateDiscardCommand(context: vscode.ExtensionContext): void {
data: { ...documentMetaData.editor.document },
});

getExtensionEventEmitter().fire({
type: 'CURRENT_PROJECT',
data: {
name: currentWorkSpaceFolder
},
});

await Promise.allSettled([
analyzeState.set(copyProblems),
feedbackService.discardSuggestion(payload, session),
Expand Down
1 change: 1 addition & 0 deletions ext-src/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export interface AnalysisEvents {
| 'No_Editor_Detected'
| 'FIX_SUGGESTION'
| 'CURRENT_FILE'
| 'CURRENT_PROJECT'
| 'INIT_DATA_UPON_NEW_FILE_OPEN';
data: any;
}
Expand Down
103 changes: 93 additions & 10 deletions ext-src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,8 @@ import {
import { Analyze } from './state';
import { Problem } from './types';

let previousEditor: vscode.TextEditor | undefined = undefined;

export function activate(context: vscode.ExtensionContext): void {
let previousEditor: vscode.TextEditor | undefined = undefined;
bootstrapExtensionEventEmitter();
debugChannel.show(true);
debugChannel.appendLine('Activating Metabob Extension...');
Expand Down Expand Up @@ -129,6 +128,7 @@ export function activate(context: vscode.ExtensionContext): void {
context.subscriptions.push(
vscode.workspace.onDidSaveTextDocument(document => {
let savedDocumentMetaData = Util.extractMetaDataFromDocument(document);
const currentWorkSpaceFolder = Util.getRootFolderName();
if (!savedDocumentMetaData.fileName) return;

let fileName: string = savedDocumentMetaData.fileName;
Expand Down Expand Up @@ -166,11 +166,19 @@ export function activate(context: vscode.ExtensionContext): void {
type: 'CURRENT_FILE',
data: { ...document },
});

extensionEventEmitter.fire({
type: 'CURRENT_PROJECT',
data: {
name: currentWorkSpaceFolder,
},
});
}),
);

context.subscriptions.push(
vscode.workspace.onDidCloseTextDocument(() => {
const currentWorkSpaceFolder = Util.getRootFolderName();
const editor = vscode.window.activeTextEditor;
if (!editor || !editor.document) {
extensionEventEmitter.fire({
Expand All @@ -196,24 +204,48 @@ export function activate(context: vscode.ExtensionContext): void {
type: 'CURRENT_FILE',
data: { ...editor.document },
});
extensionEventEmitter.fire({
type: 'CURRENT_PROJECT',
data: {
name: currentWorkSpaceFolder,
},
});
}
}),
);

context.subscriptions.push(
vscode.workspace.onDidOpenTextDocument((e: vscode.TextDocument) => {
const currentWorkSpaceFolder = Util.getRootFolderName();
const documentMetaData = Util.extractMetaDataFromDocument(e);
if (!documentMetaData.fileName) return
if (!documentMetaData.fileName) {
debugChannel.appendLine(
'onDidOpenTextDocument: fileName is undefined. ' +
'\n' +
documentMetaData.relativePath +
'\n' +
documentMetaData.filePath,
);
return;
}

const analyzeState = new Analyze(context);
const analyzeValue = analyzeState.get()?.value;
if (!analyzeValue) return;
if (!analyzeValue) {
debugChannel.appendLine('onDidOpenTextDocument: analyzeValue is undefined');

return;
}

const results: Problem[] | undefined = Util.getCurrentEditorProblems(
analyzeValue,
documentMetaData.fileName,
);
if (!results) return;
if (!results) {
debugChannel.appendLine('onDidOpenTextDocument: results is undefined');

return;
}

if (results.length === 0) {
extensionEventEmitter.fire({
Expand All @@ -233,6 +265,17 @@ export function activate(context: vscode.ExtensionContext): void {
type: 'CURRENT_FILE',
data: { ...e },
});

extensionEventEmitter.fire({
type: 'CURRENT_PROJECT',
data: {
name: currentWorkSpaceFolder,
},
});
debugChannel.appendLine(
'onDidOpenTextDocument: results have zero length. ' + documentMetaData.fileName,
);

return;
}

Expand All @@ -259,25 +302,46 @@ export function activate(context: vscode.ExtensionContext): void {
type: 'Analysis_Completed',
data: { shouldResetRecomendation: true, shouldMoveToAnalyzePage: true, ...analyzeValue },
});

extensionEventEmitter.fire({
type: 'CURRENT_PROJECT',
data: {
name: currentWorkSpaceFolder,
},
});
}),
);

context.subscriptions.push(
vscode.window.onDidChangeActiveTextEditor((e: vscode.TextEditor | undefined) => {
if (!e) return;
if (!e) {
debugChannel.appendLine('onDidChangeActiveTextEditor: e is undefined');
return;
}
const { fileName } = Util.extractMetaDataFromDocument(e.document);
if (!fileName) return;
if (!fileName) {
debugChannel.appendLine('onDidChangeActiveTextEditor: fileName is undefined');
return;
}

const currentWorkSpaceFolder = Util.getRootFolderName();

if (previousEditor) {
previousEditor.setDecorations(decorationType, []);
}

const analyzeState = new Analyze(context);
const analyzeValue = analyzeState.get()?.value;
if (!analyzeValue) return;
if (!analyzeValue) {
debugChannel.appendLine('onDidChangeActiveTextEditor: analyzeValue is undefined');
return;
}

const results: Problem[] | undefined = Util.getCurrentEditorProblems(analyzeValue, fileName);
if (!results) return;
if (!results) {
debugChannel.appendLine('onDidChangeActiveTextEditor: results is undefined');
return;
}

if (results.length === 0) {
extensionEventEmitter.fire({
Expand All @@ -296,6 +360,19 @@ export function activate(context: vscode.ExtensionContext): void {
type: 'CURRENT_FILE',
data: { ...e.document },
});

extensionEventEmitter.fire({
type: 'CURRENT_PROJECT',
data: {
name: currentWorkSpaceFolder,
},
});

debugChannel.appendLine(
'onDidChangeActiveTextEditor: results array has zero length. ' + fileName,
);

previousEditor = e;
return;
}

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

extensionEventEmitter.fire({
type: 'CURRENT_PROJECT',
data: {
name: currentWorkSpaceFolder,
},
});

previousEditor = e;
}),
);
Expand All @@ -340,5 +424,4 @@ export function deactivate(): void {
debugChannel.dispose();
decorationType.dispose();
disposeExtensionEventEmitter();
previousEditor = undefined;
}
60 changes: 52 additions & 8 deletions ext-src/helpers/HandleDocumentAnalyze.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,20 @@ export const handleDocumentAnalyze = async (
jobId?: string,
suppressRateLimitErrors = false,
) => {
const currentWorkSpaceFolder = Util.getRootFolderName();
const editor = vscode.window.activeTextEditor;
if (!editor || editor.document.fileName !== metaDataDocument.filePath) {
getExtensionEventEmitter().fire({
type: 'Analysis_Error',
data: '',
});

getExtensionEventEmitter().fire({
type: 'CURRENT_PROJECT',
data: {
name: currentWorkSpaceFolder
},
});
return failedResponseReturn;
}

Expand All @@ -58,6 +65,12 @@ export const handleDocumentAnalyze = async (
type: 'Analysis_Error',
data: '',
});
getExtensionEventEmitter().fire({
type: 'CURRENT_PROJECT',
data: {
name: currentWorkSpaceFolder
},
});
vscode.window.showErrorMessage(CONSTANTS.analyzeCommandTimeoutMessage);
}

Expand All @@ -67,6 +80,12 @@ export const handleDocumentAnalyze = async (
type: 'Analysis_Error',
data: '',
});
getExtensionEventEmitter().fire({
type: 'CURRENT_PROJECT',
data: {
name: currentWorkSpaceFolder
},
});
vscode.window.showErrorMessage(CONSTANTS.analyzeCommandErrorMessage);

return failedResponseReturn;
Expand All @@ -83,6 +102,12 @@ export const handleDocumentAnalyze = async (
type: 'Analysis_Error',
data: '',
});
getExtensionEventEmitter().fire({
type: 'CURRENT_PROJECT',
data: {
name: currentWorkSpaceFolder
},
});
vscode.window.showErrorMessage(CONSTANTS.analyzeCommandErrorMessage);

return failedResponseReturn;
Expand Down Expand Up @@ -115,7 +140,7 @@ export const handleDocumentAnalyze = async (
}
return true;
})
.filter((vulnerability) => {
.filter(vulnerability => {
const { endLine, startLine } = vulnerability;
const range = new vscode.Range(
startLine - 1,
Expand All @@ -124,11 +149,14 @@ export const handleDocumentAnalyze = async (
documentMetaData.editor.document.lineAt(endLine - 1).text.length,
);

const text = documentMetaData.editor.document.getText(range).replace("\n", "").replace("\t", "")
const text = documentMetaData.editor.document
.getText(range)
.replace('\n', '')
.replace('\t', '');
if (text.length === 0 || text === '' || text === ' ') {
return false
return false;
}
return true
return true;
})
.forEach(problem => {
const key = `${problem.path}@@${problem.id}`;
Expand All @@ -139,6 +167,7 @@ export const handleDocumentAnalyze = async (
isDiscarded: problem.discarded,
isEndorsed: problem.endorsed,
isViewed: false,
fullFilePath: currentWorkSpaceFolder,
};
results[key] = { ...analyzeMetaData };
});
Expand All @@ -149,21 +178,36 @@ export const handleDocumentAnalyze = async (
type: 'Analysis_Error',
data: '',
});
getExtensionEventEmitter().fire({
type: 'CURRENT_PROJECT',
data: {
name: currentWorkSpaceFolder
},
});
vscode.window.showErrorMessage(CONSTANTS.analyzeCommandErrorMessage);

return failedResponseReturn;
}

Util.decorateCurrentEditorWithHighlights(
problems,
documentMetaData.editor,
);
const path = problems.map(item => item.path);

const isUserOnValidEditor = path.includes(documentMetaData.fileName);
if (isUserOnValidEditor) {
Util.decorateCurrentEditorWithHighlights(problems, documentMetaData.editor);
}

await analyzeState.set(results);
getExtensionEventEmitter().fire({
type: 'Analysis_Completed',
data: { shouldResetRecomendation: true, shouldMoveToAnalyzePage: true, ...results },
});

getExtensionEventEmitter().fire({
type: 'CURRENT_PROJECT',
data: {
name: currentWorkSpaceFolder
},
});

return verifiedResponse;
};
Loading

0 comments on commit 5bff00d

Please sign in to comment.