diff --git a/src/checkAutoItCode.js b/src/checkAutoItCode.js deleted file mode 100644 index 6a5b6f8..0000000 --- a/src/checkAutoItCode.js +++ /dev/null @@ -1,36 +0,0 @@ -// const configuration = workspace.getConfiguration('autoit'); -// const { checkPath } = configuration; -import { Diagnostic, DiagnosticSeverity, Range, Position } 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; -}; - -const checkAutoItCode = () => { - return null; -}; - -export default checkAutoItCode; diff --git a/src/diagnosticUtils.js b/src/diagnosticUtils.js new file mode 100644 index 0000000..d54fd9f --- /dev/null +++ b/src/diagnosticUtils.js @@ -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 = /"(?.+)"\((?\d{1,4}),(?\d{1,4})\)\s:\s(?warning|error):\s(?.+)\./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; diff --git a/src/extension.js b/src/extension.js index d90c65a..05d4aa2 100644 --- a/src/extension.js +++ b/src/extension.js @@ -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 = /"(?.+)"\((?\d{1,4}),(?\d{1,4})\)\s:\s(?warning|error):\s(?.+)\./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(); @@ -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 = [