Skip to content

Commit 4c86828

Browse files
authored
Merge pull request #50 from MetabobProject/fix/analyze-button
Fix analyze button disabled when analysis fails
2 parents 9bcefb6 + f2fc157 commit 4c86828

File tree

2 files changed

+77
-38
lines changed

2 files changed

+77
-38
lines changed

ext-src/commands/AnalyzeDocument.ts

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

9-
export function activateAnalyzeCommand(context: vscode.ExtensionContext, _debug?: vscode.OutputChannel): void {
9+
export function activateAnalyzeCommand(
10+
context: vscode.ExtensionContext,
11+
_debug?: vscode.OutputChannel,
12+
): void {
1013
const command = CONSTANTS.analyzeDocumentCommand;
1114

1215
const commandHandler = async () => {
@@ -17,7 +20,9 @@ export function activateAnalyzeCommand(context: vscode.ExtensionContext, _debug?
1720
const sessionToken = new Session(context).get()?.value;
1821

1922
const editor = vscode.window.activeTextEditor;
20-
_debug?.appendLine('AnalyzeDocument.ts: activateAnalyzeCommand: editor: ' + JSON.stringify(editor));
23+
_debug?.appendLine(
24+
'AnalyzeDocument.ts: activateAnalyzeCommand: editor: ' + JSON.stringify(editor),
25+
);
2126

2227
// If the user has not opened any file then we can't perform any analysis.
2328
if (!editor) {
@@ -28,7 +33,11 @@ export function activateAnalyzeCommand(context: vscode.ExtensionContext, _debug?
2833
throw new Error('activateAnalyzeCommand: Editor is undefined');
2934
}
3035

31-
_debug?.appendLine('AnalyzeDocument.ts: activateAnalyzeCommand: editor.document: ' + JSON.stringify(editor.document));
36+
_debug?.appendLine(
37+
'AnalyzeDocument.ts: activateAnalyzeCommand: editor.document: ' +
38+
JSON.stringify(editor.document),
39+
);
40+
3241
// If the user has not opened valid document i.e settings page or any other page
3342
// that is not a code file we will throw an error.
3443
if (!Util.isValidDocument(editor.document)) {
@@ -41,7 +50,10 @@ export function activateAnalyzeCommand(context: vscode.ExtensionContext, _debug?
4150
throw new Error('activateAnalyzeCommand: Selected Document is not valid');
4251
}
4352

44-
_debug?.appendLine('AnalyzeDocument.ts: activateAnalyzeCommand: Session Token: ' + sessionToken);
53+
_debug?.appendLine(
54+
'AnalyzeDocument.ts: activateAnalyzeCommand: Session Token: ' + sessionToken,
55+
);
56+
4557
// If the user session is not available then we can't request file analysis.
4658
if (!sessionToken) {
4759
vscode.window.showErrorMessage(CONSTANTS.sessionTokenUndefined);
@@ -53,10 +65,21 @@ export function activateAnalyzeCommand(context: vscode.ExtensionContext, _debug?
5365
}
5466

5567
const documentMetaData = Util.extractMetaDataFromDocument(editor.document);
56-
_debug?.appendLine('AnalyzeDocument.ts: activateAnalyzeCommand: documentMetaData: ' + JSON.stringify(documentMetaData));
68+
_debug?.appendLine(
69+
'AnalyzeDocument.ts: activateAnalyzeCommand: documentMetaData: ' +
70+
JSON.stringify(documentMetaData),
71+
);
5772

5873
Util.withProgress<SubmitRepresentationResponse>(
59-
handleDocumentAnalyze(documentMetaData, sessionToken, analyzeState, context, undefined, true, _debug),
74+
handleDocumentAnalyze(
75+
documentMetaData,
76+
sessionToken,
77+
analyzeState,
78+
context,
79+
undefined,
80+
true,
81+
_debug,
82+
),
6083
CONSTANTS.analyzeCommandProgressMessage,
6184
).then(response => {
6285
if (response.status === 'pending' || response.status === 'running') {

ext-src/helpers/HandleDocumentAnalyze.ts

+48-32
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { AnalyzeState, Analyze, AnalyseMetaData } from '../state';
66
import Util from '../utils';
77
import CONSTANTS from '../constants';
88
import { getExtensionEventEmitter } from '../events';
9-
import path from 'path'
9+
import path from 'path';
1010

1111
const failedResponseReturn: SubmitRepresentationResponse = { jobId: '', status: 'failed' };
1212

@@ -47,7 +47,7 @@ export const handleDocumentAnalyze = async (
4747
getExtensionEventEmitter().fire({
4848
type: 'CURRENT_PROJECT',
4949
data: {
50-
name: currentWorkSpaceFolder
50+
name: currentWorkSpaceFolder,
5151
},
5252
});
5353

@@ -58,27 +58,27 @@ export const handleDocumentAnalyze = async (
5858
jobId !== undefined
5959
? await submitService.getJobStatus(sessionToken, jobId)
6060
: await submitService.submitTextFile(
61-
metaDataDocument.relativePath,
62-
metaDataDocument.fileContent,
63-
metaDataDocument.filePath,
64-
sessionToken,
65-
);
61+
metaDataDocument.relativePath,
62+
metaDataDocument.fileContent,
63+
metaDataDocument.filePath,
64+
sessionToken,
65+
);
6666

6767
const verifiedResponse = verifyResponseOfSubmit(response);
6868
if (!verifiedResponse || !verifiedResponse.results) {
6969
if (!suppressRateLimitErrors) {
70-
getExtensionEventEmitter().fire({
71-
type: 'Analysis_Error',
72-
data: '',
73-
});
74-
getExtensionEventEmitter().fire({
75-
type: 'CURRENT_PROJECT',
76-
data: {
77-
name: currentWorkSpaceFolder
78-
},
79-
});
8070
vscode.window.showErrorMessage(CONSTANTS.analyzeCommandTimeoutMessage);
8171
}
72+
getExtensionEventEmitter().fire({
73+
type: 'Analysis_Error',
74+
data: '',
75+
});
76+
getExtensionEventEmitter().fire({
77+
type: 'CURRENT_PROJECT',
78+
data: {
79+
name: currentWorkSpaceFolder,
80+
},
81+
});
8282

8383
return failedResponseReturn;
8484
} else if (verifiedResponse.status === 'failed') {
@@ -89,7 +89,7 @@ export const handleDocumentAnalyze = async (
8989
getExtensionEventEmitter().fire({
9090
type: 'CURRENT_PROJECT',
9191
data: {
92-
name: currentWorkSpaceFolder
92+
name: currentWorkSpaceFolder,
9393
},
9494
});
9595
vscode.window.showErrorMessage(CONSTANTS.analyzeCommandErrorMessage);
@@ -111,7 +111,7 @@ export const handleDocumentAnalyze = async (
111111
getExtensionEventEmitter().fire({
112112
type: 'CURRENT_PROJECT',
113113
data: {
114-
name: currentWorkSpaceFolder
114+
name: currentWorkSpaceFolder,
115115
},
116116
});
117117
vscode.window.showErrorMessage(CONSTANTS.analyzeCommandErrorMessage);
@@ -121,14 +121,30 @@ export const handleDocumentAnalyze = async (
121121

122122
// convert problem paths to absolute path and normalize them
123123
const workspaceFolderPath = vscode.workspace.workspaceFolders?.[0].uri.fsPath;
124-
if (!workspaceFolderPath) {return failedResponseReturn;}
124+
if (!workspaceFolderPath) {
125+
getExtensionEventEmitter().fire({
126+
type: 'Analysis_Error',
127+
data: '',
128+
});
129+
getExtensionEventEmitter().fire({
130+
type: 'CURRENT_PROJECT',
131+
data: {
132+
name: currentWorkSpaceFolder,
133+
},
134+
});
135+
136+
return failedResponseReturn;
137+
}
125138
verifiedResponse.results.forEach(result => {
126139
result.path = path.join(workspaceFolderPath, result.path);
127140
});
128141

129142
let results: AnalyzeState = {};
130143
const analyzeStateValue = new Analyze(context).get()?.value;
131-
_debug?.appendLine('AnalyzeDocument.ts: handleDocumentAnalyze: analyzeStateValue: ' + JSON.stringify(analyzeStateValue));
144+
_debug?.appendLine(
145+
'AnalyzeDocument.ts: handleDocumentAnalyze: analyzeStateValue: ' +
146+
JSON.stringify(analyzeStateValue),
147+
);
132148

133149
if (analyzeStateValue) {
134150
const responseProblemsFilePaths = verifiedResponse.results.map(problem => {
@@ -163,10 +179,7 @@ export const handleDocumentAnalyze = async (
163179
currFile.editor.document.lineAt(endLine - 1).text.length,
164180
);
165181

166-
const text = currFile.editor.document
167-
.getText(range)
168-
.replace('\n', '')
169-
.replace('\t', '');
182+
const text = currFile.editor.document.getText(range).replace('\n', '').replace('\t', '');
170183
if (text.length === 0 || text === '' || text === ' ') {
171184
return false;
172185
}
@@ -190,7 +203,9 @@ export const handleDocumentAnalyze = async (
190203

191204
_debug?.appendLine('AnalyzeDocument.ts: Document File path' + currFile.absPath);
192205
const problems = Util.getCurrentEditorProblems(results, currFile.absPath);
193-
_debug?.appendLine('AnalyzeDocument.ts: handleDocumentAnalyze: problems: ' + JSON.stringify(problems));
206+
_debug?.appendLine(
207+
'AnalyzeDocument.ts: handleDocumentAnalyze: problems: ' + JSON.stringify(problems),
208+
);
194209
if (!problems) {
195210
getExtensionEventEmitter().fire({
196211
type: 'Analysis_Error',
@@ -199,7 +214,7 @@ export const handleDocumentAnalyze = async (
199214
getExtensionEventEmitter().fire({
200215
type: 'CURRENT_PROJECT',
201216
data: {
202-
name: currentWorkSpaceFolder
217+
name: currentWorkSpaceFolder,
203218
},
204219
});
205220
vscode.window.showErrorMessage(CONSTANTS.analyzeCommandErrorMessage);
@@ -210,7 +225,9 @@ export const handleDocumentAnalyze = async (
210225
const paths = problems.map(item => item.path);
211226

212227
const isUserOnValidEditor = paths.includes(currFile.absPath);
213-
_debug?.appendLine('AnalyzeDocument.ts: handleDocumentAnalyze: isUserOnValidEditor: ' + isUserOnValidEditor);
228+
_debug?.appendLine(
229+
'AnalyzeDocument.ts: handleDocumentAnalyze: isUserOnValidEditor: ' + isUserOnValidEditor,
230+
);
214231
if (isUserOnValidEditor) {
215232
Util.decorateCurrentEditorWithHighlights(problems, currFile.editor, _debug);
216233
}
@@ -226,14 +243,13 @@ export const handleDocumentAnalyze = async (
226243
getExtensionEventEmitter().fire({
227244
type: 'CURRENT_PROJECT',
228245
data: {
229-
name: currentWorkSpaceFolder
246+
name: currentWorkSpaceFolder,
230247
},
231248
});
232249

233-
return verifiedResponse
250+
return verifiedResponse;
234251
}
235252

236-
237253
getExtensionEventEmitter().fire({
238254
type: 'Analysis_Completed',
239255
data: { shouldResetRecomendation: true, shouldMoveToAnalyzePage: true, ...results },
@@ -242,7 +258,7 @@ export const handleDocumentAnalyze = async (
242258
getExtensionEventEmitter().fire({
243259
type: 'CURRENT_PROJECT',
244260
data: {
245-
name: currentWorkSpaceFolder
261+
name: currentWorkSpaceFolder,
246262
},
247263
});
248264

0 commit comments

Comments
 (0)