branch: elpa/swift-mode commit 7837c9099840d71fd91e9ffe8c2f3bcf533a4802 Author: taku0 <mxxouy6x3m_git...@tatapa.org> Commit: taku0 <mxxouy6x3m_git...@tatapa.org>
Fix indentation around "where" and "catch" --- swift-mode-indent.el | 80 ++++++++++++++-------- test/swift-files/statements.swift | 137 +++++++++++++++++++++++++++++++------- 2 files changed, 166 insertions(+), 51 deletions(-) diff --git a/swift-mode-indent.el b/swift-mode-indent.el index fcdc999..8a4d7df 100644 --- a/swift-mode-indent.el +++ b/swift-mode-indent.el @@ -89,7 +89,7 @@ (defconst swift-mode:expression-parent-tokens (append swift-mode:statement-parent-tokens - '(\, < supertype-: "where" "if" "guard" "while")) + '(\, < supertype-: "where" "if" "guard" "while" "catch")) "Parent tokens for expressions.") (defun swift-mode:indent-line () @@ -272,21 +272,31 @@ ;; Other cases. Aligns with the previous case. (swift-mode:align-with-current-line)))) + ;; After "catch" + ((equal previous-text "catch") + (swift-mode:find-and-align-with-parents + swift-mode:statement-parent-tokens + swift-mode:multiline-statement-offset)) + ;; Before "where" on the same line ((and next-is-on-same-line (equal next-text "where")) ;; switch { ;; case let P(x) ;; where - ;; a, - ;; let Q(x) - ;; where - ;; a: + ;; a ;; aaa ;; } ;; ;; for case (x, y) in xys - ;; where - ;; aaa { + ;; where + ;; aaa { + ;; } + ;; + ;; for (x, y) + ;; in + ;; xys + ;; where + ;; aaa { ;; } ;; ;; do { @@ -308,20 +318,30 @@ ;; where ;; ABC { ;; } - (let ((parent (save-excursion (swift-mode:backward-sexps-until - (append swift-mode:statement-parent-tokens - '("case")))))) - (if (equal (swift-mode:token:text parent) "case") - (progn - (goto-char (swift-mode:token:end previous-token)) - (swift-mode:backward-token-or-list) - (swift-mode:calculate-indent-of-expression - swift-mode:multiline-statement-offset - swift-mode:multiline-statement-offset)) + (let* ((parent (save-excursion (swift-mode:backward-sexps-until + (append swift-mode:statement-parent-tokens + '("case" "catch" "for"))))) + (previous-of-parent (save-excursion + (goto-char (swift-mode:token:start parent)) + (swift-mode:backward-token)))) + (when (and + (equal (swift-mode:token:text parent) "case") + (equal (swift-mode:token:text previous-of-parent) "for")) + (setq parent previous-of-parent)) + (cond + ((member (swift-mode:token:text parent) '("case" "catch")) + (goto-char (swift-mode:token:end previous-token)) + (swift-mode:backward-token-or-list) + (swift-mode:calculate-indent-of-expression + swift-mode:multiline-statement-offset + swift-mode:multiline-statement-offset)) + ((equal (swift-mode:token:text parent) "for") + (swift-mode:find-and-align-with-parents '("for"))) + (t (swift-mode:find-and-align-with-parents (append swift-mode:statement-parent-tokens - '(< "case" "catch" "for")) - swift-mode:multiline-statement-offset)))) + '(<)) + swift-mode:multiline-statement-offset))))) ;; After { ((eq previous-type '{) @@ -408,12 +428,18 @@ (let ((parent (save-excursion (swift-mode:backward-sexps-until (append swift-mode:statement-parent-tokens - '("case")))))) - (swift-mode:find-and-align-with-parents - (append swift-mode:statement-parent-tokens - '(< "case" "catch" "for") - (if (equal (swift-mode:token:text parent) "case") '(\,) '())) - swift-mode:multiline-statement-offset)))) + '("case" "catch")))))) + (if (member (swift-mode:token:text parent) '("case" "catch")) + (progn + (goto-char (swift-mode:token:end previous-token)) + (swift-mode:backward-token-or-list) + (swift-mode:calculate-indent-of-expression + swift-mode:multiline-statement-offset + swift-mode:multiline-statement-offset)) + (swift-mode:find-and-align-with-parents + (append swift-mode:statement-parent-tokens + '(< "for")) + swift-mode:multiline-statement-offset))))) ;; After implicit-\; or ; ((memq previous-type '(implicit-\; \;)) @@ -471,7 +497,7 @@ 0 '(implicit-\; \;))) - ;; After if, guard, while + ;; After if, guard, and while ((member previous-text '("if" "guard" "while")) (swift-mode:find-and-align-with-parents swift-mode:statement-parent-tokens @@ -949,7 +975,7 @@ comma at eol." (while (and (<= (point) pos) (not result)) (cond ((member (swift-mode:token:text next-token) - '("guard" "while" "case" "where")) + '("guard" "while" "case" "where" "catch")) (setq result next-token)) ((member (swift-mode:token:text next-token) diff --git a/test/swift-files/statements.swift b/test/swift-files/statements.swift index adecd27..cd7603f 100644 --- a/test/swift-files/statements.swift +++ b/test/swift-files/statements.swift @@ -123,7 +123,7 @@ for x in xs .foo // swift-mode:test:known-bug - where // swift-mode:test:known-bug + where aaa .bbb(x) { foo() @@ -142,8 +142,8 @@ for x for x in xs - where aaa - .bbb(x) { // swift-mode:test:known-bug + where aaa + .bbb(x) { foo() foo() } @@ -151,19 +151,44 @@ for x for x in xs where aaa - .bbb(x) { // swift-mode:test:known-bug + .bbb(x) { foo() foo() } for x in xs + where + aaa.bbb(x) { + foo() + foo() +} + +for case .P(x) + in + xs where aaa.bbb(x) { foo() foo() } +for case .P(x) in xs where + aaa + .bbb(x) { + foo() + foo() +} + +for .P(x) in xs where + aaa + .bbb(x) { + foo() + foo() +} + + + // While statements while foo @@ -260,16 +285,16 @@ repeat { foo() foo() } while foo - .bar() // swift-mode:test:known-bug - .baz() + .bar() + .baz() repeat { foo() foo() } while foo - .bar() // swift-mode:test:known-bug - .baz() + .bar() + .baz() repeat { foo() @@ -277,16 +302,16 @@ repeat { } while foo - .bar() // swift-mode:test:known-bug - .baz() + .bar() + .baz() repeat { foo() foo() } while foo - .bar() // swift-mode:test:known-bug - .baz() + .bar() + .baz() // If statement @@ -713,6 +738,38 @@ default: foo() } + +switch foo { +case let .P(x) + where + foo + .bar: +case let AAA + .P(x) + where + foo + .bar: +case let .P(x) where + foo + .bar: +case let AAA + .P(x) where + foo + .bar: +case let .P(x) + where foo + .bar: +case let AAA + .P(x) + where foo + .bar: +case let .P(x) where foo + .bar: +case let AAA + .P(x) where foo + .bar: +} + // swift-mode:test:eval (setq-local swift-mode:switch-case-offset 2) switch foo { @@ -786,27 +843,59 @@ defer { do { } catch Foo - .Bar(x) - where // swift-mode:test:known-bug - foo() - .bar() { + .Bar(x) + where + foo() + .bar() { + foo() + foo() +} catch Foo + .Bar(x) + where + foo() + .bar() { foo() foo() } catch - Foo // swift-mode:test:known-bug - .Bar(x) - where - foo() - .bar() { + Foo + .Bar(x) + where + foo() + .bar() { foo() foo() } catch - where // swift-mode:test:known-bug - foo() - .bar() { + where + foo() + .bar() { foo() foo() } +catch Foo + .Bar(x) + where + foo() + .bar() { + foo() + foo() +} +catch + Foo + .Bar(x) + where + foo() + .bar() { + foo() + foo() +} +catch + where + foo() + .bar() { + foo() + foo() +} + // Conditional control statements