Skip to content

Commit

Permalink
Moves parseAu3CheckOutput to separate file
Browse files Browse the repository at this point in the history
  • Loading branch information
loganch committed Apr 28, 2021
1 parent f4412d8 commit 5c9eda8
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 72 deletions.
36 changes: 0 additions & 36 deletions src/checkAutoItCode.js

This file was deleted.

63 changes: 63 additions & 0 deletions src/diagnosticUtils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { Diagnostic, DiagnosticSeverity, Range, Position, Uri } from 'vscode';

export const getDiagnosticSeverity = severityString => {
switch (severityString) {
case 'warning':
return DiagnosticSeverity.Warning;
default:
return DiagnosticSeverity.Error;
}
};

export const getDiagnosticRange = (line, position) => {
const diagnosticPosition = new Position(parseInt(line, 10) - 1, parseInt(position, 10) - 1);

return new Range(diagnosticPosition, diagnosticPosition);
};

export const updateDiagnostics = (currentDiagnostics, scriptPath, range, description, severity) => {
const diagnosticToAdd = new Diagnostic(range, description, severity);
const updatedDiagnostics = currentDiagnostics;

if (!(scriptPath in updatedDiagnostics)) {
updatedDiagnostics[scriptPath] = [];
}
updatedDiagnostics[scriptPath].push(diagnosticToAdd);

return updatedDiagnostics;
};

/**
* Processes the results of AU3Check, identifies warnings and errors.
* @param {string} output Text returned from AU3Check.
* @param {vscode.DiagnosticCollection} collection - The diagnostic collection to update.
*/
export const parseAu3CheckOutput = (output, collection) => {
const OUTPUT_REGEXP = /"(?<scriptPath>.+)"\((?<line>\d{1,4}),(?<position>\d{1,4})\)\s:\s(?<severity>warning|error):\s(?<description>.+)\./gm;
let matches = null;
let diagnosticRange;
let diagnosticSeverity;
let diagnostics = {};

matches = OUTPUT_REGEXP.exec(output);
while (matches !== null) {
diagnosticRange = getDiagnosticRange(matches.groups.line, matches.groups.position);
diagnosticSeverity = getDiagnosticSeverity(matches.groups.severity);

diagnostics = updateDiagnostics(
diagnostics,
matches.groups.scriptPath,
diagnosticRange,
matches.groups.description,
diagnosticSeverity,
);

matches = OUTPUT_REGEXP.exec(output);
}

Object.keys(diagnostics).forEach(scriptPath => {
collection.set(Uri.file(scriptPath), diagnostics[scriptPath]);
});
};

export default parseAu3CheckOutput;
40 changes: 4 additions & 36 deletions src/extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,43 +11,11 @@ const workspaceSymbolsFeature = require('./ai_workspaceSymbols');
const goToDefinitionFeature = require('./ai_definition');

const { registerCommands } = require('./registerCommands');
const {
getDiagnosticRange,
getDiagnosticSeverity,
updateDiagnostics,
} = require('./checkAutoItCode');
const { parseAu3CheckOutput } = require('./diagnosticUtils');

let diagnosticCollection;

const parseAu3CheckOutput = (document, output) => {
const OUTPUT_REGEXP = /"(?<scriptPath>.+)"\((?<line>\d{1,4}),(?<position>\d{1,4})\)\s:\s(?<severity>warning|error):\s(?<description>.+)\./gm;
let matches = null;
let diagnosticRange;
let diagnosticSeverity;
let diagnostics = {};

matches = OUTPUT_REGEXP.exec(output);
while (matches !== null) {
diagnosticRange = getDiagnosticRange(matches.groups.line, matches.groups.position);
diagnosticSeverity = getDiagnosticSeverity(matches.groups.severity);

diagnostics = updateDiagnostics(
diagnostics,
matches.groups.scriptPath,
diagnosticRange,
matches.groups.description,
diagnosticSeverity,
);

matches = OUTPUT_REGEXP.exec(output);
}

Object.keys(diagnostics).forEach(scriptPath => {
diagnosticCollection.set(vscode.Uri.file(scriptPath), diagnostics[scriptPath]);
});
};

function checkAutoItCode(document) {
const checkAutoItCode = document => {
const { checkPath, enableDiagnostics } = vscode.workspace.getConfiguration('autoit');

diagnosticCollection.clear();
Expand Down Expand Up @@ -75,13 +43,13 @@ function checkAutoItCode(document) {
if (data.length === 0) {
return;
}
parseAu3CheckOutput(document, data.toString());
parseAu3CheckOutput(data.toString(), diagnosticCollection);
});

checkProcess.stderr.on('error', error => {
vscode.window.showErrorMessage(`${checkPath} error: ${error}`);
});
}
};

const activate = ctx => {
const features = [
Expand Down

0 comments on commit 5c9eda8

Please sign in to comment.