-
-
Notifications
You must be signed in to change notification settings - Fork 17
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
Line order of comments and code is not preserved #348
Comments
Thanks for the report. I actually think this is two separate issues. I'll address leading commas in my next comment For the column comments, I agree this is confusing, and not very desirable. I'm not sure I like the alternatives more, though? Alternatives being:
Of these options, (1) seems best to me, but that can lead to some undesirable outcomes, also. FWIW, Black's behavior here is weird, and hard to predict, but for short code and inline comments it'll one-line everything. Generally, it's very conservative around comments: $ black -
a = (
1 # one
+ 2 # two
)
a = 1 + 2 # one # two
reformatted -
All done! ✨ 🍰 ✨
1 file reformatted.
$ black -
a = ( # a
1 # one
+ 2 # two
)
a = 1 + 2 # a # one # two
reformatted -
All done! ✨ 🍰 ✨
1 file reformatted.
$ black -
a = (
# a
1 # one
+ 2 # two
)
a = (
# a
1 # one
+ 2 # two
)
reformatted -
All done! ✨ 🍰 ✨
1 file reformatted.
$ black -
a = ( # a can have a longer comment that might take up a whole line
1 # one short comment
+ 2 # another comment
)
a = ( # a can have a longer comment that might take up a whole line
1 + 2 # one short comment # another comment
)
reformatted -
All done! ✨ 🍰 ✨
1 file reformatted.
$ black -
a = (
1 # one longer comment that will prevent this whole thing from being one-lined
+ 2 # two
)
a = (
1 # one longer comment that will prevent this whole thing from being one-lined
+ 2 # two
)
reformatted -
All done! ✨ 🍰 ✨
1 file reformatted.
$ black -
a = (
# a longer comment that is on its own line, inside the parens
1 # one
+ 2 # two
)
a = (
# a longer comment that is on its own line, inside the parens
1 # one
+ 2 # two
)
reformatted -
All done! ✨ 🍰 ✨
1 file reformatted. |
Re: leading commas, I'm wondering how special this case is? If it's just commas, we could "attach" the comment to the next token, but I'm wondering if there are other situations where we get this wrong? |
Came here with the same issue. For anyone dealing with this issue right now, the very bad, no good, temporary workaround that I found was to use
becomes
|
@IanEdington of the options I listed above, do you have a preference for how this should work? |
I personally would prefer "Don't merge lines if they contain comments." Both the existence of the new line and the comment are extra work that indicate the writer wants to convey more context on those lines. Writing this required me to write out 3 additional and 27 additional characters. This would indicate that these lines are a lot more complex than the code communicates. a = ( # num apples
1 # eating time
+ 2 # bugs consume
) Neither of these communicate the same thing as the above code block a = 1 + 2 # num apples # eating time # bugs consume
# num apples
# eating time
# bugs consume
a = 1 + 2 I could see the argument for doing this (if it was sql) but I still prefer the multiple lines. a /* num apples */ in ( 1, /* eating time */ 2 /* bugs consume */ ) This would be nice a = ( # num apples
1 # eating time
+ 2 # bugs consume
) |
Ok, I'm agreeing with you. We're definitely not doing this: a /* num apples */ in ( 1, /* eating time */ 2 /* bugs consume */ ) Will need to think through edge cases, like this: myfunc(a, b, # comment on myfunc or b?
c,
d
) Because both of these could make sense to me: myfunc( # comment on myfunc or b?
a, b, c, d
)
myfunc (
a,
b, # comment on myfunc or b?
c,
d
) And this will also require special-casing leading commas. TBD if this can be done with the current Line-based implementation of Comment (I hope so), or if we have to refactor so Comments are attached to Nodes. |
I think you need to isolate if the comment is in-line or not. I imagine that's where the complexity is added. FWIW, I'm a proponent of avoiding in-line comments unless absolutely necessary (though I can understand not everyone shares that same philosophy). If a comment is on its own line, then it needs to stay on its own line and preserve the original line order. Then you can apply the formatting for in-line comments afterwards--which may include combining comments on multiple lines. |
No, we already do this, actually.
This is kind of the opposite of the direction I would have gone in, which is telling me that we probably shouldn't be messing with comments at all. |
I 💯 agree with this. If I took the time to put a comment on a new line. I want it on a new line and I'm going to make sure it's on a new line even if I have to do something ridiculous like this
Actual code (except we don't actually work with anchovies) in our codebase in order to get around our current autoformatter that puts all comments as inline comments regardless of how long they are. The trailing |
* feat: revisit comments, close #348 * chore: improve unit testing and docs * fix: allow merging if lines end in trailing inline comment * fix: allow merging ahead of inline comments' * fix: allow merging of blank lines after inline comments * fix: keep inline comments inline * fix: strip whitespace when splitting comments onto multiple lines * fix: merge through comments if directly after standalone operator * chore: bump primer refs * fix: rename split param to no_tail * fix: improve comment placement with trailing operators; self-review * chore: bump primer ref for http
Describe the bug
The line order of comments and code is not preserved after formatting.
select
keyword insteadTo Reproduce
Expected behavior
The columns in the
table_a
CTE should continue to stay on different lines to preserve the meaning of the comments. The comment above thetable_b
CTE should also stay above it, despite using a leading comma.Actual behavior
Because the
select
statement in thetable_a
CTE can fit on a single line, all comments were pushed above the CTE (including in-line comments).The comment above the
table_b
CTE was also pushed above thetable_a
CTE (presumably because of the leading comma).Additional context
What is the output of
sqlfmt --version
?The text was updated successfully, but these errors were encountered: