Skip to content

Commit e54f2b1

Browse files
authored
Improves handling of SKIP in formatter (#477)
1 parent c149b33 commit e54f2b1

File tree

3 files changed

+60
-4
lines changed

3 files changed

+60
-4
lines changed

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

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ import {
9393
SetPropContext,
9494
SetPropsContext,
9595
ShowCommandYieldContext,
96+
SkipContext,
9697
StatementsOrCommandsContext,
9798
SubqueryClauseContext,
9899
TrimFunctionContext,
@@ -847,6 +848,16 @@ export class TreePrintVisitor extends CypherCmdParserVisitor<void> {
847848
this.endGroup(deleteClauseGrp);
848849
};
849850

851+
visitSkip = (ctx: SkipContext) => {
852+
const skipGrp = this.startGroup();
853+
this._visit(ctx.OFFSET());
854+
this._visit(ctx.SKIPROWS());
855+
const skipIndent = this.addIndentation();
856+
this._visit(ctx.expression());
857+
this.removeIndentation(skipIndent);
858+
this.endGroup(skipGrp);
859+
};
860+
850861
// Separate the return items from the return body so that ORDER BY and LIMIT can be
851862
// excluded from the whole RETURN group.
852863
// Used by returnClause and withClause
@@ -863,10 +874,9 @@ export class TreePrintVisitor extends CypherCmdParserVisitor<void> {
863874
_visitReturnBodyNotItems = (ctx: ReturnBodyContext) => {
864875
if (ctx.orderBy() || ctx.skip()) {
865876
this.breakLine();
866-
const orderSkipGrp = this.startGroup();
867877
this._visit(ctx.orderBy());
878+
this.breakLine();
868879
this._visit(ctx.skip());
869-
this.endGroup(orderSkipGrp);
870880
}
871881
this._visit(ctx.limit());
872882
};
@@ -888,11 +898,10 @@ export class TreePrintVisitor extends CypherCmdParserVisitor<void> {
888898
this._visit(ctx.returnItems());
889899
this.removeIndentation(returnItemsIndent);
890900
if (ctx.orderBy() || ctx.skip()) {
891-
const orderSkipGrp = this.startGroup();
892901
this.breakLine();
893902
this._visit(ctx.orderBy());
903+
this.breakLine();
894904
this._visit(ctx.skip());
895-
this.endGroup(orderSkipGrp);
896905
}
897906
this._visit(ctx.limit());
898907
};

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1225,6 +1225,38 @@ RETURN count(*)`.trimStart();
12251225
const expected = query;
12261226
verifyFormatting(query, expected);
12271227
});
1228+
1229+
test('should keep the ORDER BY parts together', () => {
1230+
// Right now this works because ORDER BY doesn't have a group at all.
1231+
// This should be fine since we wouldn't want to break it anyway, but might break if we decide
1232+
// to start e.g. putting ASC/DESCENDING on a new line and indent them.
1233+
const query = `MATCH (n)
1234+
WITH n
1235+
ORDER BY n.priiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiice ASC
1236+
LIMIT 100
1237+
RETURN n`;
1238+
const expected = query;
1239+
verifyFormatting(query, expected);
1240+
});
1241+
1242+
test('SKIP that needs to break should handle it gracefully', () => {
1243+
const query = `MATCH (n)
1244+
WITH n
1245+
SKIP CASE WHEN n.department = 'Engineering' THEN 'Engineer' WHEN n.department = 'Sales' THEN 'Sales Leader' ELSE 'Manager' END
1246+
LIMIT 100
1247+
RETURN n`;
1248+
const expected = `MATCH (n)
1249+
WITH n
1250+
SKIP
1251+
CASE
1252+
WHEN n.department = 'Engineering' THEN 'Engineer'
1253+
WHEN n.department = 'Sales' THEN 'Sales Leader'
1254+
ELSE 'Manager'
1255+
END
1256+
LIMIT 100
1257+
RETURN n`;
1258+
verifyFormatting(query, expected);
1259+
});
12281260
});
12291261

12301262
describe('tests for respcecting user line breaks', () => {

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,4 +346,19 @@ CALL (c, c2, trxs, avgTrx, totalSum) {
346346
} IN 10 CONCURRENT TRANSACTIONS OF 25 ROWS;`;
347347
verifyFormatting(query, expected);
348348
});
349+
350+
test('should put SKIP on a newline, similarly to LIMIT and ORDER BY', () => {
351+
const query = `MATCH (n)
352+
WITH n
353+
ORDER BY n.price ASC SKIP 10
354+
LIMIT 100
355+
RETURN n`;
356+
const expected = `MATCH (n)
357+
WITH n
358+
ORDER BY n.price ASC
359+
SKIP 10
360+
LIMIT 100
361+
RETURN n`;
362+
verifyFormatting(query, expected);
363+
});
349364
});

0 commit comments

Comments
 (0)