Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes bug splitting the statements into commands #346

Merged
merged 5 commits into from
Feb 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/shiny-parents-cry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@neo4j-cypher/language-support': patch
---

Solves bug splitting commands which was breaking for queries just containing comments
5 changes: 3 additions & 2 deletions packages/language-support/src/parserWrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -610,8 +610,9 @@ function parseToCommand(

return { type: 'parse-error', start, stop };
}
const statement = inputstream.getText(start.start, stop.stop);
return { type: 'cypher', statement, start: start, stop: stop };
const stopToken = stop ?? tokens.at(-1);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Double checking my understanding - I would explain like:
"When ANTLR can't finish the parsing (but can tokenize) because a comment/string/escaped sequence is unfinished we get here with stop===null"

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem if we just a comment is that's not passed to the parser, so the CST is empty, therefore because the parser expects something, it errors and cannot find a stop. Strings and backticked elements are not on the hidden channel, so they are passed to the parser

const statement = inputstream.getText(start.start, stopToken.stop);
return { type: 'cypher', statement, start: start, stop: stopToken };
}
return { type: 'parse-error', start: stmts.start, stop: stmts.stop };
}
Expand Down
72 changes: 72 additions & 0 deletions packages/language-support/src/tests/syntaxColouring/misc.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,4 +232,76 @@ describe('Unfinished tokens', () => {
},
]);
});

test('Correctly colours multiline comments', () => {
const query = `/* something
foo

bar */`;

expect(applySyntaxColouring(query)).toEqual([
{
bracketInfo: undefined,
length: 12,
position: {
line: 0,
startCharacter: 0,
startOffset: 0,
},
token: '/* something',
tokenType: 'comment',
},
{
bracketInfo: undefined,
length: 7,
position: {
line: 1,
startCharacter: 0,
startOffset: 13,
},
token: ' foo',
tokenType: 'comment',
},
{
bracketInfo: undefined,
length: 4,
position: {
line: 2,
startCharacter: 0,
startOffset: 21,
},
token: ' ',
tokenType: 'comment',
},
{
bracketInfo: undefined,
length: 10,
position: {
line: 3,
startCharacter: 0,
startOffset: 26,
},
token: ' bar */',
tokenType: 'comment',
},
]);
});

test('Correctly colours single line comments', () => {
const query = `// single line comment`;

expect(applySyntaxColouring(query)).toEqual([
{
bracketInfo: undefined,
length: 22,
position: {
line: 0,
startCharacter: 0,
startOffset: 0,
},
token: '// single line comment',
tokenType: 'comment',
},
]);
});
});