@@ -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.
13581483Return the brace token.
13591484When this function returns, the cursor is at the start of the token.
0 commit comments