[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[nongnu] elpa/swift-mode 37cab39 263/496: Improve indentation for switch
From: |
ELPA Syncer |
Subject: |
[nongnu] elpa/swift-mode 37cab39 263/496: Improve indentation for switch and class statements |
Date: |
Sun, 29 Aug 2021 11:33:48 -0400 (EDT) |
branch: elpa/swift-mode
commit 37cab39a050691b66bea71461496e66d6a51af40
Author: ap4y <lod@pisem.net>
Commit: ap4y <lod@pisem.net>
Improve indentation for switch and class statements
Some of the recent changes broke indentation around switch statements
due to the lack of the test around some of the cases.
- Restores support for the custom case keyword offset
- Fixes several lexer regex inconsistences around switch and class
statements
- Restored default indentation for the hanging comma prior to #99
- Improved test coverage
fixes #106
---
swift-mode.el | 40 ++++++-----
test/indentation-tests.el | 165 +++++++++++++++++++++++++++++++++++++++++-----
2 files changed, 166 insertions(+), 39 deletions(-)
diff --git a/swift-mode.el b/swift-mode.el
index 592179a..edea55f 100644
--- a/swift-mode.el
+++ b/swift-mode.el
@@ -62,10 +62,11 @@
:type 'integer
:package-version '(swift-mode "0.3.0"))
-(defcustom swift-indent-hanging-comma-offset 4
+(defcustom swift-indent-hanging-comma-offset nil
"Defines the indentation offset for hanging comma."
:group 'swift
- :type 'integer
+ :type '(choice (const :tag "Use default relative formatting" nil)
+ (integer :tag "Custom offset"))
:package-version '(swift-mode "0.4.0"))
(defcustom swift-repl-executable
@@ -165,8 +166,8 @@
(case-exps (exp)
(guard-exp)
(case-exps "," case-exps))
- (case ("case" case-exps "case-:" insts))
- (switch-body (case))
+ (case (case-exps "case-:" insts))
+ (switch-body (case) (case "case" case))
(for-head (in-exp) (op-exp) (for-head ";" for-head))
@@ -266,6 +267,11 @@
token
))
+(defconst swift-smie--lookback-max-lines -2
+ "Max number of lines 'looking-back' allowed to scan.
+In some cases we can't avoid reverse lookup and this operation can be slow.
+We try to constraint those lookups by reasonable number of lines.")
+
(defun swift-smie--forward-token ()
(skip-chars-forward " \t")
(cond
@@ -274,14 +280,14 @@
";")
((looking-at "{") (forward-char 1)
- (if (looking-back "\\(class\\|protocol\\) [^{]+{"
(line-beginning-position) t)
+ (if (looking-back "\\(class\\|protocol\\) [^{]+{" (line-beginning-position
swift-smie--lookback-max-lines) t)
(concat (match-string 1) "-{")
"{"))
((looking-at "}") (forward-char 1) "}")
((looking-at ",") (forward-char 1) ",")
((looking-at ":") (forward-char 1)
- (if (looking-back "case [^:]+:" (line-beginning-position 0) t)
+ (if (looking-back "\\(case [^:]+\\|default\\):" (line-beginning-position
0) t)
"case-:"
":"))
@@ -331,14 +337,14 @@
";")
((eq (char-before) ?\{) (backward-char 1)
- (if (looking-back "\\(class\\|protocol\\) [^{]+"
(line-beginning-position) t)
+ (if (looking-back "\\(class\\|protocol\\) [^{]+"
(line-beginning-position swift-smie--lookback-max-lines) t)
(concat (match-string 1) "-{")
"{"))
((eq (char-before) ?\}) (backward-char 1) "}")
((eq (char-before) ?,) (backward-char 1) ",")
((eq (char-before) ?:) (backward-char 1)
- (if (looking-back "case [^:]+" (line-beginning-position 0))
+ (if (looking-back "case [^:]+\\|default" (line-beginning-position 0))
"case-:"
":"))
@@ -394,18 +400,15 @@
;; assignment expression.
;; Static indentation relatively to =
((smie-rule-parent-p "=") 2)
- ;; Rule for the case statement.
- ((smie-rule-parent-p "case") swift-indent-offset)
((smie-rule-parent-p ",") (smie-rule-parent swift-indent-offset))
;; Rule for the class definition.
((smie-rule-parent-p "class") (smie-rule-parent swift-indent-offset))))
- (`(:after . "{")
- (if (smie-rule-parent-p "switch")
+ ;; Indentation rules for switch statements
+ (`(:before . "case")
+ (if (smie-rule-parent-p "{")
(smie-rule-parent swift-indent-switch-case-offset)))
- (`(:before . ";")
- (if (smie-rule-parent-p "case")
- (smie-rule-parent swift-indent-offset)))
+ (`(:before . "case-:") (smie-rule-parent swift-indent-offset))
;; Apply swift-indent-multiline-statement-offset only if
;; - if is a first token on the line
@@ -424,13 +427,8 @@
(+ swift-indent-offset swift-indent-multiline-statement-offset)
swift-indent-multiline-statement-offset)))
- ;; Indent second line of the multi-line class
- ;; definitions with swift-indent-offset
- (`(:before . "case")
- (smie-rule-parent))
-
(`(:before . ",")
- (if (smie-rule-parent-p "class" "case")
+ (if (and swift-indent-hanging-comma-offset (smie-rule-parent-p "class"
"case"))
(smie-rule-parent swift-indent-hanging-comma-offset)))
;; Disable unnecessary default indentation for
diff --git a/test/indentation-tests.el b/test/indentation-tests.el
index e03b982..8defb9a 100644
--- a/test/indentation-tests.el
+++ b/test/indentation-tests.el
@@ -59,8 +59,7 @@ values of customisable variables."
(swift-indent-offset 4)
(swift-indent-switch-case-offset 0)
(swift-indent-multiline-statement-offset 2)
- ;; Change from default value to detect offset bug.
- (swift-indent-hanging-comma-offset 3)
+ (swift-indent-hanging-comma-offset nil)
,@var-bindings)
(with-temp-buffer
(insert ,before)
@@ -219,22 +218,22 @@ if foo {
(check-indentation indents-case-statements-to-same-level-as-enclosing-switch/1
"
switch true {
- |case
+ |case foo:
}
" "
switch true {
-|case
+|case foo:
}
")
(check-indentation indents-case-statements-to-same-level-as-enclosing-switch/2
"
switch true {
- |case
+ |case foo:
}
" "
switch true {
-|case
+|case foo:
}
")
@@ -242,13 +241,13 @@ switch true {
"
{
switch true {
-|case
+|case foo:
}
}
" "
{
switch true {
- |case
+ |case foo:
}
}
")
@@ -257,13 +256,13 @@ switch true {
"
{
switch true {
- |case
+ |case foo:
}
}
" "
{
switch true {
- |case
+ |case foo:
}
}
")
@@ -298,6 +297,23 @@ switch {
}
")
+(check-indentation indents-case-statements-to-same-level-as-enclosing-switch/7
+ "
+switch {
+case foo:
+ foo
+ bar
+ |case baz:
+}
+" "
+switch {
+case foo:
+ foo
+ bar
+|case baz:
+}
+")
+
(check-indentation indents-case-statement-bodies/1
"
switch x {
@@ -421,6 +437,27 @@ case y:
}
")
+(check-indentation
indents-default-statements-to-same-level-as-enclosing-switch/3
+ "
+{
+ switch true {
+ case y:
+ x
+ default:
+ foo
+ |}
+}
+" "
+{
+ switch true {
+ case y:
+ x
+ default:
+ foo
+ |}
+}
+")
+
(check-indentation indents-statements-under-default-case/1
"
{
@@ -490,10 +527,24 @@ case foo where bar,
" "
switch true {
case foo where bar,
- |bar where baz:
+ |bar where baz:
}
")
+(check-indentation indents-case-statements-with-multiline-guard-custom-offset/1
+ "
+switch true {
+case foo where bar,
+|bar where baz:
+}
+" "
+switch true {
+case foo where bar,
+ |bar where baz:
+}
+"
+((swift-indent-hanging-comma-offset 3)))
+
(check-indentation indents-case-statements-with-multiline-guard/2
"
switch true {
@@ -513,11 +564,11 @@ case foo where bar,
(check-indentation indents-case-statements-to-user-defined-offset/1
"
switch true {
- |case
+ |case foo:
}
" "
switch true {
- |case
+ |case foo:
}
"
((swift-indent-switch-case-offset 2)))
@@ -525,11 +576,11 @@ switch true {
(check-indentation indents-case-statements-to-user-defined-offset/2
"
switch true {
- |case
+ |default:
}
" "
switch true {
- |case
+ |default:
}
"
((swift-indent-switch-case-offset 2)))
@@ -700,10 +751,22 @@ class Foo: Foo, Bar,
}
" "
class Foo: Foo, Bar,
- |Baz {
+ |Baz {
}
")
+(check-indentation indents-class-declaration-custom-offset/1
+ "
+class Foo: Foo, Bar,
+|Baz {
+}
+" "
+class Foo: Foo, Bar,
+ |Baz {
+}
+"
+((swift-indent-hanging-comma-offset 3)))
+
(check-indentation indents-class-declaration/6
"
class Foo:
@@ -724,7 +787,7 @@ class Foo: Bar<A, B,
|C>
")
-(check-indentation indents-class-declaration/9
+(check-indentation indents-class-declaration/8
"
class Foo<A: B<C>>:
|Bar
@@ -733,6 +796,36 @@ class Foo<A: B<C>>:
|Bar
")
+(check-indentation indents-class-declaration/9
+ "
+class Foo: Foo,
+ Bar,
+ Bar2,
+ |Baz {
+}
+" "
+class Foo: Foo,
+ Bar,
+ Bar2,
+ |Baz {
+}
+")
+
+(check-indentation indents-class-declaration/10
+ "
+class Foo: Foo,
+ Bar,
+ Bar2,
+ Baz {
+ |}
+" "
+class Foo: Foo,
+ Bar,
+ Bar2,
+ Baz {
+|}
+")
+
(check-indentation indents-public-class-declaration/1
"
public class Foo: Foo, Bar,
@@ -740,10 +833,22 @@ public class Foo: Foo, Bar,
}
" "
public class Foo: Foo, Bar,
- |Baz {
+ |Baz {
}
")
+(check-indentation indents-public-class-declaration-custom-offset/1
+ "
+public class Foo: Foo, Bar,
+|Baz {
+}
+" "
+public class Foo: Foo, Bar,
+ |Baz {
+}
+"
+((swift-indent-hanging-comma-offset 3)))
+
(check-indentation indents-public-class-declaration/2
"
public class Foo {
@@ -755,6 +860,30 @@ public class Foo {
}
")
+(check-indentation indents-public-class-declaration/3
+ "
+public class Foo: Foo, Bar,
+ Baz {
+ |}
+" "
+public class Foo: Foo, Bar,
+ Baz {
+|}
+")
+
+(check-indentation indents-public-class-declaration/4
+ "
+public class Foo: Foo, Bar,
+ Baz {
+|foo
+}
+" "
+public class Foo: Foo, Bar,
+ Baz {
+ |foo
+}
+")
+
(check-indentation indents-func-declaration/1
"
func Foo(a: String) {
- [nongnu] elpa/swift-mode a6267eb 189/496: Improve indentation for the function definitions, (continued)
- [nongnu] elpa/swift-mode a6267eb 189/496: Improve indentation for the function definitions, ELPA Syncer, 2021/08/29
- [nongnu] elpa/swift-mode 782378c 214/496: Correct regex in lexer for the "default" keyword, ELPA Syncer, 2021/08/29
- [nongnu] elpa/swift-mode c38b721 216/496: Support default values for arguments in function declaration, ELPA Syncer, 2021/08/29
- [nongnu] elpa/swift-mode 2cb54c9 222/496: Make grammar for if statement a bit more precise, ELPA Syncer, 2021/08/29
- [nongnu] elpa/swift-mode b6bad10 225/496: Set minimum required version to 24.4, ELPA Syncer, 2021/08/29
- [nongnu] elpa/swift-mode 4a27477 230/496: Merge pull request #89 from uk-ar/fix-indent-in-trailing-closure, ELPA Syncer, 2021/08/29
- [nongnu] elpa/swift-mode 4289bd7 235/496: Fix implicit semicolon rule, ELPA Syncer, 2021/08/29
- [nongnu] elpa/swift-mode a29398b 239/496: Fix half-open range operator, ELPA Syncer, 2021/08/29
- [nongnu] elpa/swift-mode 7f25806 241/496: Add as! operator, ELPA Syncer, 2021/08/29
- [nongnu] elpa/swift-mode 86339ed 247/496: Fix curly bracket's syntax table to enable electric-pair, ELPA Syncer, 2021/08/29
- [nongnu] elpa/swift-mode 37cab39 263/496: Improve indentation for switch and class statements,
ELPA Syncer <=
- [nongnu] elpa/swift-mode cb7ba6f 245/496: Add an option for indent after hanging comma, ELPA Syncer, 2021/08/29
- [nongnu] elpa/swift-mode 0185315 274/496: Merge pull request #107 from uk-ar/fix-forward-list, ELPA Syncer, 2021/08/29
- [nongnu] elpa/swift-mode 27ec163 286/496: Merge pull request #113 from uk-ar/add-keyword-for-attribute, ELPA Syncer, 2021/08/29
- [nongnu] elpa/swift-mode f43317e 290/496: Add MELPA badge in README, ELPA Syncer, 2021/08/29
- [nongnu] elpa/swift-mode c73ce40 301/496: Update README.md about swift3-mode, ELPA Syncer, 2021/08/29
- [nongnu] elpa/swift-mode 7fc03bf 295/496: Merge pull request #119 from syohex/update-url, ELPA Syncer, 2021/08/29
- [nongnu] elpa/swift-mode 73abb98 292/496: Use cl-lib function instead of cl.el one, ELPA Syncer, 2021/08/29
- [nongnu] elpa/swift-mode 787658f 305/496: Update CONTRIBUTING.md, ELPA Syncer, 2021/08/29
- [nongnu] elpa/swift-mode 0e24233 307/496: Fix forward/backward-sexp inside comments, ELPA Syncer, 2021/08/29
- [nongnu] elpa/swift-mode 60dab9b 308/496: Update test to return informative exit status, ELPA Syncer, 2021/08/29