8000 Support if-expression and switch expression · swift-emacs/swift-mode@25e41ed · GitHub
[go: up one dir, main page]

Skip to conten 8000 t

Commit 25e41ed

Browse files
committed
1 parent 629b8a5 commit 25e41ed

File tree

6 files changed

+375
-38
lines changed

6 files changed

+375
-38
lines changed

swift-mode-beginning-of-defun.el

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ The cursor must be at the beginning of a statement."
151151
((member (swift-mode:token:text token) '("var" "let"))
152152
(when (swift-mode:class-like-member-p) token))
153153
((equal (swift-mode:token:text token) "case")
154-
(swift-mode:backward-sexps-until-open-curly-brace)
154+
(swift-mode:backward-sexps-until-open-curly-bracket)
155155
(swift-mode:beginning-of-statement)
156156
(let ((parent-token (swift-mode:find-defun-keyword-simple)))
157157
(when (equal (swift-mode:token:text parent-token) "enum")
@@ -201,7 +201,7 @@ The cursor must be at the beginning of a statement."
201201
Also return t if the cursor is on a global declaration.
202202
Return nil otherwise."
203203
(or
204-
(let ((parent (swift-mode:backward-sexps-until-open-curly-brace)))
204+
(let ((parent (swift-mode:backward-sexps-until-open-curly-bracket)))
205205
(eq (swift-mode:token:type parent) 'outside-of-buffer))
206206
(progn
207207
(swift-mode:beginning-of-statement)
@@ -1391,7 +1391,7 @@ of ancestors."
13911391
(if (bobp)
13921392
nil
13931393
(let ((name-token (swift-mode:current-defun-name-token)))
1394-
(swift-mode:backward-sexps-until-open-curly-brace)
1394+
(swift-mode:backward-sexps-until-open-curly-bracket)
13951395
(if name-token
13961396
(cons name-token (swift-mode:current-defun-name-token-list))
13971397
(swift-mode:current-defun-name-token-list)))))

swift-mode-indent.el

Lines changed: 148 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ Also used for regexes."
308308
((and next-is-on-current-line (eq next-type '}))
309309
(goto-char (swift-mode:token:end next-token))
310310
(backward-list)
311-
(swift-mode:calculate-indent-after-open-curly-brace 0))
311+
(swift-mode:calculate-indent-for-curly-bracket 0))
312312

313313
;; Before ) or ] on the current line
314314
((and next-is-on-current-line (memq next-type '(\) \])))
@@ -417,12 +417,56 @@ Also used for regexes."
417417
'("switch") nil '("case" "default"))))
418418
(if (equal (swift-mode:token:text parent) "switch")
419419
;; Inside a switch-statement. Aligns with the "switch"
420-
(swift-mode:find-parent-and-align-with-next
421-
swift-mode:statement-parent-tokens
422-
swift-mode:switch-case-offset)
420+
(if (swift-mode:bol-other-than-comments-p)
421+
(swift-mode:align-with-current-line
422+
swift-mode:switch-case-offset)
423+
(swift-mode:find-parent-and-align-with-next
424+
swift-mode:statement-parent-tokens
425+
swift-mode:switch-case-offset))
423426
;; Other cases. Aligns with the previous case.
424427
(swift-mode:align-with-current-line))))
425428

429+
;; Before "else" on the current line
430+
((and next-is-on-current-line (equal next-text "else"))
431+
(swift-mode:calculate-indent-before-else))
432+
433+
;; After "else"
434+
;;
435+
;; let a =
436+
;; if x { 1 }
437+
;; else
438+
;; if y { 2 }
439+
;; else { 3 }
440+
;;
441+
;; let a =
442+
;; if x { 1 } else
443+
;; if y { 2 } else
444+
;; { 3 }
445+
;;
446+
;; let a = if x { 1 } else if y { 2 } else
447+
;; { 3 }
448+
((equal previous-text "else")
449+
(goto-char (swift-mode:token:start previous-token))
450+
(if (swift-mode:bol-other-than-comments-p)
451+
(swift-mode:align-with-current-line
452+
swift-mode:multiline-statement-offset)
453+
(swift-mode:calculate-indent-before-else
454+
swift-mode:multiline-statement-offset)))
455+
456+
;; After "if"
457+
;;
458+
;; let a = if
459+
;; x
460+
;; .foo() {
461+
;; 1
462+
;; } else {
463+
;; 2
464+
;; }
465+
((equal previous-text "if")
466+
(goto-char (swift-mode:token:start previous-token))
467+
(swift-mode:align-with-current-line
468+
swift-mode:multiline-statement-offset))
469+
426470
;; After "catch"
427471
((equal previous-text "catch")
428472
(swift-mode:find-parent-and-align-with-next
@@ -432,7 +476,7 @@ Also used for regexes."
432476
;; After {
433477
((eq previous-type '{)
434478
(goto-char (swift-mode:token:start previous-token))
435-
(swift-mode:calculate-indent-after-open-curly-brace
479+
(swift-mode:calculate-indent-for-curly-bracket
436480
swift-mode:basic-offset))
437481

438482
;; After (, [, or < as a open angle bracket
@@ -653,8 +697,8 @@ Also used for regexes."
653697
;; After "in" for anonymous function parameters
654698
((eq previous-type 'anonymous-function-parameter-in)
655699
(goto-char (swift-mode:token:start previous-token))
656-
(swift-mode:backward-sexps-until-open-curly-brace)
657-
(swift-mode:calculate-indent-after-open-curly-brace
700+
(swift-mode:backward-sexps-until-open-curly-bracket)
701+
(swift-mode:calculate-indent-for-curly-bracket
658702
swift-mode:basic-offset))
659703

660704
;; After "in" for "for" statements
@@ -841,14 +885,80 @@ the expression."
841885
(swift-mode:forward-token-simple))
842886
(point))))))
843887

844-
(defun swift-mode:calculate-indent-after-open-curly-brace (offset)
845-
"Return indentation after open curly braces.
888+
(defun swift-mode:calculate-indent-before-else (&optional offset)
889+
"Return indentation before \"else\" token.
890+
891+
Assuming the cursor is before \"else\".
892+
OFFSET is extra offset if given."
893+
;; let x = if x { 1 }
894+
;; else if y { 2 }
895+
;; else { 3 }
896+
;;
897+
;; let x =
898+
;; if x { 1 }
899+
;; else if y { 2 }
900+
;; else { 3 }
901+
;;
902+
;; let a = if x { 1 }
903+
;; else
904+
;; if y { 2 }
905+
;; else { 3 }
906+
;;
907+
;; let a =
908+
;; if x { 1 }
909+
;; else
910+
;; if y { 2 }
911+
;; else { 3 }
912+
(let ((parent (swift-mode:backward-sexps-until
913+
(append
914+
(remove 'implicit-\; swift-mode:statement-parent-tokens)
915+
'("if")))))
916+
(if (equal (swift-mode:token:text parent) "if")
917+
(cond
918+
;; Found "if" at the beginning of a line. Align with it.
919+
;;
920+
;; let a =
921+
;; if x { 1 }
922+
;; else
923+
;; if y { 2 }
924+
;; else { 3 }
925+
((swift-mode:bol-other-than-comments-p)
926+
(swift-mode:align-with-current-line offset))
927+
928+
;; Found "else if".
929+
;;
930+
;; let x =
931+
;; if x { 1 }
932+
;; else if y { 2 }
933+
;; else { 3 }
934+
;;
935+
;; let x =
936+
;; if x { 1 } else if y { 2 }
937+
;; else { 3 }
938+
((equal (swift-mode:token:text (save-excursion
939+
(swift-mode:backward-token)))
940+
"else")
941+
(swift-mode:backward-token)
942+
(if (swift-mode:bol-other-than-comments-p)
943+
(swift-mode:align-with-current-line offset)
944+
(swift-mode:calculate-indent-before-else offset)))
945+
946+
;; let x = if x { 1 }
947+
;; else { 2 }
948+
(t
949+
(swift-mode:calculate-indent-of-expression
950+
(or offset swift-mode:multiline-statement-offset))))
951+
(swift-mode:align-with-current-line offset))))
952+
953+
(defun swift-mode:calculate-indent-for-curly-bracket (offset)
954+
"Return indentation relating to curly brackets.
846955
847-
Assuming the cursor is on the open brace.
848-
OFFSET is the offset of the contents.
849-
This function is also used for close-curly-brace."
956+
It is used for indentation after open curly brackets and for close brackets.
957+
958+
Assuming the cursor is on the open bracket.
959+
OFFSET is the offset of the contents."
850960
;; If the statement is multiline expression, aligns with the start of
851-
;; the line on which the open brace is:
961+
;; the line on which the open bracket is:
852962
;;
853963
;; foo()
854964
;; .then { x in
@@ -860,7 +970,7 @@ This function is also used for close-curly-brace."
860970
;; foo()
861971
;; }
862972
;;
863-
;; rather than
973+
;; rather than the start of the statement:
864974
;;
865975
;; foo()
866976
;; .then { x in
@@ -886,7 +996,7 @@ This function is also used for close-curly-brace."
886996
;; .foo() {
887997
;; }
888998
;;
889-
;; Note that curly brace after binary operator is a part of
999+
;; Note that curly bracket after binary operator is a part of
8901000
;; a multiline expression:
8911001
;;
8921002
;; for x in
@@ -930,7 +1040,7 @@ This function is also used for close-curly-brace."
9301040
(cond
9311041
((member
9321042
(swift-mode:token:text next-token)
933-
'("for" "while" "repeat" "switch" "if" "else" "guard"
1043+
'("for" "while" "repeat" "guard" "switch" "if" "else"
9341044
"defer" "do" "catch"
9351045
"get" "set" "willSet" "didSet" "func" "init" "subscript"
9361046
"enum" "struct" "actor" "class" "extension"
@@ -987,21 +1097,36 @@ This function is also used for close-curly-brace."
9871097
;; foo {
9881098
;; A
9891099
;;
990-
;; This function is called on the open curly brace.
991-
;; If the close curly brace doesn't exist,
1100+
;; This function is called on the open curly bracket.
1101+
;; If the close curly bracket doesn't exist,
9921102
;; swift-mode:forward-token-or-list results in
9931103
;; "Unbalanced parentheses" error.
994-
;; So if the point is just before the open curly brace,
1104+
;; So if the point is just before the open curly bracket,
9951105
;; exits immediately.
9961106
(forward-comment (point-max))
9971107
(if (< (point) pos)
9981108
(setq next-token (swift-mode:forward-token-or-list))
9991109
(goto-char (1+ pos))))))))
1000-
(if is-declaration-or-control-statement-body
1110+
(cond
1111+
((equal (swift-mode:token:text previous-token) "else")
1112+
(goto-char (swift-mode:token:start previous-token))
1113+
(swift-mode:calculate-indent-before-else offset))
1114+
1115+
((or (member (swift-mode:token:text next-token) '("if" "switch")))
1116+
(goto-char (swift-mode:token:start next-token))
1117+
(if (swift-mode:bol-other-than-comments-p)
1118+
(swift-mode:align-with-current-line offset)
10011119
(swift-mode:find-parent-and-align-with-next
10021120
swift-mode:statement-parent-tokens
1003-
offset)
1004-
(swift-mode:calculate-indent-of-expression offset offset))))
1121+
offset)))
1122+
1123+
(is-declaration-or-control-statement-body
1124+
(swift-mode:find-parent-and-align-with-next
1125+
swift-mode:statement-parent-tokens
1126+
offset))
1127+
1128+
(t
1129+
(swift-mode:calculate-indent-of-expression offset offset)))))
10051130

10061131
(defun swift-mode:calculate-indent-of-prefix-comma ()
10071132
"Return indentation for prefix comma.
@@ -1353,7 +1478,7 @@ is the symbol `any', it matches all tokens."
13531478
(setq text (swift-mode:token:text parent)))
13541479
parent))
13551480

1356-
(defun swift-mode:backward-sexps-until-open-curly-brace ()
1481+
(defun swift-mode:backward-sexps-until-open-curly-bracket ()
13571482
"Backward sexps until an open curly brace appears.
13581483
Return the brace token.
13591484
When this function returns, the cursor is at the start of the token.

swift-mode-lexer.el

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -640,6 +640,12 @@ return non-nil."
640640
(swift-mode:forward-token-simple)))
641641
"let"))
642642

643+
;; Suppress implicit semicolon around else
644+
((or
645+
(equal (swift-mode:token:text previous-token) "else")
646+
(equal (swift-mode:token:text next-token) "else"))
647+
nil)
648+
643649
;; Inserts semicolon before open curly bracket.
644650
;;
645651
;; Open curly bracket may continue the previous line, but we do not indent
@@ -718,7 +724,7 @@ return non-nil."
718724
;; Inserts implicit semicolon before keywords that starts a new
719725
;; statement.
720726
((member (swift-mode:token:text next-token)
721-
'("for" "repeat" "switch" "case" "default" "defer" "do" "if"
727+
'("for" "repeat" "case" "default" "defer" "do"
722728
"guard" "let" "var" "throw" "import" "return"))
723729
t)
724730

@@ -740,12 +746,6 @@ return non-nil."
740746
(swift-mode:backward-token-simple)))
741747
"repeat"))))))
742748

743-
;; Inserts implicit semicolon around else
744-
((or
745-
(equal (swift-mode:token:text previous-token) "else")
746-
(equal (swift-mode:token:text next-token) "else"))
747-
t)
748-
749749
;; Inserts implicit semicolon before keywords that behave like method
750750
;; names.
751751
((member (swift-mode:token:text next-token)

0 commit comments

Comments
 (0)
0