From 5a586c46e231e2bb2d7a4e6b1370197af3bd3096 Mon Sep 17 00:00:00 2001 From: Trevor Paley <10186337+TheUnlocked@users.noreply.github.com> Date: Wed, 27 Mar 2024 17:12:23 -0700 Subject: [PATCH] Add more tests and fix lexer bug --- src/parser/lexer.ts | 8 +++--- tests/grammar/incremental.spec.ts | 45 +++++++++++++++++++++++++++++-- 2 files changed, 48 insertions(+), 5 deletions(-) diff --git a/src/parser/lexer.ts b/src/parser/lexer.ts index d1e1636..392e823 100644 --- a/src/parser/lexer.ts +++ b/src/parser/lexer.ts @@ -644,10 +644,12 @@ export class StringLexer extends TrackedDiagnosticsMixin implements ILexer { const lineDelta = linesAdded - linesRemoved; /** The column directly after the inserted content */ const endColumn = - linesAdded === 0 + linesAdded !== 0 // If the edit doesn't include any newlines, add the byte delta to the starting column - ? lastToken.range.end.col + byteDelta - : insert.length - insert.lastIndexOf('\n'); + ? insert.length - insert.lastIndexOf('\n') + : linesRemoved !== 0 + ? firstToken.range.start.col + insert.length + : lastToken.range.end.col + byteDelta; // Push onto the edit change so that tokens can update their position this.edits = this.edits.push({ diff --git a/tests/grammar/incremental.spec.ts b/tests/grammar/incremental.spec.ts index 53505de..38bb121 100644 --- a/tests/grammar/incremental.spec.ts +++ b/tests/grammar/incremental.spec.ts @@ -16,6 +16,7 @@ function testIncremental(title: string) { const p1 = new Parser(); p1.setDiagnostics(d1.getReporter('mike')); p1.loadSource(original); + p1.parse(); d1.clear(); p1.editSource(part1.length, remove.length, insert); @@ -149,9 +150,49 @@ export default () => describe('incremental', () => { } `; - testIncremental('shift diagnostic forwards')` + testIncremental('make change in the presence of parser diagnostic earlier in file')` on foo() { - let x = ${['1', '15']}; 10e; + x + } + + on bar() { + let x = ${['1', '15']}; + } + `; + + testIncremental('make change in the presence of parser diagnostic later in file')` + on foo() { + let x = ${['1', '15']}; + } + + on bar() { + x + } + `; + + testIncremental('make change which would shift lexer diagnostic to another column')` + on foo() { + let x = ${['1', '15']}; @ + } + `; + + testIncremental('make change which would shift lexer diagnostic to another line')` + on foo() { + let x = ${['1', `" + "`]}; @ + } + `; + + testIncremental('make change which would shift parser diagnostic to another column')` + on foo() { + let x = ${['1', '15']}; x123 + } + `; + + testIncremental('make change which would shift parser diagnostic to another line')` + on foo() { + let x = ${['1', ` + 1`]}; x123 } `;