Skip to content

Commit 5c9eda8

Browse files
committed
Moves parseAu3CheckOutput to separate file
1 parent f4412d8 commit 5c9eda8

File tree

3 files changed

+67
-72
lines changed

3 files changed

+67
-72
lines changed

src/checkAutoItCode.js

-36
This file was deleted.

src/diagnosticUtils.js

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import { Diagnostic, DiagnosticSeverity, Range, Position, Uri } from 'vscode';
2+
3+
export const getDiagnosticSeverity = severityString => {
4+
switch (severityString) {
5+
case 'warning':
6+
return DiagnosticSeverity.Warning;
7+
default:
8+
return DiagnosticSeverity.Error;
9+
}
10+
};
11+
12+
export const getDiagnosticRange = (line, position) => {
13+
const diagnosticPosition = new Position(parseInt(line, 10) - 1, parseInt(position, 10) - 1);
14+
15+
return new Range(diagnosticPosition, diagnosticPosition);
16+
};
17+
18+
export const updateDiagnostics = (currentDiagnostics, scriptPath, range, description, severity) => {
19+
const diagnosticToAdd = new Diagnostic(range, description, severity);
20+
const updatedDiagnostics = currentDiagnostics;
21+
22+
if (!(scriptPath in updatedDiagnostics)) {
23+
updatedDiagnostics[scriptPath] = [];
24+
}
25+
updatedDiagnostics[scriptPath].push(diagnosticToAdd);
26+
27+
return updatedDiagnostics;
28+
};
29+
30+
/**
31+
* Processes the results of AU3Check, identifies warnings and errors.
32+
* @param {string} output Text returned from AU3Check.
33+
* @param {vscode.DiagnosticCollection} collection - The diagnostic collection to update.
34+
*/
35+
export const parseAu3CheckOutput = (output, collection) => {
36+
const OUTPUT_REGEXP = /"(?<scriptPath>.+)"\((?<line>\d{1,4}),(?<position>\d{1,4})\)\s:\s(?<severity>warning|error):\s(?<description>.+)\./gm;
37+
let matches = null;
38+
let diagnosticRange;
39+
let diagnosticSeverity;
40+
let diagnostics = {};
41+
42+
matches = OUTPUT_REGEXP.exec(output);
43+
while (matches !== null) {
44+
diagnosticRange = getDiagnosticRange(matches.groups.line, matches.groups.position);
45+
diagnosticSeverity = getDiagnosticSeverity(matches.groups.severity);
46+
47+
diagnostics = updateDiagnostics(
48+
diagnostics,
49+
matches.groups.scriptPath,
50+
diagnosticRange,
51+
matches.groups.description,
52+
diagnosticSeverity,
53+
);
54+
55+
matches = OUTPUT_REGEXP.exec(output);
56+
}
57+
58+
Object.keys(diagnostics).forEach(scriptPath => {
59+
collection.set(Uri.file(scriptPath), diagnostics[scriptPath]);
60+
});
61+
};
62+
63+
export default parseAu3CheckOutput;

src/extension.js

+4-36
Original file line numberDiff line numberDiff line change
@@ -11,43 +11,11 @@ const workspaceSymbolsFeature = require('./ai_workspaceSymbols');
1111
const goToDefinitionFeature = require('./ai_definition');
1212

1313
const { registerCommands } = require('./registerCommands');
14-
const {
15-
getDiagnosticRange,
16-
getDiagnosticSeverity,
17-
updateDiagnostics,
18-
} = require('./checkAutoItCode');
14+
const { parseAu3CheckOutput } = require('./diagnosticUtils');
1915

2016
let diagnosticCollection;
2117

22-
const parseAu3CheckOutput = (document, output) => {
23-
const OUTPUT_REGEXP = /"(?<scriptPath>.+)"\((?<line>\d{1,4}),(?<position>\d{1,4})\)\s:\s(?<severity>warning|error):\s(?<description>.+)\./gm;
24-
let matches = null;
25-
let diagnosticRange;
26-
let diagnosticSeverity;
27-
let diagnostics = {};
28-
29-
matches = OUTPUT_REGEXP.exec(output);
30-
while (matches !== null) {
31-
diagnosticRange = getDiagnosticRange(matches.groups.line, matches.groups.position);
32-
diagnosticSeverity = getDiagnosticSeverity(matches.groups.severity);
33-
34-
diagnostics = updateDiagnostics(
35-
diagnostics,
36-
matches.groups.scriptPath,
37-
diagnosticRange,
38-
matches.groups.description,
39-
diagnosticSeverity,
40-
);
41-
42-
matches = OUTPUT_REGEXP.exec(output);
43-
}
44-
45-
Object.keys(diagnostics).forEach(scriptPath => {
46-
diagnosticCollection.set(vscode.Uri.file(scriptPath), diagnostics[scriptPath]);
47-
});
48-
};
49-
50-
function checkAutoItCode(document) {
18+
const checkAutoItCode = document => {
5119
const { checkPath, enableDiagnostics } = vscode.workspace.getConfiguration('autoit');
5220

5321
diagnosticCollection.clear();
@@ -75,13 +43,13 @@ function checkAutoItCode(document) {
7543
if (data.length === 0) {
7644
return;
7745
}
78-
parseAu3CheckOutput(document, data.toString());
46+
parseAu3CheckOutput(data.toString(), diagnosticCollection);
7947
});
8048

8149
checkProcess.stderr.on('error', error => {
8250
vscode.window.showErrorMessage(`${checkPath} error: ${error}`);
8351
});
84-
}
52+
};
8553

8654
const activate = ctx => {
8755
const features = [

0 commit comments

Comments
 (0)