Skip to content

Commit 245336b

Browse files
authored
Take CST node boundaries into account for formatting (#1629)
1 parent b2c40e3 commit 245336b

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

packages/langium/src/lsp/formatter.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,11 @@ export abstract class AbstractFormatter implements Formatter {
359359
if (b.hidden) {
360360
return this.createHiddenTextEdits(a, b, formatting, context);
361361
}
362+
// Ignore the edit if the previous node ends after the current node starts
363+
if (a && (a.range.end.line > b.range.start.line ||
364+
(a.range.end.line === b.range.start.line && a.range.end.character > b.range.start.character))) {
365+
return [];
366+
}
362367
const betweenRange: Range = {
363368
start: a?.range.end ?? {
364369
character: 0,

packages/langium/test/grammar/lsp/grammar-formatter.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,16 @@ describe('Grammar Formatter', () => {
6262
});
6363
});
6464

65+
test('Formats parser rule definitions with alternatives', async () => {
66+
await formatting({
67+
before: expandToString`
68+
Type:
69+
DataType | Entity;
70+
`,
71+
after: expandToString`
72+
Type:
73+
DataType | Entity;
74+
`
75+
});
76+
});
6577
});

0 commit comments

Comments
 (0)