Skip to content

Commit d08dc15

Browse files
authored
Fixes postfix edgecases in formatter (#490)
1 parent d53f189 commit d08dc15

File tree

3 files changed

+76
-16
lines changed

3 files changed

+76
-16
lines changed

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1639,18 +1639,29 @@ export class TreePrintVisitor extends CypherCmdParserVisitor<void> {
16391639
};
16401640

16411641
visitIndexPostfix = (ctx: IndexPostfixContext) => {
1642+
this.avoidSpaceBetween();
1643+
this.avoidBreakBetween();
1644+
const indexPostfixGrp = this.startGroup();
16421645
this._visit(ctx.LBRACKET());
1646+
this.avoidBreakBetween();
16431647
this._visit(ctx.expression());
1644-
this.avoidSpaceBetween();
16451648
this._visit(ctx.RBRACKET());
1649+
this.endGroup(indexPostfixGrp);
16461650
};
16471651

16481652
visitRangePostfix = (ctx: RangePostfixContext) => {
1653+
this.avoidSpaceBetween();
1654+
this.avoidBreakBetween();
1655+
const rangePostfixGrp = this.startGroup();
16491656
this._visit(ctx.LBRACKET());
16501657
if (ctx._fromExp) {
16511658
this._visit(ctx.expression(0));
16521659
}
1653-
this._visit(ctx.DOTDOT());
1660+
if (ctx.DOTDOT()) {
1661+
this.avoidSpaceBetween();
1662+
this._visit(ctx.DOTDOT());
1663+
this.avoidSpaceBetween();
1664+
}
16541665
if (ctx._toExp) {
16551666
if (ctx._fromExp) {
16561667
this._visit(ctx.expression(1));
@@ -1660,6 +1671,7 @@ export class TreePrintVisitor extends CypherCmdParserVisitor<void> {
16601671
}
16611672
this.avoidSpaceBetween();
16621673
this._visit(ctx.RBRACKET());
1674+
this.endGroup(rangePostfixGrp);
16631675
};
16641676

16651677
// Handled separately because it contains subclauses (and thus indentation rules)

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

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ RETURN
189189
description:
190190
CASE
191191
WHEN p.Description IS NULL OR size(p.Description) = "TxWb1jb3" THEN []
192-
ELSE p.Description[.. "VM6fSkTL"]
192+
ELSE p.Description[.."VM6fSkTL"]
193193
END
194194
} AS node,
195195
r,
@@ -198,7 +198,7 @@ RETURN
198198
description:
199199
CASE
200200
WHEN b.Description IS NULL OR size(b.Description) = "wnBMZdOC" THEN []
201-
ELSE b.Description[.. "NHIwucAy"]
201+
ELSE b.Description[.."NHIwucAy"]
202202
END
203203
} AS endNode;`.trimStart();
204204
verifyFormatting(query, expected);
@@ -219,7 +219,7 @@ RETURN
219219
description:
220220
CASE p.age
221221
WHEN p.Description IS NULL OR size(p.Description) = "TxWb1jb3" THEN []
222-
ELSE p.Description[.. "VM6fSkTL"]
222+
ELSE p.Description[.."VM6fSkTL"]
223223
END
224224
} AS node,
225225
r,
@@ -228,7 +228,7 @@ RETURN
228228
description:
229229
CASE p.age
230230
WHEN b.Description IS NULL OR size(b.Description) = "wnBMZdOC" THEN []
231-
ELSE b.Description[.. "NHIwucAy"]
231+
ELSE b.Description[.."NHIwucAy"]
232232
END
233233
} AS endNode;`;
234234
verifyFormatting(query, expected);
@@ -1115,4 +1115,52 @@ WHERE
11151115
(aaaaaa)<-[:aaaaaa]-(:aaaaaa)-[:aaaaaa]->(aaaaaa) //IKCKaFRvOLUsiwpZZgdhsXrNwWHZHLoUkraBtkoUQrFiLEW`;
11161116
verifyFormatting(query, expected);
11171117
});
1118+
1119+
test('postfix after a collect', () => {
1120+
const query = `WITH DISTINCT
1121+
COLLECT {
1122+
MATCH (p)-[:XYZ_ABCD12]->(q)
1123+
RETURN q.blnk
1124+
}
1125+
[
1126+
"Ab12Cd34"] AS sdflkjl52_2462,
1127+
qwer1["Bc34Df56"] AS ftgy3_hjkl_pqr23,
1128+
qwer1["Gh78Ij90"] AS zxcv5_bnmd_qwe45,
1129+
qwer1["Kl12Mn34"] AS rtyu6_asdf_ghj67,
1130+
qwer1
1131+
1132+
RETURN *`;
1133+
const expected = `WITH DISTINCT
1134+
COLLECT {
1135+
MATCH (p)-[:XYZ_ABCD12]->(q)
1136+
RETURN q.blnk
1137+
}["Ab12Cd34"] AS sdflkjl52_2462,
1138+
qwer1["Bc34Df56"] AS ftgy3_hjkl_pqr23,
1139+
qwer1["Gh78Ij90"] AS zxcv5_bnmd_qwe45,
1140+
qwer1["Kl12Mn34"] AS rtyu6_asdf_ghj67,
1141+
qwer1
1142+
1143+
RETURN *`;
1144+
verifyFormatting(query, expected);
1145+
});
1146+
1147+
test('range postfix after a collect', () => {
1148+
const query = `MATCH (n)
1149+
RETURN
1150+
COLLECT {
1151+
MATCH (n)
1152+
RETURN n
1153+
}
1154+
[
1155+
1
1156+
..
1157+
10]`;
1158+
const expected = `MATCH (n)
1159+
RETURN
1160+
COLLECT {
1161+
MATCH (n)
1162+
RETURN n
1163+
}[1..10]`;
1164+
verifyFormatting(query, expected);
1165+
});
11181166
});

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -515,7 +515,7 @@ RETURN [stop in n[..-1] | stop.name] AS stops`;
515515
MATCH
516516
SHORTEST 1
517517
(:Station {name: 'Hartlebury'}) (()--(n))+(:Station {name: 'Cheltenham Spa'})
518-
RETURN [stop IN n[.. -1] | stop.name] AS stops`.trimStart();
518+
RETURN [stop IN n[..-1] | stop.name] AS stops`.trimStart();
519519
verifyFormatting(query, expected);
520520
});
521521

@@ -530,8 +530,8 @@ MATCH
530530
((:Station {name: 'Hartlebury'})
531531
(()--(n:Station))+
532532
(:Station {name: 'Cheltenham Spa'})
533-
WHERE none(stop IN n[.. -1] WHERE stop.name = 'Bromsgrove'))
534-
RETURN [stop IN n[.. -1] | stop.name] AS stops`.trimStart();
533+
WHERE none(stop IN n[..-1] WHERE stop.name = 'Bromsgrove'))
534+
RETURN [stop IN n[..-1] | stop.name] AS stops`.trimStart();
535535
verifyFormatting(query, expected);
536536
});
537537

@@ -550,10 +550,10 @@ MATCH
550550
(:Station {name: 'Cheltenham Spa'})
551551
WHERE
552552
none(
553-
stop IN n[.. -1]
553+
stop IN n[..-1]
554554
WHERE stop.name = 'Bromsgroveeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee'
555555
))
556-
RETURN [stop IN n[.. -1] | stop.name] AS stops`.trimStart();
556+
RETURN [stop IN n[..-1] | stop.name] AS stops`.trimStart();
557557
verifyFormatting(query, expected);
558558
});
559559

@@ -569,8 +569,8 @@ MATCH
569569
((:Station {name: 'Thisisanabsurdlylongnametomakeitawkward'})
570570
(()--(n:Station))+
571571
(:Station {name: 'Cheltenham Spa'})
572-
WHERE none(stop IN n[.. -1] WHERE stop.name = 'Bromsgrove'))
573-
RETURN [stop IN n[.. -1] | stop.name] AS stops`.trimStart();
572+
WHERE none(stop IN n[..-1] WHERE stop.name = 'Bromsgrove'))
573+
RETURN [stop IN n[..-1] | stop.name] AS stops`.trimStart();
574574
verifyFormatting(query, expected);
575575
});
576576

@@ -1127,14 +1127,14 @@ LIMIT "g68S0y7w";`;
11271127
WITH n, collect(n) AS duplicates
11281128
WHERE size(duplicates) > "zuEVCUOg"
11291129
WITH
1130-
duplicates["2x5H4FCD"] AS keepNode, duplicates["oMXseK4u" ..] AS deleteNodes
1130+
duplicates["2x5HHHH4FCD"] AS keepNode, duplicates["oMXseK4u" ..] AS deleteNodes
11311131
RETURN deleteNodes`;
11321132
const expected = `MATCH (n:Course {id: "fxmrRAfg"})-[r*]->(b)
11331133
WITH n, collect(n) AS duplicates
11341134
WHERE size(duplicates) > "zuEVCUOg"
11351135
WITH
1136-
duplicates["2x5H4FCD"] AS keepNode,
1137-
duplicates["oMXseK4u" ..] AS deleteNodes
1136+
duplicates["2x5HHHH4FCD"] AS keepNode,
1137+
duplicates["oMXseK4u"..] AS deleteNodes
11381138
RETURN deleteNodes`;
11391139
verifyFormatting(query, expected);
11401140
});

0 commit comments

Comments
 (0)