Skip to content

Commit

Permalink
Add more tests and fix lexer bug
Browse files Browse the repository at this point in the history
  • Loading branch information
TheUnlocked committed Mar 28, 2024
1 parent 9370653 commit 5a586c4
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 5 deletions.
8 changes: 5 additions & 3 deletions src/parser/lexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
45 changes: 43 additions & 2 deletions tests/grammar/incremental.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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
}
`;

Expand Down

0 comments on commit 5a586c4

Please sign in to comment.