|
| 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; |
0 commit comments