Skip to content

Commit f03db1a

Browse files
committed
Fix indentation of switch.
Close #134.
1 parent 1d43501 commit f03db1a

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

swift-mode-indent.el

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -262,10 +262,24 @@ declaration and its offset is `swift-mode:basic-offset'."
262262
next-is-on-same-line
263263
(member next-text '("case" "default"))
264264
(save-excursion
265-
(equal (swift-mode:token:text
266-
(swift-mode:backward-sexps-until
267-
'("switch" "enum" "for" "while" "if" "guard")))
268-
"switch")))
265+
(let ((head
266+
(swift-mode:backward-sexps-until
267+
'(implicit-\; \; "switch" "enum" "for" "while" "if" "guard"))))
268+
(or
269+
(equal (swift-mode:token:text head) "switch")
270+
(and
271+
;; If we got a semicolon, the statement is either switch or enum:
272+
;;
273+
;; switch foo {
274+
;; case 1:
275+
;; if aaa {
276+
;; }; // implicit semicolon
277+
;; case 2:
278+
;; }
279+
(memq (swift-mode:token:type head) '(implicit-\; \;))
280+
(equal (swift-mode:token:text
281+
(swift-mode:backward-sexps-until '("switch" "enum")))
282+
"switch"))))))
269283
;; "case" is used for "switch", "enum", "for", "while", "if", and "guard".
270284
;; Only switch statement has special indentation rule.
271285
;;

test/swift-files/statements.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -885,6 +885,18 @@ case let AAA
885885
.bar:
886886
}
887887

888+
switch foo {
889+
case 1:
890+
if aaa {
891+
}
892+
case 2:
893+
if bbb {
894+
}
895+
default:
896+
if ccc {
897+
}
898+
}
899+
888900
// swift-mode:test:eval (setq-local swift-mode:switch-case-offset 2)
889901

890902
switch foo {

0 commit comments

Comments
 (0)