Skip to content

Commit 6ba9a29

Browse files
authored
Merge pull request #43 from MetabobProject/fix/fs-path-handling
Fix for file structure handling
2 parents 3612be9 + 901e618 commit 6ba9a29

File tree

5 files changed

+30
-15
lines changed

5 files changed

+30
-15
lines changed

ext-src/commands/AnalyzeDocument.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import CONSTANTS from '../constants';
66
import Util from '../utils';
77
import { getExtensionEventEmitter } from '../events';
88

9-
export function activateAnalyzeCommand(context: vscode.ExtensionContext): void {
9+
export function activateAnalyzeCommand(context: vscode.ExtensionContext, _debug?: vscode.OutputChannel): void {
1010
const command = CONSTANTS.analyzeDocumentCommand;
1111

1212
const commandHandler = async () => {
@@ -17,6 +17,7 @@ export function activateAnalyzeCommand(context: vscode.ExtensionContext): void {
1717
const sessionToken = new Session(context).get()?.value;
1818

1919
const editor = vscode.window.activeTextEditor;
20+
_debug?.appendLine('AnalyzeDocument.ts: activateAnalyzeCommand: editor: ' + JSON.stringify(editor));
2021

2122
// If the user has not opened any file then we can't perform any analysis.
2223
if (!editor) {
@@ -53,7 +54,7 @@ export function activateAnalyzeCommand(context: vscode.ExtensionContext): void {
5354
const documentMetaData = Util.extractMetaDataFromDocument(editor.document);
5455

5556
Util.withProgress<SubmitRepresentationResponse>(
56-
handleDocumentAnalyze(documentMetaData, sessionToken, analyzeState, context),
57+
handleDocumentAnalyze(documentMetaData, sessionToken, analyzeState, context, undefined, true, _debug),
5758
CONSTANTS.analyzeCommandProgressMessage,
5859
).then(response => {
5960
if (response.status === 'pending' || response.status === 'running') {

ext-src/extension.ts

+6-3
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ let expirationTimer: any = undefined;
2929

3030
export function activate(context: vscode.ExtensionContext): void {
3131
let previousEditor: vscode.TextEditor | undefined = undefined;
32+
const _debug = undefined; // vscode.window.createOutputChannel('Metabob');
3233
bootstrapExtensionEventEmitter();
3334

3435
initState(context);
@@ -56,7 +57,7 @@ export function activate(context: vscode.ExtensionContext): void {
5657

5758
// Analyze command that hit /analyze endpoint with current file content
5859
// then decorate current file with error
59-
activateAnalyzeCommand(context);
60+
activateAnalyzeCommand(context, _debug);
6061

6162
// If the user Discard a suggestion, it would be removed from decoration
6263
// and the global state as well
@@ -129,14 +130,15 @@ export function activate(context: vscode.ExtensionContext): void {
129130
);
130131

131132
const extensionEventEmitter = getExtensionEventEmitter();
133+
132134
// Analyze on Save functionality is only ran if the user enabled it.
133135
context.subscriptions.push(
134136
vscode.workspace.onDidSaveTextDocument(document => {
135-
let savedDocumentMetaData = Util.extractMetaDataFromDocument(document);
137+
const savedDocumentMetaData = Util.extractMetaDataFromDocument(document);
136138
const currentWorkSpaceFolder = Util.getRootFolderName();
137139
if (!savedDocumentMetaData.fileName) return;
138140

139-
let fileName: string = savedDocumentMetaData.fileName;
141+
const fileName: string = savedDocumentMetaData.fileName;
140142

141143
// Will check if the current document is valid code file.
142144
if (!(analyzeDocumentOnSaveConfig && analyzeDocumentOnSaveConfig === true)) {
@@ -417,6 +419,7 @@ export function activate(context: vscode.ExtensionContext): void {
417419
});
418420

419421
previousEditor = e;
422+
420423
return;
421424
}
422425

ext-src/helpers/GenerateDecorations.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export function GenerateDecorations(
1313
results: Problem[],
1414
editor: vscode.TextEditor,
1515
jobId?: string,
16+
_debug?: vscode.OutputChannel,
1617
): {
1718
decorationType: vscode.TextEditorDecorationType;
1819
decorations: vscode.DecorationOptions[];
@@ -23,6 +24,7 @@ export function GenerateDecorations(
2324
if ((endLine - 1) < 0 || (startLine - 1) < 0) {
2425
return false;
2526
}
27+
2628
return true;
2729
})
2830
.map(vulnerability => {
@@ -43,7 +45,7 @@ export function GenerateDecorations(
4345
};
4446

4547
const viewDescriptionURI = encodeURIComponent(JSON.stringify(payload));
46-
48+
_debug?.appendLine("Decoration: " + viewDescriptionURI);
4749
const hoverFixMessage = `**[Fix](command:metabob.fixSuggestion?${viewDescriptionURI} "This action will display a comprehensive view of the issue along with a recommended solution.")**`;
4850
const hoverViewDescriptionMessage = `**[More Details](command:metabob.showDetailSuggestion?${viewDescriptionURI} "This action will display a comprehensive view of the issue.")**`;
4951
const hoverMessage = new vscode.MarkdownString(

ext-src/helpers/HandleDocumentAnalyze.ts

+9-2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export const handleDocumentAnalyze = async (
3030
context: vscode.ExtensionContext,
3131
jobId?: string,
3232
suppressRateLimitErrors = false,
33+
_debug?: vscode.OutputChannel,
3334
) => {
3435
const tomorrow = new Date();
3536
tomorrow.setDate(tomorrow.getDate() + 1);
@@ -48,6 +49,7 @@ export const handleDocumentAnalyze = async (
4849
name: currentWorkSpaceFolder
4950
},
5051
});
52+
5153
return failedResponseReturn;
5254
}
5355

@@ -118,13 +120,14 @@ export const handleDocumentAnalyze = async (
118120

119121
let results: AnalyzeState = {};
120122
const analyzeStateValue = new Analyze(context).get()?.value;
123+
_debug?.appendLine('AnalyzeDocument.ts: handleDocumentAnalyze: analyzeStateValue: ' + JSON.stringify(analyzeStateValue));
121124

122125
if (analyzeStateValue) {
123126
const aggregatedProblemsFilePaths = verifiedResponse.results.map(problem => {
124127
return problem.path;
125128
});
126129

127-
let buggerAnalyzeStateValue: AnalyzeState = {};
130+
const buggerAnalyzeStateValue: AnalyzeState = {};
128131

129132
Object.keys(analyzeStateValue).forEach(key => {
130133
const problem = analyzeStateValue[key];
@@ -141,6 +144,7 @@ export const handleDocumentAnalyze = async (
141144
if (endLine - 1 < 0 || startLine - 1 < 0) {
142145
return false;
143146
}
147+
144148
return true;
145149
})
146150
.filter(vulnerability => {
@@ -159,6 +163,7 @@ export const handleDocumentAnalyze = async (
159163
if (text.length === 0 || text === '' || text === ' ') {
160164
return false;
161165
}
166+
162167
return true;
163168
})
164169
.forEach(problem => {
@@ -177,6 +182,7 @@ export const handleDocumentAnalyze = async (
177182
});
178183

179184
const problems = Util.getCurrentEditorProblems(results, documentMetaData.fileName);
185+
_debug?.appendLine('AnalyzeDocument.ts: handleDocumentAnalyze: problems: ' + JSON.stringify(problems));
180186
if (!problems) {
181187
getExtensionEventEmitter().fire({
182188
type: 'Analysis_Error',
@@ -196,8 +202,9 @@ export const handleDocumentAnalyze = async (
196202
const path = problems.map(item => item.path);
197203

198204
const isUserOnValidEditor = path.includes(documentMetaData.fileName);
205+
_debug?.appendLine('AnalyzeDocument.ts: handleDocumentAnalyze: isUserOnValidEditor: ' + isUserOnValidEditor);
199206
if (isUserOnValidEditor) {
200-
Util.decorateCurrentEditorWithHighlights(problems, documentMetaData.editor);
207+
Util.decorateCurrentEditorWithHighlights(problems, documentMetaData.editor, _debug);
201208
}
202209

203210
await analyzeState.set(results);

ext-src/utils.ts

+9-7
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ export default class Utils {
6565
static sleep = (ms: number): Promise<void> => new Promise(res => setTimeout(res, ms));
6666

6767
static getWorkspacePath(): string | undefined {
68-
let folders = workspace.workspaceFolders;
69-
let path = folders ? folders![0].uri.fsPath : undefined;
68+
const folders = workspace.workspaceFolders;
69+
const path = folders ? folders![0].uri.fsPath : undefined;
7070
if (path === undefined) {
7171
return undefined
7272
}
@@ -87,9 +87,9 @@ export default class Utils {
8787
}
8888

8989
static extractMetaDataFromDocument(document: vscode.TextDocument): IDocumentMetaData {
90-
const filePath = document.uri.fsPath;
90+
const filePath = document.uri.path;
9191
const workspaceFolder = workspace.getWorkspaceFolder(document.uri);
92-
const relativePath = workspaceFolder ? path.relative(workspaceFolder.uri.fsPath, filePath) : '';
92+
const relativePath = workspaceFolder ? path.relative(workspaceFolder.uri.path, filePath) : '';
9393
const splitKey: string | undefined = relativePath.split('/').pop();
9494
const fileContent = document.getText();
9595
const isTextDocument = Utils.isTextDocument(document);
@@ -145,6 +145,7 @@ export default class Utils {
145145

146146
if (workspaceFolders) {
147147
const rootFolder = workspaceFolders[0];
148+
148149
return rootFolder.name;
149150
}
150151

@@ -161,8 +162,8 @@ export default class Utils {
161162
return undefined;
162163
}
163164

164-
let documentMetaData = this.extractMetaDataFromDocument(editor.document);
165-
let fileName: string | undefined = documentMetaData.fileName;
165+
const documentMetaData = this.extractMetaDataFromDocument(editor.document);
166+
const fileName: string | undefined = documentMetaData.fileName;
166167
if (!fileName) return undefined
167168

168169
return {
@@ -195,7 +196,7 @@ export default class Utils {
195196
return results
196197
}
197198

198-
static decorateCurrentEditorWithHighlights(problems: Problem[], problemEditor: vscode.TextEditor): boolean {
199+
static decorateCurrentEditorWithHighlights(problems: Problem[], problemEditor: vscode.TextEditor, _debug?: vscode.OutputChannel): boolean {
199200
const currentEditor = vscode.window.activeTextEditor;
200201
if (!currentEditor) return false;
201202

@@ -204,6 +205,7 @@ export default class Utils {
204205
if (!isUserOnProblemEditor) return false;
205206

206207
const { decorations } = GenerateDecorations(problems, currentEditor);
208+
_debug?.appendLine('Decorations: ' + JSON.stringify(decorations));
207209
problemEditor.setDecorations(decorationType, []);
208210
problemEditor.setDecorations(decorationType, decorations);
209211

0 commit comments

Comments
 (0)