on master help sign in
Avoid extra indentation on Go switches (#6817)

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.

commite7f25d88ca059ba006525bd7d476da9ada5341b6 authorDaniel Martí <mvdan@mvdan.cc> dateWed 26 Apr 2023, 01:05 (4 years ago) parent096ed0c
1 file changed+16−4
runtime/queries/go/indents.scm+16−4
runtime/queries/go/indents.scm+16−4
@@ -14,8 +14,6 @@
1414 (argument_list)
1515 (field_declaration_list)
1616 (block)
17− (type_switch_statement)
18− (expression_switch_statement)
1917 (var_declaration)
2018] @indent
2119
@@ -24,5 +22,19 @@
2422 ")"
2523] @outdent
2624
27−((_ "}" @outdent) @outer (#not-kind-eq? @outer "select_statement"))
28−(communication_case) @extend
25+; Switches and selects aren't indented, only their case bodies are.
26+; Outdent all closing braces except those closing switches or selects.
27+(
28+ (_ "}" @outdent) @outer
29+ (#not-kind-eq? @outer "select_statement")
30+ (#not-kind-eq? @outer "type_switch_statement")
31+ (#not-kind-eq? @outer "expression_switch_statement")
32+)
33+
34+; Starting a line after a new case should indent.
35+[
36+ (communication_case)
37+ (expression_case)
38+ (default_case)
39+ (type_case)
40+] @extend