branch: elpa/swift-mode commit 6793bd99eae478873324baf12ee9fbacdcbe3031 Author: uk-ar <yuuki....@gmail.com> Commit: uk-ar <yuuki....@gmail.com>
Indent according to operators precedence --- swift-mode.el | 43 ++++++++++++++++++++----------------------- test/indentation-tests.el | 14 +++++++++++++- 2 files changed, 33 insertions(+), 24 deletions(-) diff --git a/swift-mode.el b/swift-mode.el index 2833047..9b74ae8 100644 --- a/swift-mode.el +++ b/swift-mode.el @@ -144,12 +144,10 @@ (method-args (method-arg) (method-args "," method-args)) (method-arg (id "{" closure "}") (exp)) - (exp (op-exp) - ("[" decl-exps "]")) + (exp ("[" decl-exps "]")) (in-exp (id "in" exp)) (guard-exp (exp "where" exp)) - (op-exp (exp "OP" exp)) - (tern-exp (op-exp "?" exp ":" exp)) + (tern-exp (exp "?" exp ":" exp)) (enum-case ("ecase" assign-exp) ("ecase" "(" type ")")) @@ -176,7 +174,7 @@ (closure (insts) (exp "in" insts) (exp "->" id "in" insts))) ;; Conflicts '((nonassoc "{") (assoc "in") (assoc ",") (assoc ";") (assoc ":") (right "=")) - '((assoc "in") (assoc "where") (assoc "OP")) + '((assoc "in") (assoc "where")) '((assoc ";") (assoc "ecase")) '((assoc "case"))) @@ -186,6 +184,7 @@ "^=" "|=" "&&=" "||=" "=") ;; Assignment (Right associative, precedence level 90) (left "||") ;; Disjunctive (Left associative, precedence level 110) (left "&&") ;; Conjunctive (Left associative, precedence level 120) + (right "??") ;; Nil Coalescing (Right associativity, precedence level 120) (nonassoc "<" "<=" ">" ">=" "==" "!=" "===" "!==" "~=") ;; Comparative (No associativity, precedence level 130) (nonassoc "is" "as" "as!" "as?") ;; Cast (No associativity, precedence level 132) (nonassoc "..<" "...") ;; Range (No associativity, precedence level 135) @@ -204,13 +203,16 @@ value) value)) +(defvar swift-smie--operators + '("*=" "/=" "%=" "+=" "-=" "<<=" ">>=" "&=" "^=" "|=" "&&=" "||=" + "<" "<=" ">" ">=" "==" "!=" "===" "!==" "~=" "||" "&&" + "is" "as" "as!" "as?" "..<" "..." + "+" "-" "&+" "&-" "|" "^" + "*" "/" "%" "&*" "&/" "&%" "&" + "<<" ">>" "??")) + (defvar swift-smie--operators-regexp - (regexp-opt '("*=" "/=" "%=" "+=" "-=" "<<=" ">>=" "&=" "^=" "|=" "&&=" "||=" - "<" "<=" ">" ">=" "==" "!=" "===" "!==" "~=" "||" "&&" - "is" "as" "as!" "as?" "..<" "..." - "+" "-" "&+" "&-" "|" "^" - "*" "/" "%" "&*" "&/" "&%" "&" - "<<" ">>" "??"))) + (regexp-opt swift-smie--operators)) (defvar swift-smie--decl-specifier-regexp "\\(?1:mutating\\|override\\|static\\|unowned\\|weak\\)") @@ -287,14 +289,11 @@ We try to constraint those lookups by reasonable number of lines.") ((looking-at "->") (forward-char 2) "->") ((looking-at "<") (forward-char 1) - (if (looking-at "[[:upper:]]") "<T" "OP")) + (if (looking-at "[[:upper:]]") "<T" "<")) ((looking-at ">[?!]?") (goto-char (match-end 0)) - (if (looking-back "[[:space:]]>" 2 t) "OP" "T>")) - - ((looking-at swift-smie--operators-regexp) - (goto-char (match-end 0)) "OP") + (if (looking-back "[[:space:]]>" 2 t) ">" "T>")) ((looking-at swift-smie--decl-specifier-regexp) (goto-char (match-end 1)) "DECSPEC") @@ -344,18 +343,15 @@ We try to constraint those lookups by reasonable number of lines.") (goto-char (match-beginning 0)) "->") ((eq (char-before) ?<) (backward-char 1) - (if (looking-at "<[[:upper:]]") "<T" "OP")) + (if (looking-at "<[[:upper:]]") "<T" "<")) ((looking-back ">[?!]?" (- (point) 2) t) (goto-char (match-beginning 0)) - (if (looking-back "[[:space:]]" 1 t) "OP" "T>")) + (if (looking-back "[[:space:]]" 1 t) ">" "T>")) ((looking-back (regexp-opt swift-mode--type-decl-keywords) (- (point) 9) t) (goto-char (match-beginning 0)) (match-string-no-properties 0)) - ((looking-back swift-smie--operators-regexp (- (point) 3) t) - (goto-char (match-beginning 0)) "OP") - ((looking-back swift-smie--decl-specifier-regexp (- (point) 8) t) (goto-char (match-beginning 1)) "DECSPEC") @@ -412,9 +408,10 @@ We try to constraint those lookups by reasonable number of lines.") ;; Apply swift-indent-multiline-statement-offset if ;; operator is the last symbol on the line - (`(:before . "OP") + (`(:before . ,(pred (lambda (token) + (member token swift-smie--operators)))) (when (and (smie-rule-hanging-p) - (not (smie-rule-parent-p "OP"))) + (not (apply 'smie-rule-parent-p swift-smie--operators))) (if (smie-rule-parent-p "{") (+ swift-indent-offset swift-indent-multiline-statement-offset) swift-indent-multiline-statement-offset))) diff --git a/test/indentation-tests.el b/test/indentation-tests.el index bb11116..4e01ab0 100644 --- a/test/indentation-tests.el +++ b/test/indentation-tests.el @@ -1663,7 +1663,7 @@ let foo = bar > |baz ") -(check-indentation indents-multiline-operators-only-once +(check-indentation indents-multiline-operators-only-once/1 " 1 + 2 + 5 * @@ -1671,6 +1671,18 @@ let foo = bar > " " 1 + 2 + 5 * + |3 +" +) + +(check-indentation indents-multiline-operators-only-once/2 + " +1 + + 2 * 5 + +|3 +" " +1 + + 2 * 5 + |3 " )