Skip to content

Commit 7954107

Browse files
committed
Small fixes
1 parent acfc7fd commit 7954107

File tree

5 files changed

+49
-47
lines changed

5 files changed

+49
-47
lines changed

swift-mode-indent.el

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -407,15 +407,15 @@ declaration and its offset is `swift-mode:basic-offset'."
407407
'("switch") nil '("case" "default"))))
408408
(if (equal (swift-mode:token:text parent) "switch")
409409
;; Inside a switch-statement. Aligns with the "switch"
410-
(swift-mode:find-and-align-with-parents
410+
(swift-mode:find-parent-and-align-with-next
411411
swift-mode:statement-parent-tokens
412412
swift-mode:switch-case-offset)
413413
;; Other cases. Aligns with the previous case.
414414
(swift-mode:align-with-current-line))))
415415

416416
;; After "catch"
417417
((equal previous-text "catch")
418-
(swift-mode:find-and-align-with-parents
418+
(swift-mode:find-parent-and-align-with-next
419419
swift-mode:statement-parent-tokens
420420
swift-mode:multiline-statement-offset))
421421

@@ -476,7 +476,7 @@ declaration and its offset is `swift-mode:basic-offset'."
476476
;; in
477477
;; a
478478
;; }
479-
(swift-mode:find-and-align-with-parents '("for" {)))
479+
(swift-mode:find-parent-and-align-with-next '("for" {)))
480480

481481
;; Before "where" on the current line
482482
((and next-is-on-current-line (equal next-text "where"))
@@ -542,9 +542,9 @@ declaration and its offset is `swift-mode:basic-offset'."
542542
swift-mode:multiline-statement-offset
543543
swift-mode:multiline-statement-offset))
544544
((equal (swift-mode:token:text parent) "for")
545-
(swift-mode:find-and-align-with-parents '("for")))
545+
(swift-mode:find-parent-and-align-with-next '("for")))
546546
(t
547-
(swift-mode:find-and-align-with-parents
547+
(swift-mode:find-parent-and-align-with-next
548548
(append swift-mode:statement-parent-tokens '(<))
549549
swift-mode:multiline-statement-offset)))))
550550

@@ -628,14 +628,14 @@ declaration and its offset is `swift-mode:basic-offset'."
628628
(swift-mode:calculate-indent-of-expression
629629
swift-mode:multiline-statement-offset
630630
swift-mode:multiline-statement-offset))
631-
(swift-mode:find-and-align-with-parents
631+
(swift-mode:find-parent-and-align-with-next
632632
(append swift-mode:statement-parent-tokens '(< "for"))
633633
swift-mode:multiline-statement-offset)))))
634634

635635
;; After implicit-\; or ;
636636
((memq previous-type '(implicit-\; \;))
637637
(goto-char (swift-mode:token:start previous-token))
638-
(swift-mode:find-and-align-with-parents
638+
(swift-mode:find-parent-and-align-with-next
639639
(remove '\; (remove 'implicit-\; swift-mode:statement-parent-tokens))
640640
0
641641
'(implicit-\; \;)))
@@ -677,20 +677,20 @@ declaration and its offset is `swift-mode:basic-offset'."
677677
;; After case ... : or default:
678678
((eq previous-type 'case-:)
679679
(goto-char (swift-mode:token:start previous-token))
680-
(swift-mode:find-and-align-with-parents
680+
(swift-mode:find-parent-and-align-with-next
681681
swift-mode:statement-parent-tokens
682682
(- swift-mode:basic-offset swift-mode:switch-case-offset)))
683683

684684
;; Before ; on the current line
685685
((and next-is-on-current-line (eq next-type '\;))
686-
(swift-mode:find-and-align-with-parents
686+
(swift-mode:find-parent-and-align-with-next
687687
(remove '\; (remove 'implicit-\; swift-mode:statement-parent-tokens))
688688
0
689689
'(implicit-\; \;)))
690690

691691
;; After if, guard, and while
692692
((member previous-text '("if" "guard" "while"))
693-
(swift-mode:find-and-align-with-parents
693+
(swift-mode:find-parent-and-align-with-next
694694
swift-mode:statement-parent-tokens
695695
swift-mode:multiline-statement-offset))
696696

@@ -710,14 +710,17 @@ declaration and its offset is `swift-mode:basic-offset'."
710710
(swift-mode:calculate-indent-of-expression
711711
swift-mode:multiline-statement-offset)))))
712712

713-
(defun swift-mode:find-and-align-with-parents
713+
(defun swift-mode:find-parent-and-align-with-next
714714
(parents
715715
&optional
716716
offset
717717
stop-at-eol-token-types
718718
stop-at-bol-token-types
719719
bol-offset)
720-
"Return start column of the current expressions or statement plus offset.
720+
"Find the parent and return indentation based on it.
721+
722+
A parent is, for example, the open bracket of the containing block or
723+
semicolon of the preceding statement.
721724
722725
PARENTS is a list of token types that precedes an expression or a statement.
723726
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
727730
BOL-OFFSET.
728731
If scanning stops at STOP-AT-BOL-TOKEN-TYPES, align with that token with
729732
BOL-OFFSET.
730-
If STOP-AT-BOL-TOKEN-TYPES is the symbol `any', the cursor is assumed to be
731-
on the previous line."
733+
If STOP-AT-BOL-TOKEN-TYPES or STOP-AT-BOL-TOKEN-TYPES is the symbol
734+
`any', it matches all tokens.
735+
The point is assumed to be on the previous line."
732736
(save-excursion
733737
(let* ((parent (swift-mode:backward-sexps-until
734738
parents
@@ -749,25 +753,19 @@ on the previous line."
749753
stop-at-eol-token-types)
750754
(member (swift-mode:token:text parent)
751755
stop-at-eol-token-types)))))
752-
(when (or stopped-at-parent stopped-at-eol)
753-
(goto-char parent-end)
754-
(forward-comment (point-max)))
755-
;; Now, the cursor is at the first token of the expression.
756-
757756
(if stopped-at-parent
758-
;; The cursor is at the start of the entire expression.
759-
;; Aligns with the start of the expression with offset.
760757
(swift-mode:align-with-next-token parent offset)
761-
;; The cursor is at the middle of the expression.
762-
;; Aligns with this line with bol-offset.
758+
(when stopped-at-eol
759+
(goto-char parent-end)
760+
(forward-comment (point-max)))
763761
(swift-mode:align-with-current-line bol-offset)))))
764762

765763
(defun swift-mode:calculate-indent-of-expression
766764
(&optional
767765
offset
768766
bol-offset
769767
after-attributes)
770-
"Return start column of the current expressions plus offset.
768+
"Return indentation of the current expression.
771769
772770
the cursor is assumed to be on the previous line.
773771
@@ -975,7 +973,7 @@ This function is also used for close-curly-brace."
975973
(setq next-token (swift-mode:forward-token-or-list))
976974
(goto-char (1+ pos))))))))
977975
(if is-declaration-or-control-statement-body
978-
(swift-mode:find-and-align-with-parents
976+
(swift-mode:find-parent-and-align-with-next
979977
swift-mode:statement-parent-tokens
980978
offset)
981979
(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.
14871485
MATCHING-BRACKET-TEXT is a text of the matching bracket.
14881486
UNMATCHING-BRACKET-TEXT is a text of the current bracket."
14891487
(let ((pos (point))
1490-
(prohibited-tokens (append
1488+
(prohibited-tokens (cons
14911489
unmatching-bracket-text
14921490
swift-mode:tokens-not-in-generic-parameter-list))
14931491
(next-token (funcall skip-token-or-list-function)))

swift-mode-lexer.el

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -786,8 +786,7 @@ https://github.com/apple/swift-evolution/blob/master/proposals/0071-member-keywo
786786
"Move point forward to the next position of the end of a token.
787787
788788
Return the token object. If no more tokens available, return a token with
789-
type `out-of-buffer'"
790-
789+
type `outside-of-buffer'."
791790
(let ((pos (point)))
792791
(let ((chunk (swift-mode:chunk-after)))
793792
(when (swift-mode:chunk:comment-p chunk)
@@ -872,22 +871,22 @@ This function does not return `implicit-;' or `type-:'."
872871
;; for angle bracket, and a type parameter starts with an upper case
873872
;; character, a square bracket, a parenthesis, or keyword 'protocol'.
874873
((and (eq (char-after) ?<)
875-
(looking-at "<\\([[:upper:]\\[[(]\\|protocol\\)"))
874+
(looking-at "<\\([[:upper:][(]\\|protocol\\)"))
876875
(forward-char)
877876
(swift-mode:token '< "<" (1- (point)) (point)))
878877

879878
;; Close angle bracket for type parameters
880879
;;
881880
;; Close angle bracket follows identifier, a square bracket, a parenthesis,
882-
;; or another another bracket (e.g. Foo<Bar<[(Int, String)]>>)
881+
;; or another angle bracket (e.g. Foo<Bar<[(Int, String)]>>)
883882
((and (eq (char-after) ?>)
884883
(save-excursion
885884
;; You know that regular languages can be reversed. Thus you may
886885
;; think that `looking-back' reverses the given regexp and scans
887-
;; chars backwards. Nevertheless, `looking-back' function does not
888-
;; do that. It just repeats `looking-at' with decrementing start
886+
;; chars backwards. Nevertheless, `looking-back' function does not
887+
;; do that. It just repeats `looking-at' with decrementing start
889888
;; position until it succeeds. The document says that it is not
890-
;; recommended to use. So using `skip-chars-backward',
889+
;; recommended to use. So using `skip-chars-backward',
891890
;; `skip-syntax-backward', and `looking-at' here.
892891
(skip-chars-backward "])>")
893892
(skip-syntax-backward "w_")
@@ -999,7 +998,7 @@ This function does not return `implicit-;' or `type-:'."
999998
"Move point backward to the previous position of the end of a token.
1000999
10011000
Return the token object. If no more tokens available, return a token with
1002-
type `out-of-buffer'."
1001+
type `outside-of-buffer'."
10031002

10041003
(let ((pos (point)))
10051004
(let ((chunk (swift-mode:chunk-after)))
@@ -1115,14 +1114,14 @@ This function does not return `implicit-;' or `type-:'."
11151114
;; for angle bracket, and a type parameter starts with an upper case
11161115
;; character, a square bracket, a parenthesis, or keyword `protocol'.
11171116
((and (eq (char-before) ?<)
1118-
(looking-at "\\([[:upper:]\\[[(]\\|protocol\\)"))
1117+
(looking-at "\\([[:upper:][(]\\|protocol\\)"))
11191118
(backward-char)
11201119
(swift-mode:token '< "<" (point) (1+ (point))))
11211120

11221121
;; Close angle bracket for type parameters
11231122
;;
11241123
;; Close angle bracket follows identifier, a square bracket, a parenthesis,
1125-
;; or another another bracket (e.g. Foo<Bar<[(Int, String)]>>)
1124+
;; or another angle bracket (e.g. Foo<Bar<[(Int, String)]>>)
11261125
((and (eq (char-before) ?>)
11271126
(save-excursion
11281127
(skip-chars-backward "])>")
@@ -1230,6 +1229,7 @@ This function does not return `implicit-;' or `type-:'."
12301229

12311230
(defun swift-mode:forward-string-chunk ()
12321231
"Skip forward a string chunk.
1232+
12331233
A string chunk is a part of single-line/multiline string delimited with
12341234
quotation marks or interpolated expressions."
12351235
(condition-case nil
@@ -1238,6 +1238,7 @@ quotation marks or interpolated expressions."
12381238

12391239
(defun swift-mode:backward-string-chunk ()
12401240
"Skip backward a string chunk.
1241+
12411242
A string chunk is a part of single-line/multiline string delimited with
12421243
quotation marks or interpolated expressions."
12431244
(condition-case nil
@@ -1246,6 +1247,7 @@ quotation marks or interpolated expressions."
12461247

12471248
(defun swift-mode:beginning-of-string ()
12481249
"Move point to the beginning of single-line/multiline string.
1250+
12491251
Assuming the cursor is on a string."
12501252
(goto-char (or (nth 8 (syntax-ppss)) (point)))
12511253
(let (matching-parenthesis)
@@ -1259,6 +1261,7 @@ Assuming the cursor is on a string."
12591261

12601262
(defun swift-mode:end-of-string ()
12611263
"Move point to the end of single-line/multiline string.
1264+
12621265
Assuming the cursor is on a string."
12631266
(goto-char (or (nth 8 (syntax-ppss)) (point)))
12641267
(let (matching-parenthesis)

test/swift-files/indent/declarations.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,17 +76,17 @@ class Foo {
7676
xx
7777

7878
let f
79-
= g
8079
:
8180
(
8281
Int,
8382
Int
8483
)
85-
->
8684
throws
85+
->
8786
[
8887
X
8988
]
89+
= g
9090

9191

9292
let x = 1,
@@ -420,7 +420,7 @@ struct A {
420420
// Protocol declarations
421421

422422
protocol Foo {
423-
func foo(x, y) -> throws (A, B)
423+
func foo(x, y) throws -> (A, B)
424424
init<A, B>(x: Int) throws
425425
where
426426
A: C

test/swift-files/indent/expressions.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,8 +305,8 @@ let x = { (
305305
x: Int,
306306
y: Int
307307
)
308-
->
309308
throws
309+
->
310310
Foo
311311
in
312312
println("Hello, World! " + x + y)

test/swift-files/indent/types.swift

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -202,13 +202,14 @@ let foo: (
202202
A,
203203
B
204204
)
205+
throws
205206
->
206-
throws (
207+
(
207208
A,
208209
B
209210
)
210-
->
211211
throws
212+
->
212213
[
213214
A
214215
]
@@ -220,14 +221,14 @@ let foo:
220221
A,
221222
B
222223
)
223-
->
224224
throws
225+
->
225226
(
226227
A,
227228
B
228229
)
229-
->
230230
throws
231+
->
231232
[
232233
B
233234
]
@@ -238,22 +239,22 @@ let foo
238239
A,
239240
B
240241
)
241-
->
242242
throws
243+
->
243244
B
244245
= abc
245246

246247
let foo:
247248
(A, B)
248-
->
249249
rethrows
250+
->
250251
B
251252
= abc
252253

253254
let foo
254255
:(A, B)
255-
->
256256
rethrows
257+
->
257258
B
258259
= abc
259260

0 commit comments

Comments
 (0)