Skip to content

Commit

Permalink
Fix issue where syntax highlighting crashes on create constraint query (
Browse files Browse the repository at this point in the history
  • Loading branch information
OskarDamkjaer authored Nov 22, 2023
1 parent 4309a0e commit a790700
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/purple-suns-applaud.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@neo4j-cypher/language-support': patch
---

Fix issue where syntax highlighting crashes on create constraint query
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,12 @@ class SyntaxHighlighter extends CypherParserListener {
};

exitLabelOrRelType = (ctx: LabelOrRelTypeContext) => {
const labelName = ctx.symbolicNameString().start;

this.addToken(labelName, CypherTokenType.label, labelName.text);
// Error recovery can insert a LabelOrRelType node with no text
// See for example CREATE CONSTRAINT FOR (node)
const labelName = ctx.symbolicNameString()?.start;
if (labelName) {
this.addToken(labelName, CypherTokenType.label, labelName.text);
}
};

exitLeftArrow = (ctx: LeftArrowContext) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2121,3 +2121,83 @@ describe('Subqueries colouring', () => {
]);
});
});

describe('CREATE colouring', () => {
test('correctly highlight broken create constraint', () => {
// is missing :Label, should not crash
expect(applySyntaxColouring('CREATE CONSTRAINT FOR (node)')).toEqual([
{
bracketInfo: undefined,
length: 6,
position: {
line: 0,
startCharacter: 0,
startOffset: 0,
},
token: 'CREATE',
tokenType: 'keyword',
},
{
bracketInfo: undefined,
length: 10,
position: {
line: 0,
startCharacter: 7,
startOffset: 7,
},
token: 'CONSTRAINT',
tokenType: 'keyword',
},
{
bracketInfo: undefined,
length: 3,
position: {
line: 0,
startCharacter: 18,
startOffset: 18,
},
token: 'FOR',
tokenType: 'keyword',
},
{
bracketInfo: {
bracketLevel: 0,
bracketType: 'parenthesis',
},
length: 1,
position: {
line: 0,
startCharacter: 22,
startOffset: 22,
},
token: '(',
tokenType: 'bracket',
},
{
bracketInfo: undefined,
length: 4,
position: {
line: 0,
startCharacter: 23,
startOffset: 23,
},
token: 'node',
tokenType: 'variable',
},
{
bracketInfo: {
bracketLevel: 0,
bracketType: 'parenthesis',
},
length: 1,
position: {
line: 0,
startCharacter: 27,
startOffset: 27,
},
token: ')',
tokenType: 'bracket',
},
]);
});
});

0 comments on commit a790700

Please sign in to comment.