Skip to content

Commit f5553ac

Browse files
tomasnybergsimonthuresson
authored andcommitted
Removes reordering of ON CREATE/MATCH clauses in formatter (neo4j#399)
1 parent a112798 commit f5553ac

File tree

5 files changed

+15
-57
lines changed

5 files changed

+15
-57
lines changed

packages/language-support/src/formatting/formatting.ts

+9-9
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ import {
6565
CommentChunk,
6666
findTargetToken,
6767
getParseTreeAndTokens,
68-
handleMergeClause,
6968
isComment,
7069
RegularChunk,
7170
wantsToBeConcatenated,
@@ -1167,15 +1166,16 @@ export class TreePrintVisitor extends CypherCmdParserVisitor<void> {
11671166
this.endGroup(invocationGrp);
11681167
};
11691168

1170-
// Handled separately because we want ON CREATE before ON MATCH
11711169
visitMergeClause = (ctx: MergeClauseContext) => {
1172-
handleMergeClause(
1173-
ctx,
1174-
(node) => this.visit(node),
1175-
() => this.startGroupAlsoOnComment(),
1176-
(id) => this.endGroup(id),
1177-
() => this.avoidBreakBetween(),
1178-
);
1170+
this.visit(ctx.MERGE());
1171+
this.avoidBreakBetween();
1172+
const patternGrp = this.startGroupAlsoOnComment();
1173+
this.visit(ctx.pattern());
1174+
this.endGroup(patternGrp);
1175+
const n = ctx.mergeAction_list().length;
1176+
for (let i = 0; i < n; i++) {
1177+
this.visit(ctx.mergeAction(i));
1178+
}
11791179
};
11801180

11811181
// Handled separately because it wants indentation

packages/language-support/src/formatting/formattingHelpers.ts

-37
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,13 @@ import {
33
CommonToken,
44
CommonTokenStream,
55
ErrorListener as ANTLRErrorListener,
6-
ParseTree,
76
Recognizer,
87
TerminalNode,
98
Token,
109
} from 'antlr4';
1110
import { default as CypherCmdLexer } from '../generated-parser/CypherCmdLexer';
1211
import CypherCmdParser, {
1312
EscapedSymbolicNameStringContext,
14-
MergeClauseContext,
1513
UnescapedSymbolicNameString_Context,
1614
} from '../generated-parser/CypherCmdParser';
1715
import { lexerKeywords } from '../lexerSymbols';
@@ -76,41 +74,6 @@ const traillingCharacters = [
7674
CypherCmdLexer.RBRACKET,
7775
];
7876

79-
// TODO: This function should probably not exist; we're not really fans of
80-
// shuffling around the AST like we're doing right now...
81-
export function handleMergeClause(
82-
ctx: MergeClauseContext,
83-
visit: (node: ParseTree) => void,
84-
startGroup?: () => number,
85-
endGroup?: (id: number) => void,
86-
avoidBreakBetween?: () => void,
87-
) {
88-
visit(ctx.MERGE());
89-
avoidBreakBetween?.();
90-
let patternGrp: number;
91-
if (startGroup) {
92-
patternGrp = startGroup();
93-
}
94-
visit(ctx.pattern());
95-
if (endGroup) {
96-
endGroup(patternGrp);
97-
}
98-
const mergeActions = ctx
99-
.mergeAction_list()
100-
.map((action, index) => ({ action, index }));
101-
mergeActions.sort((a, b) => {
102-
if (a.action.CREATE() && b.action.MATCH()) {
103-
return -1;
104-
} else if (a.action.MATCH() && b.action.CREATE()) {
105-
return 1;
106-
}
107-
return a.index - b.index;
108-
});
109-
mergeActions.forEach(({ action }) => {
110-
visit(action);
111-
});
112-
}
113-
11477
export function wantsToBeUpperCase(node: TerminalNode): boolean {
11578
return isKeywordTerminal(node);
11679
}

packages/language-support/src/formatting/standardizer.ts

+2-9
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
import { TerminalNode } from 'antlr4';
2-
import {
3-
MergeClauseContext,
4-
StatementsOrCommandsContext,
5-
} from '../generated-parser/CypherCmdParser';
2+
import { StatementsOrCommandsContext } from '../generated-parser/CypherCmdParser';
63
import CypherCmdParserVisitor from '../generated-parser/CypherCmdParserVisitor';
7-
import { getParseTreeAndTokens, handleMergeClause } from './formattingHelpers';
4+
import { getParseTreeAndTokens } from './formattingHelpers';
85

96
class StandardizingVisitor extends CypherCmdParserVisitor<void> {
107
buffer = [];
@@ -14,10 +11,6 @@ class StandardizingVisitor extends CypherCmdParserVisitor<void> {
1411
return this.buffer.join('');
1512
};
1613

17-
visitMergeClause = (ctx: MergeClauseContext) => {
18-
handleMergeClause(ctx, (node) => this.visit(node));
19-
};
20-
2114
visitTerminal = (node: TerminalNode) => {
2215
this.buffer.push(node.getText().toLowerCase());
2316
this.buffer.push(' ');

packages/language-support/src/tests/formatting/comments.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ RETURN a.prop // Return the 'prop' of 'a'
2323
MERGE (n)
2424
ON CREATE SET n.prop = 0 // Ensure 'n' exists and initialize 'prop' to 0 if created
2525
MERGE (a:A)-[:T]->(b:B) // Create or match a relationship from 'a:A' to 'b:B'
26-
ON CREATE SET a.name = 'me' // If 'a' is created, set its 'name' to 'me'
2726
ON MATCH SET b.name = 'you' // If 'b' already exists, set its 'name' to 'you'
27+
ON CREATE SET a.name = 'me' // If 'a' is created, set its 'name' to 'me'
2828
RETURN a.prop // Return the 'prop' of 'a'`.trim();
2929
verifyFormatting(inlinecomments, expected);
3030
});

packages/language-support/src/tests/formatting/styleguide.test.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { verifyFormatting } from './testutil';
22

33
describe('styleguide examples', () => {
4+
// NOTE: We do not swap the order of ON MATCH and ON CREATE since
5+
// we feel that it falls outside the responsbilities of a formatter.
46
test('on match indentation example', () => {
57
const query = `MERGE (n) ON CREATE SET n.prop = 0
68
MERGE (a:A)-[:T]->(b:B)
@@ -10,8 +12,8 @@ RETURN a.prop`;
1012
const expected = `MERGE (n)
1113
ON CREATE SET n.prop = 0
1214
MERGE (a:A)-[:T]->(b:B)
13-
ON CREATE SET a.name = 'me'
1415
ON MATCH SET b.name = 'you'
16+
ON CREATE SET a.name = 'me'
1517
RETURN a.prop`;
1618
verifyFormatting(query, expected);
1719
});

0 commit comments

Comments
 (0)