Skip to content
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
21 changes: 15 additions & 6 deletions src/syntax/expr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,12 +115,21 @@ export const exprMap: CstToDocMap<AllExprNodes> = {
postfix_op_expr: (print) => print.spaced(["expr", "operator"]),
between_expr: (print) =>
print.spaced(["left", "betweenKw", "begin", "andKw", "end"]),
case_expr: (print) => [
print.spaced(["caseKw", "expr"]),
indent([hardline, join(hardline, print("clauses"))]),
hardline,
print("endKw"),
],
case_expr: (print, node) => {
if (node.clauses.length === 1) {
return group([
print.spaced(["caseKw", "expr", "clauses"]),
line,
print("endKw"),
]);
}
return [
print.spaced(["caseKw", "expr"]),
indent([hardline, join(hardline, print("clauses"))]),
hardline,
print("endKw"),
];
},
case_when: (print, node) => {
const whenThen = group([
group([print("whenKw"), indent([line, print("condition")])]),
Expand Down
35 changes: 20 additions & 15 deletions test/expr/expr.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,15 @@ describe("expr", () => {
});

describe("case", () => {
it(`formats CASE expression always on multiple lines`, async () => {
it(`formats single-WHEN CASE expression on one line`, async () => {
await test(`SELECT CASE WHEN x = 1 THEN 'A' END`);
});

it(`formats single-WHEN CASE expression with base expression on one line`, async () => {
await test(`SELECT CASE x WHEN 1 THEN 'A' END`);
});

it(`formats CASE expression with ELSE on multiple lines`, async () => {
await test(dedent`
SELECT CASE x
WHEN 1 THEN 'A'
Expand Down Expand Up @@ -192,15 +200,14 @@ describe("expr", () => {
it(`breaks long WHEN/THEN into separate lines`, async () => {
await test(
dedent`
SELECT CASE
WHEN column_name = 1 THEN
result_name
SELECT CASE WHEN column_name = 1
THEN
result_name
Comment on lines +204 to +205

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This looks a little awkward but fixing it is beyond the scope of this PR (it'd also go against the design in #80 (comment)). It would also probably require some coupling between WHEN and THEN i.e. "break THEN if WHEN conditions break", so we can keep:

WHEN 
  column_name = 1 
  AND other_column = 2
THEN
  result_name

But also get:

CASE WHEN column_name = 1 
THEN result_name

I'm not sure how to go about this, or if closing this PR is the way to go for now (which is also fine by me)

END
Comment on lines +203 to 206

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

This looks weird. It reads like SELECT ... THEN ... END.

Similar with other examples where we have a CASE with single WHEN clause that has multi-line content.

I think we really need to take a different approach to this. IMHO what we really desire is:

  • when the whole CASE ... END expression can fit on a single line, then don't break it to multiple lines.

But if somewhere inside the case-expression we have multi-line content, then we should switch to multi-line formatting of the case-expression as well.

I think a simple general fix for this all would be to eliminate this node.clauses.length === 1 special case and just use line instead if hardline in the case_expr implementation. We could instead add a different special case: saying that whenever there are more then one item in CASE we force it on multiple lines, but if it's just one, it can be on a single line, if it fits.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Ha, I commented on this before I saw your comment. I'll look into it later this week but feel free to pick up before then if you'd like :)

`,
{ printWidth: 35 },
);
});

it(`breaks multiple long WHEN/THEN clauses without blank lines between them`, async () => {
await test(
dedent`
Expand Down Expand Up @@ -232,12 +239,11 @@ describe("expr", () => {
it(`indents long multi-condition WHEN clauses`, async () => {
await test(
dedent`
SELECT CASE
WHEN
column_name = 1
AND (other_name = 2 OR other_name = 3)
THEN
result_name
SELECT CASE WHEN
column_name = 1
AND (other_name = 2 OR other_name = 3)
THEN
result_name
END
`,
{ printWidth: 50 },
Expand All @@ -247,10 +253,9 @@ describe("expr", () => {
it(`indents long multi-expression THEN clauses`, async () => {
await test(
dedent`
SELECT CASE
WHEN column_name = 1 THEN
result_name = 1
AND other_name = 2
SELECT CASE WHEN column_name = 1 THEN
result_name = 1
AND other_name = 2
END
`,
{ printWidth: 45 },
Expand Down
Loading