branch: elpa/swift-mode commit 79541073d96f11ad0341a66967e302217ab3ec53 Author: taku0 <mxxouy6x3m_git...@tatapa.org> Commit: taku0 <mxxouy6x3m_git...@tatapa.org>
Small fixes --- swift-mode-indent.el | 50 ++++++++++++++---------------- swift-mode-lexer.el | 23 ++++++++------ test/swift-files/indent/declarations.swift | 6 ++-- test/swift-files/indent/expressions.swift | 2 +- test/swift-files/indent/types.swift | 15 ++++----- 5 files changed, 49 insertions(+), 47 deletions(-) diff --git a/swift-mode-indent.el b/swift-mode-indent.el index 3b73c75..dfb59b8 100644 --- a/swift-mode-indent.el +++ b/swift-mode-indent.el @@ -407,7 +407,7 @@ declaration and its offset is `swift-mode:basic-offset'." '("switch") nil '("case" "default")))) (if (equal (swift-mode:token:text parent) "switch") ;; Inside a switch-statement. Aligns with the "switch" - (swift-mode:find-and-align-with-parents + (swift-mode:find-parent-and-align-with-next swift-mode:statement-parent-tokens swift-mode:switch-case-offset) ;; Other cases. Aligns with the previous case. @@ -415,7 +415,7 @@ declaration and its offset is `swift-mode:basic-offset'." ;; After "catch" ((equal previous-text "catch") - (swift-mode:find-and-align-with-parents + (swift-mode:find-parent-and-align-with-next swift-mode:statement-parent-tokens swift-mode:multiline-statement-offset)) @@ -476,7 +476,7 @@ declaration and its offset is `swift-mode:basic-offset'." ;; in ;; a ;; } - (swift-mode:find-and-align-with-parents '("for" {))) + (swift-mode:find-parent-and-align-with-next '("for" {))) ;; Before "where" on the current line ((and next-is-on-current-line (equal next-text "where")) @@ -542,9 +542,9 @@ declaration and its offset is `swift-mode:basic-offset'." 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"))) + (swift-mode:find-parent-and-align-with-next '("for"))) (t - (swift-mode:find-and-align-with-parents + (swift-mode:find-parent-and-align-with-next (append swift-mode:statement-parent-tokens '(<)) swift-mode:multiline-statement-offset))))) @@ -628,14 +628,14 @@ declaration and its offset is `swift-mode:basic-offset'." (swift-mode:calculate-indent-of-expression swift-mode:multiline-statement-offset swift-mode:multiline-statement-offset)) - (swift-mode:find-and-align-with-parents + (swift-mode:find-parent-and-align-with-next (append swift-mode:statement-parent-tokens '(< "for")) swift-mode:multiline-statement-offset))))) ;; After implicit-\; or ; ((memq previous-type '(implicit-\; \;)) (goto-char (swift-mode:token:start previous-token)) - (swift-mode:find-and-align-with-parents + (swift-mode:find-parent-and-align-with-next (remove '\; (remove 'implicit-\; swift-mode:statement-parent-tokens)) 0 '(implicit-\; \;))) @@ -677,20 +677,20 @@ declaration and its offset is `swift-mode:basic-offset'." ;; After case ... : or default: ((eq previous-type 'case-:) (goto-char (swift-mode:token:start previous-token)) - (swift-mode:find-and-align-with-parents + (swift-mode:find-parent-and-align-with-next swift-mode:statement-parent-tokens (- swift-mode:basic-offset swift-mode:switch-case-offset))) ;; Before ; on the current line ((and next-is-on-current-line (eq next-type '\;)) - (swift-mode:find-and-align-with-parents + (swift-mode:find-parent-and-align-with-next (remove '\; (remove 'implicit-\; swift-mode:statement-parent-tokens)) 0 '(implicit-\; \;))) ;; After if, guard, and while ((member previous-text '("if" "guard" "while")) - (swift-mode:find-and-align-with-parents + (swift-mode:find-parent-and-align-with-next swift-mode:statement-parent-tokens swift-mode:multiline-statement-offset)) @@ -710,14 +710,17 @@ declaration and its offset is `swift-mode:basic-offset'." (swift-mode:calculate-indent-of-expression swift-mode:multiline-statement-offset))))) -(defun swift-mode:find-and-align-with-parents +(defun swift-mode:find-parent-and-align-with-next (parents &optional offset stop-at-eol-token-types stop-at-bol-token-types bol-offset) - "Return start column of the current expressions or statement plus offset. + "Find the parent and return indentation based on it. + +A parent is, for example, the open bracket of the containing block or +semicolon of the preceding statement. PARENTS is a list of token types that precedes an expression or a statement. OFFSET is the offset. If it is omitted, assumed to be 0. @@ -727,8 +730,9 @@ If scanning stops at STOP-AT-EOL-TOKEN-TYPES, align with the next token with BOL-OFFSET. If scanning stops at STOP-AT-BOL-TOKEN-TYPES, align with that token with BOL-OFFSET. -If STOP-AT-BOL-TOKEN-TYPES is the symbol `any', the cursor is assumed to be -on the previous line." +If STOP-AT-BOL-TOKEN-TYPES or STOP-AT-BOL-TOKEN-TYPES is the symbol +`any', it matches all tokens. +The point is assumed to be on the previous line." (save-excursion (let* ((parent (swift-mode:backward-sexps-until parents @@ -749,17 +753,11 @@ on the previous line." stop-at-eol-token-types) (member (swift-mode:token:text parent) stop-at-eol-token-types))))) - (when (or stopped-at-parent stopped-at-eol) - (goto-char parent-end) - (forward-comment (point-max))) - ;; Now, the cursor is at the first token of the expression. - (if stopped-at-parent - ;; The cursor is at the start of the entire expression. - ;; Aligns with the start of the expression with offset. (swift-mode:align-with-next-token parent offset) - ;; The cursor is at the middle of the expression. - ;; Aligns with this line with bol-offset. + (when stopped-at-eol + (goto-char parent-end) + (forward-comment (point-max))) (swift-mode:align-with-current-line bol-offset))))) (defun swift-mode:calculate-indent-of-expression @@ -767,7 +765,7 @@ on the previous line." offset bol-offset after-attributes) - "Return start column of the current expressions plus offset. + "Return indentation of the current expression. the cursor is assumed to be on the previous line. @@ -975,7 +973,7 @@ This function is also used for close-curly-brace." (setq next-token (swift-mode:forward-token-or-list)) (goto-char (1+ pos)))))))) (if is-declaration-or-control-statement-body - (swift-mode:find-and-align-with-parents + (swift-mode:find-parent-and-align-with-next swift-mode:statement-parent-tokens offset) (swift-mode:calculate-indent-of-expression offset offset)))) @@ -1487,7 +1485,7 @@ SKIP-TOKEN-OR-LIST-FUNCTION skips forward/backward a token or a list. MATCHING-BRACKET-TEXT is a text of the matching bracket. UNMATCHING-BRACKET-TEXT is a text of the current bracket." (let ((pos (point)) - (prohibited-tokens (append + (prohibited-tokens (cons unmatching-bracket-text swift-mode:tokens-not-in-generic-parameter-list)) (next-token (funcall skip-token-or-list-function))) diff --git a/swift-mode-lexer.el b/swift-mode-lexer.el index 326e750..a41d152 100644 --- a/swift-mode-lexer.el +++ b/swift-mode-lexer.el @@ -786,8 +786,7 @@ https://github.com/apple/swift-evolution/blob/master/proposals/0071-member-keywo "Move point forward to the next position of the end of a token. Return the token object. If no more tokens available, return a token with -type `out-of-buffer'" - +type `outside-of-buffer'." (let ((pos (point))) (let ((chunk (swift-mode:chunk-after))) (when (swift-mode:chunk:comment-p chunk) @@ -872,22 +871,22 @@ This function does not return `implicit-;' or `type-:'." ;; for angle bracket, and a type parameter starts with an upper case ;; character, a square bracket, a parenthesis, or keyword 'protocol'. ((and (eq (char-after) ?<) - (looking-at "<\\([[:upper:]\\[[(]\\|protocol\\)")) + (looking-at "<\\([[:upper:][(]\\|protocol\\)")) (forward-char) (swift-mode:token '< "<" (1- (point)) (point))) ;; Close angle bracket for type parameters ;; ;; Close angle bracket follows identifier, a square bracket, a parenthesis, - ;; or another another bracket (e.g. Foo<Bar<[(Int, String)]>>) + ;; or another angle bracket (e.g. Foo<Bar<[(Int, String)]>>) ((and (eq (char-after) ?>) (save-excursion ;; You know that regular languages can be reversed. Thus you may ;; think that `looking-back' reverses the given regexp and scans - ;; chars backwards. Nevertheless, `looking-back' function does not - ;; do that. It just repeats `looking-at' with decrementing start + ;; chars backwards. Nevertheless, `looking-back' function does not + ;; do that. It just repeats `looking-at' with decrementing start ;; position until it succeeds. The document says that it is not - ;; recommended to use. So using `skip-chars-backward', + ;; recommended to use. So using `skip-chars-backward', ;; `skip-syntax-backward', and `looking-at' here. (skip-chars-backward "])>") (skip-syntax-backward "w_") @@ -999,7 +998,7 @@ This function does not return `implicit-;' or `type-:'." "Move point backward to the previous position of the end of a token. Return the token object. If no more tokens available, return a token with -type `out-of-buffer'." +type `outside-of-buffer'." (let ((pos (point))) (let ((chunk (swift-mode:chunk-after))) @@ -1115,14 +1114,14 @@ This function does not return `implicit-;' or `type-:'." ;; for angle bracket, and a type parameter starts with an upper case ;; character, a square bracket, a parenthesis, or keyword `protocol'. ((and (eq (char-before) ?<) - (looking-at "\\([[:upper:]\\[[(]\\|protocol\\)")) + (looking-at "\\([[:upper:][(]\\|protocol\\)")) (backward-char) (swift-mode:token '< "<" (point) (1+ (point)))) ;; Close angle bracket for type parameters ;; ;; Close angle bracket follows identifier, a square bracket, a parenthesis, - ;; or another another bracket (e.g. Foo<Bar<[(Int, String)]>>) + ;; or another angle bracket (e.g. Foo<Bar<[(Int, String)]>>) ((and (eq (char-before) ?>) (save-excursion (skip-chars-backward "])>") @@ -1230,6 +1229,7 @@ This function does not return `implicit-;' or `type-:'." (defun swift-mode:forward-string-chunk () "Skip forward a string chunk. + A string chunk is a part of single-line/multiline string delimited with quotation marks or interpolated expressions." (condition-case nil @@ -1238,6 +1238,7 @@ quotation marks or interpolated expressions." (defun swift-mode:backward-string-chunk () "Skip backward a string chunk. + A string chunk is a part of single-line/multiline string delimited with quotation marks or interpolated expressions." (condition-case nil @@ -1246,6 +1247,7 @@ quotation marks or interpolated expressions." (defun swift-mode:beginning-of-string () "Move point to the beginning of single-line/multiline string. + Assuming the cursor is on a string." (goto-char (or (nth 8 (syntax-ppss)) (point))) (let (matching-parenthesis) @@ -1259,6 +1261,7 @@ Assuming the cursor is on a string." (defun swift-mode:end-of-string () "Move point to the end of single-line/multiline string. + Assuming the cursor is on a string." (goto-char (or (nth 8 (syntax-ppss)) (point))) (let (matching-parenthesis) diff --git a/test/swift-files/indent/declarations.swift b/test/swift-files/indent/declarations.swift index 469d0f9..5fda5c8 100644 --- a/test/swift-files/indent/declarations.swift +++ b/test/swift-files/indent/declarations.swift @@ -76,17 +76,17 @@ class Foo { xx let f - = g : ( Int, Int ) - -> throws + -> [ X ] + = g let x = 1, @@ -420,7 +420,7 @@ struct A { // Protocol declarations protocol Foo { - func foo(x, y) -> throws (A, B) + func foo(x, y) throws -> (A, B) init<A, B>(x: Int) throws where A: C diff --git a/test/swift-files/indent/expressions.swift b/test/swift-files/indent/expressions.swift index 3be9735..3e7ffc2 100644 --- a/test/swift-files/indent/expressions.swift +++ b/test/swift-files/indent/expressions.swift @@ -305,8 +305,8 @@ let x = { ( x: Int, y: Int ) - -> throws + -> Foo in println("Hello, World! " + x + y) diff --git a/test/swift-files/indent/types.swift b/test/swift-files/indent/types.swift index ca3ab48..331a3c9 100644 --- a/test/swift-files/indent/types.swift +++ b/test/swift-files/indent/types.swift @@ -202,13 +202,14 @@ let foo: ( A, B ) + throws -> - throws ( + ( A, B ) - -> throws + -> [ A ] @@ -220,14 +221,14 @@ let foo: A, B ) - -> throws + -> ( A, B ) - -> throws + -> [ B ] @@ -238,22 +239,22 @@ let foo A, B ) - -> throws + -> B = abc let foo: (A, B) - -> rethrows + -> B = abc let foo :(A, B) - -> rethrows + -> B = abc