Skip to content

Keep single-WHEN CASE expressions on one line - #92

Open
joelmukuthu wants to merge 1 commit into
nene:masterfrom
joelmukuthu:fix/single-when-case
Open

joelmukuthu wants to merge 1 commit into
nene:masterfrom
joelmukuthu:fix/single-when-case

Conversation

@joelmukuthu

Copy link
Copy Markdown
Contributor

Ref #91

@nene nene left a comment

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.

Thanks. But looking at the resulting formatting, I think a different approach is needed. See below.

Comment thread test/expr/expr.test.ts
Comment on lines +203 to 206
SELECT CASE WHEN column_name = 1
THEN
result_name
END

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 :)

Comment thread test/expr/expr.test.ts
Comment on lines +204 to +205
THEN
result_name

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants