-
Notifications
You must be signed in to change notification settings - Fork 15
Apply various formatting fixes #80
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f333c58
Match WHEN/THEN/ELSE clause formatting with procedural style
joelmukuthu d830774
Break long COMMENT ON clauses into separate lines
joelmukuthu b1a2322
Break long CREATE INDEX clauses into separate lines
joelmukuthu c9069a4
Break long binary expressions into separate lines
joelmukuthu 868b3f9
Avoid line-break between empty parenthesis
joelmukuthu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,6 +20,27 @@ describe("expr", () => { | |
| `); | ||
| }); | ||
|
|
||
| it(`keeps short binary expressions on one line`, async () => { | ||
| await test(dedent` | ||
| SELECT * | ||
| FROM foo | ||
| WHERE bar = short_func() | ||
| `); | ||
| }); | ||
|
|
||
| it(`breaks long binary expressions into multiple lines with indentation`, async () => { | ||
| await test( | ||
| dedent` | ||
| SELECT * | ||
| FROM foo | ||
| WHERE | ||
| bar = | ||
| my_func() | ||
| `, | ||
| { printWidth: 15 }, | ||
| ); | ||
| }); | ||
|
|
||
| it(`formats IN expressions`, async () => { | ||
| await test(`SELECT col1 IN (1, 2, 3), col2 NOT IN (4, 5, 6)`); | ||
| }); | ||
|
|
@@ -144,8 +165,10 @@ describe("expr", () => { | |
| await test(dedent` | ||
| SELECT | ||
| CASE x | ||
| WHEN 1 THEN 'A' | ||
| ELSE 'B' | ||
| WHEN 1 THEN | ||
| 'A' | ||
| ELSE | ||
| 'B' | ||
| END | ||
| `); | ||
| }); | ||
|
|
@@ -154,9 +177,12 @@ describe("expr", () => { | |
| await test(dedent` | ||
| SELECT | ||
| CASE status | ||
| WHEN 1 THEN 'good' | ||
| WHEN 2 THEN 'bad' | ||
| ELSE 'unknown' | ||
| WHEN 1 THEN | ||
| 'good' | ||
| WHEN 2 THEN | ||
| 'bad' | ||
| ELSE | ||
| 'unknown' | ||
| END | ||
| `); | ||
| }); | ||
|
|
@@ -165,12 +191,75 @@ describe("expr", () => { | |
| await test(dedent` | ||
| SELECT | ||
| CASE | ||
| WHEN status = 1 THEN 'good' | ||
| WHEN status = 2 THEN 'bad' | ||
| ELSE 'unknown' | ||
| WHEN status = 1 THEN | ||
| 'good' | ||
| WHEN status = 2 THEN | ||
| 'bad' | ||
| ELSE | ||
| 'unknown' | ||
| END | ||
| `); | ||
| }); | ||
|
|
||
| it(`breaks long WHEN/THEN into separate lines`, async () => { | ||
| await test( | ||
| dedent` | ||
| SELECT | ||
| CASE | ||
| WHEN column_name = 1 THEN | ||
| result_name | ||
| END | ||
| `, | ||
| { printWidth: 40 }, | ||
| ); | ||
| }); | ||
|
|
||
| it(`breaks multiple long WHEN/THEN clauses without blank lines between them`, async () => { | ||
| await test( | ||
| dedent` | ||
| SELECT | ||
| CASE | ||
| WHEN column_name = 1 THEN | ||
| result_name | ||
| WHEN column_name = 2 THEN | ||
| other_result | ||
| ELSE | ||
| foo | ||
| END | ||
| `, | ||
| { printWidth: 40 }, | ||
| ); | ||
| }); | ||
|
Comment on lines
+183
to
+211
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think these tests have become obsolete now that we always break. Best to remove |
||
|
|
||
| it(`indents multi-condition WHEN clauses and keeps ORs parenthesized`, async () => { | ||
| await test( | ||
| dedent` | ||
| SELECT | ||
| CASE | ||
| WHEN | ||
| column_name = 1 | ||
| AND (other_name = 2 OR other_name = 3) | ||
| THEN | ||
| result_name | ||
| END | ||
| `, | ||
| { printWidth: 50 }, | ||
| ); | ||
| }); | ||
|
|
||
| it(`indents multi-expression THEN clauses and keeps ORs parenthesized`, async () => { | ||
| await test( | ||
| dedent` | ||
| SELECT | ||
| CASE | ||
| WHEN column_name = 1 THEN | ||
| result_name = 1 | ||
| AND (other_name = 2 OR other_name = 3) | ||
| END | ||
| `, | ||
| { printWidth: 45 }, | ||
| ); | ||
| }); | ||
| }); | ||
|
|
||
| it(`formats quantifier expressions`, async () => { | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I gave you the wrong idea when I said we should format the
caseexpression the same as thecasestatement in procedural SQL. What I meant was, that we should format them similarly when they are so long that they don't fit on a single line. That is, I'd still expect to format the above as:Only when the expression doesn't fit on a single line should be break the
WHEN..THENorELSEblock to multiple lines like so:or when the condition part is long:
CASE x WHEN some_long_expression_in_here THEN 'A' ELSE 'B' ENDor extra long:
CASE x WHEN some_extra_long_expression_in_here THEN 'A' ELSE 'B' ENDThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was what I understood at first, but I changed the implementation because of the desired
ELSEformatting. With the current code setup, theELSEclause is formatted separately/independently, so a "short" ELSE gets formatted as follows (regardless ofTHENs formatting):Alternatively, the current implementation could be changed to couple WHEN/THEN/ELSE formatting, so we can know to break the ELSE statement if the WHEN/THEN statements also break.
Or alternatively, always break the statements, which also happens to look like
if..elsestatements (with braces). So when I got to this point I thought this what you meant all along :)Anyway to summarise, I think the options are:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I'm leaning towards option 2. But I also don't really know how to easily achieve that. Although I'm not 100% sure we want that. The bad thing with this variant is that making one
WHEN-block longer would cause a large reindent for all of them. That might or might not be what one desires. Like, if one has a longCASEexpression with lots of small WHEN-THEN blocks, and then one that's a bit longer, it could be annoying to have everything split to multiple lines because of that one block. On the other hand, it can be annoying to have a mix of one-line and two-line WHEN-THEN blocks.But option 1 is definitely the next best thing. And maybe it's even the better variant out of the two. Let's go with that and not try to fight too much with the general indentation approach of Prettier.