From 4f0cf64ee111065cd43bee821e4ef2521384a313 Mon Sep 17 00:00:00 2001 From: Chet Joswig Date: Sat, 26 Oct 2024 19:47:00 -0700 Subject: [PATCH] Include context for parse errors --- src/utilities/codemirror/vml/vmlLinter.ts | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/utilities/codemirror/vml/vmlLinter.ts b/src/utilities/codemirror/vml/vmlLinter.ts index 279876d7bc..5e73f1773e 100644 --- a/src/utilities/codemirror/vml/vmlLinter.ts +++ b/src/utilities/codemirror/vml/vmlLinter.ts @@ -1,6 +1,6 @@ import { syntaxTree } from '@codemirror/language'; import { linter, type Diagnostic } from '@codemirror/lint'; -import type { Extension } from '@codemirror/state'; +import type { Extension, Text } from '@codemirror/state'; import type { SyntaxNode, Tree } from '@lezer/common'; import type { CommandDictionary, FswCommand, FswCommandArgument } from '@nasa-jpl/aerie-ampcs'; import type { EditorView } from 'codemirror'; @@ -35,12 +35,12 @@ export function vmlLinter(commandDictionary: CommandDictionary | null = null): E return linter(view => { const diagnostics: Diagnostic[] = []; const tree = syntaxTree(view.state); - diagnostics.push(...validateParserErrors(tree)); + const sequence = view.state.sliceDoc(); + diagnostics.push(...validateParserErrors(tree, sequence, view.state.toText(sequence))); if (!commandDictionary) { return diagnostics; } - const sequence = view.state.sliceDoc(); const parsed = VmlLanguage.parser.parse(sequence); diagnostics.push(...validateCommands(commandDictionary, sequence, parsed)); @@ -274,15 +274,16 @@ function unquote(s: string): string { * @param tree * @returns */ -function validateParserErrors(tree: Tree): Diagnostic[] { +function validateParserErrors(tree: Tree, sequence: string, text: Text): Diagnostic[] { const diagnostics: Diagnostic[] = []; tree.iterate({ enter: node => { if (node.name === TOKEN_ERROR && diagnostics.length < MAX_PARSER_ERRORS) { const { from, to } = node; + const line = text.lineAt(from); diagnostics.push({ from, - message: `Unexpected token`, + message: `Unexpected token: "${sequence.slice(from, to)}" [Line ${line.number}, Col ${from - line.from}]`, severity: 'error', to, });