Skip to content

Commit fe61103

Browse files
authored
fix: attach orphan comments to root AST node when query has no commands (#51)
## Summary This fix prevents showing suggestions when editor starts with a comment <img width="732" height="277" alt="image" src="https://github.com/user-attachments/assets/e10ed9e4-d812-42a1-8a76-f22463cfcc9a" /> https://github.com/user-attachments/assets/ef8ef0f6-3348-442e-be05-d8e5dc3644cb
1 parent a158760 commit fe61103

2 files changed

Lines changed: 34 additions & 0 deletions

File tree

src/parser/__tests__/comments.test.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1051,6 +1051,38 @@ FROM a
10511051
});
10521052
});
10531053

1054+
describe('comment-only queries (no commands)', () => {
1055+
it('attaches a single-line comment to the root query node', () => {
1056+
const { root } = parse('// hello', { withFormatting: true });
1057+
1058+
expect(root.commands).toHaveLength(0);
1059+
expect(root.formatting).toMatchObject({
1060+
top: [
1061+
{
1062+
type: 'comment',
1063+
subtype: 'single-line',
1064+
text: ' hello',
1065+
},
1066+
],
1067+
});
1068+
});
1069+
1070+
it('attaches a multi-line comment to the root query node', () => {
1071+
const { root } = parse('/* hello */', { withFormatting: true });
1072+
1073+
expect(root.commands).toHaveLength(0);
1074+
expect(root.formatting).toMatchObject({
1075+
top: [
1076+
{
1077+
type: 'comment',
1078+
subtype: 'multi-line',
1079+
text: ' hello ',
1080+
},
1081+
],
1082+
});
1083+
});
1084+
});
1085+
10541086
describe('many comments', () => {
10551087
test('can attach all possible inline comments in basic RERANK command', () => {
10561088
const src = `

src/parser/core/decorations.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,8 @@ const attachCommentDecoration = (
197197
const lastCommand = commands[commands.length - 1];
198198
if (lastCommand) {
199199
attachBottomComment(lastCommand, comment.node);
200+
} else {
201+
attachTopComment(ast, comment.node);
200202
}
201203
return;
202204
}

0 commit comments

Comments
 (0)