-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
Avoid extra indentation on Go switches #6817
Conversation
@Triton171 I am not an expert on the indent queries but from your comment on #6772 it sounds like you had something slightly different in mind. |
Yeah, this is not what I meant. I believe this issue needs essentially the same fix as #5713. This PR currently breaks go indentation in some ways (for example since the |
I indeed had a different fix in mind at first, but then I realized that indenting on switches and selects is the wrong approach to begin with, since those aren't indented in Go at all - only their cases. That is what I tried to explain in the commit message :) I have tested these changes locally and tried to find edge cases where they break (by using I'm slightly confused by the need for the |
Sorry, I should have been more specific. Removing the func f() {
do_smt()
} // This curly brace is part of the function block but it shouldn't be indented, so it needs an @outdent query Your change causes the last curly brace to be indented one level which is incorrect. This can be fixed by keeping the query but adding more exceptions to it:
Also, your solution will not correctly indent incomplete case expressions: func f() {
switch {
case true: // Pressing <Enter> here will not increase indentation but it should
}
} This happens because the
I haven't tested all of this, so let me know if you encounter any issues. |
Thank you, you're right that this PR as-is is not entirely correct. I spotted two of the problems you mentioned on my second day of using the file as I work. I'll come back with a new version :) |
Your suggested changes do seem to do a lot better. I've updated the PR to include them. I spotted two more bugs, starting at one level of indentation:
The second one is arguably not worth fixing, as I only found it by chance, but the first one we should fix. I'm not sure why I'm getting the indentation; switches and selects don't appear in any |
Loogs good to me, thanks for working on this! I think the CI failure is unrelated (rebasing on the latest master should fix it).
|
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.
LGTM please rebase on master so the ci is fixed and we can merge
Unlike other languages, in Go, switches themselves are not indented; it's just each case body which is indented by one level: switch foo { case "bar": baz() } As such, we shouldn't @indent for type_switch_statement nor expression_switch_statement, as otherwise inserted lines show up as: switch foo { // inserted with "o" case "bar": // inserted with "o" baz() } With the fix, the inserted lines are indented properly: switch foo { // inserted with "o" case "bar": // inserted with "o" baz() } I also verified that indentation on selects similarly works well. Thanks to Triton171 for helping with this fix. Fixes helix-editor#6772.
e8cefce
to
d5a0f1a
Compare
@Triton171 thanks again - that makes sense. I've attributed you in the commit message. @pascalkuthe rebased. |
Unlike other languages, in Go, switches themselves are not indented; it's just each case body which is indented by one level: switch foo { case "bar": baz() } As such, we shouldn't `@indent` for type_switch_statement nor expression_switch_statement, as otherwise inserted lines show up as: switch foo { // inserted with "o" case "bar": // inserted with "o" baz() } With the fix, the inserted lines are indented properly: switch foo { // inserted with "o" case "bar": // inserted with "o" baz() } I also verified that indentation on selects similarly works well. Thanks to Triton171 for helping with this fix.
Unlike other languages, in Go, switches themselves are not indented; it's just each case body which is indented by one level: switch foo { case "bar": baz() } As such, we shouldn't `@indent` for type_switch_statement nor expression_switch_statement, as otherwise inserted lines show up as: switch foo { // inserted with "o" case "bar": // inserted with "o" baz() } With the fix, the inserted lines are indented properly: switch foo { // inserted with "o" case "bar": // inserted with "o" baz() } I also verified that indentation on selects similarly works well. Thanks to Triton171 for helping with this fix.
Unlike other languages, in Go, switches themselves are not indented; it's just each case body which is indented by one level: switch foo { case "bar": baz() } As such, we shouldn't `@indent` for type_switch_statement nor expression_switch_statement, as otherwise inserted lines show up as: switch foo { // inserted with "o" case "bar": // inserted with "o" baz() } With the fix, the inserted lines are indented properly: switch foo { // inserted with "o" case "bar": // inserted with "o" baz() } I also verified that indentation on selects similarly works well. Thanks to Triton171 for helping with this fix.
(see commit message)
Fixes #6772.