@@ -308,7 +308,7 @@ Also used for regexes."
308
308
((and next-is-on-current-line (eq next-type '} ))
309
309
(goto-char (swift-mode:token:end next-token))
310
310
(backward-list )
311
- (swift-mode:calculate-indent-after-open- curly-brace 0 ))
311
+ (swift-mode:calculate-indent-for- curly-bracket 0 ))
312
312
313
313
; ; Before ) or ] on the current line
314
314
((and next-is-on-current-line (memq next-type '(\) \] )))
@@ -417,12 +417,56 @@ Also used for regexes."
417
417
'(" switch" ) nil '(" case" " default" ))))
418
418
(if (equal (swift-mode:token:text parent) " switch" )
419
419
; ; 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))
423
426
; ; Other cases. Aligns with the previous case.
424
427
(swift-mode:align-with-current-line))))
425
428
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
+
426
470
; ; After "catch"
427
471
((equal previous-text " catch" )
428
472
(swift-mode:find-parent-and-align-with-next
@@ -432,7 +476,7 @@ Also used for regexes."
432
476
; ; After {
433
477
((eq previous-type '{ )
434
478
(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
436
480
swift-mode:basic-offset))
437
481
438
482
; ; After (, [, or < as a open angle bracket
@@ -653,8 +697,8 @@ Also used for regexes."
653
697
; ; After "in" for anonymous function parameters
654
698
((eq previous-type 'anonymous-function-parameter-in )
655
699
(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
658
702
swift-mode:basic-offset))
659
703
660
704
; ; After "in" for "for" statements
@@ -841,14 +885,80 @@ the expression."
841
885
(swift-mode:forward-token-simple))
842
886
(point ))))))
843
887
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.
846
955
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."
850
960
; ; 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:
852
962
; ;
853
963
; ; foo()
854
964
; ; .then { x in
@@ -860,7 +970,7 @@ This function is also used for close-curly-brace."
860
970
; ; foo()
861
971
; ; }
862
972
; ;
863
- ; ; rather than
973
+ ; ; rather than the start of the statement:
864
974
; ;
865
975
; ; foo()
866
976
; ; .then { x in
@@ -886,7 +996,7 @@ This function is also used for close-curly-brace."
886
996
; ; .foo() {
887
997
; ; }
888
998
; ;
889
- ; ; Note that curly brace after binary operator is a part of
999
+ ; ; Note that curly bracket after binary operator is a part of
890
1000
; ; a multiline expression:
891
1001
; ;
892
1002
; ; for x in
@@ -930,7 +1040,7 @@ This function is also used for close-curly-brace."
930
1040
(cond
931
1041
((member
932
1042
(swift-mode:token:text next-token)
933
- '(" for" " while" " repeat" " switch " " if " " else " " guard "
1043
+ '(" for" " while" " repeat" " guard " " switch " " if " " else "
934
1044
" defer" " do" " catch"
935
1045
" get" " set" " willSet" " didSet" " func" " init" " subscript"
936
1046
" enum" " struct" " actor" " class" " extension"
@@ -987,21 +1097,36 @@ This function is also used for close-curly-brace."
987
1097
; ; foo {
988
1098
; ; A
989
1099
; ;
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,
992
1102
; ; swift-mode:forward-token-or-list results in
993
1103
; ; "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 ,
995
1105
; ; exits immediately.
996
1106
(forward-comment (point-max ))
997
1107
(if (< (point ) pos)
998
1108
(setq next-token (swift-mode:forward-token-or-list))
999
1109
(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)
1001
1119
(swift-mode:find-parent-and-align-with-next
1002
1120
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)))))
1005
1130
1006
1131
(defun swift-mode:calculate-indent-of-prefix-comma ()
1007
1132
" Return indentation for prefix comma.
@@ -1353,7 +1478,7 @@ is the symbol `any', it matches all tokens."
1353
1478
(setq text (swift-mode:token:text parent)))
1354
1479
parent))
1355
1480
1356
- (defun swift-mode:backward-sexps-until-open-curly-brace ()
1481
+ (defun swift-mode:backward-sexps-until-open-curly-bracket ()
1357
1482
" Backward sexps until an open curly brace appears.
1358
1483
Return the brace token.
1359
1484
When this function returns, the cursor is at the start of the token.
0 commit comments