Skip to content

Commit

Permalink
Take CST node boundaries into account for formatting (#1629)
Browse files Browse the repository at this point in the history
  • Loading branch information
ydaveluy authored Aug 14, 2024
1 parent b2c40e3 commit 245336b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
5 changes: 5 additions & 0 deletions packages/langium/src/lsp/formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,11 @@ export abstract class AbstractFormatter implements Formatter {
if (b.hidden) {
return this.createHiddenTextEdits(a, b, formatting, context);
}
// Ignore the edit if the previous node ends after the current node starts
if (a && (a.range.end.line > b.range.start.line ||
(a.range.end.line === b.range.start.line && a.range.end.character > b.range.start.character))) {
return [];
}
const betweenRange: Range = {
start: a?.range.end ?? {
character: 0,
Expand Down
12 changes: 12 additions & 0 deletions packages/langium/test/grammar/lsp/grammar-formatter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,16 @@ describe('Grammar Formatter', () => {
});
});

test('Formats parser rule definitions with alternatives', async () => {
await formatting({
before: expandToString`
Type:
DataType | Entity;
`,
after: expandToString`
Type:
DataType | Entity;
`
});
});
});

0 comments on commit 245336b

Please sign in to comment.