-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathswift-mode-indent.el
2002 lines (1819 loc) · 66 KB
/
swift-mode-indent.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; swift-mode-indent.el --- Major-mode for Apple's Swift programming language, indentation. -*- lexical-binding: t -*-
;; Copyright (C) 2014-2021 taku0, Chris Barrett, Bozhidar Batsov,
;; Arthur Evstifeev
;; Author: taku0 (http://github.com/taku0)
;; Chris Barrett <[email protected]>
;; Bozhidar Batsov <[email protected]>
;; Arthur Evstifeev <[email protected]>
;; This file is not part of GNU Emacs.
;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; Routines for Indentation
;;; Code:
(require 'swift-mode-lexer)
;;; Customizations
(defcustom swift-mode:basic-offset 4
"Amount of indentation for block contents."
:type 'integer
:group 'swift
:safe #'integerp)
(defcustom swift-mode:parenthesized-expression-offset 2
"Amount of indentation inside parentheses and square brackets."
:type 'integer
:group 'swift
:safe #'integerp)
(defcustom swift-mode:multiline-statement-offset 2
"Amount of indentation for continuations of expressions."
:type 'integer
:group 'swift
:safe #'integerp)
(defcustom swift-mode:switch-case-offset 0
"Amount of indentation for case labels in switch statements."
:type 'integer
:group 'swift
:safe #'integerp)
(defcustom swift-mode:prepend-asterisk-to-comment-line nil
"Automatically insert a asterisk to each comment line if non-nil."
:type 'boolean
:group 'swift
:safe #'booleanp)
(defcustom swift-mode:insert-space-after-asterisk-in-comment t
"Automatically insert a space after asterisk in comment if non-nil."
:type 'boolean
:group 'swift
:safe #'booleanp)
(defcustom swift-mode:auto-close-multiline-comment t
"If non-nil, `indent-new-comment-line' automatically close multiline comment."
:type 'boolean
:group 'swift
:safe #'booleanp)
(defcustom swift-mode:fix-comment-close t
"Fix \"* /\" in incomplete multiline comment to \"*/\" if non-nil."
:type 'boolean
:group 'swift
:safe #'booleanp)
(defcustom swift-mode:break-line-before-comment-close t
"If non-nil, break line before the closing delimiter of multiline comments."
:type 'boolean
:group 'swift
:safe #'booleanp)
(defcustom swift-mode:highlight-anchor nil
"Highlight anchor point for indentation if non-nil.
Intended for debugging."
:type 'boolean
:group 'swift
:safe #'booleanp)
;;; Constants and variables
(defconst swift-mode:statement-parent-tokens
'(implicit-\; \; case-: { anonymous-function-parameter-in)
"Parent tokens for statements.")
(defconst swift-mode:expression-parent-tokens
(append swift-mode:statement-parent-tokens
'(\, < \( \[ supertype-: "where" "if" "guard" "while" "for" "catch"
string-chunk-before-interpolated-expression))
"Parent tokens for expressions.")
(defvar-local swift-mode:anchor-overlay nil)
(defvar-local swift-mode:anchor-overlay-timer nil)
;;; Indentation struct
(defun swift-mode:indentation (point offset)
"Construct and return a indentation.
POINT is the position of the anchor point, such as the start of the previous
line or the start of the class declaration.
OFFSET is the offset from the anchor point. For example, when indenting the
first line of a class body, its anchor point is the start of the class
declaration and its offset is `swift-mode:basic-offset'."
(list point offset))
(defun swift-mode:indentation:point (indentation)
"Return the point of INDENTATION."
(nth 0 indentation))
(defun swift-mode:indentation:offset (indentation)
"Return the offset of INDENTATION."
(nth 1 indentation))
;;; Indentation logics
(defun swift-mode:indent-line ()
"Indent the current line."
(let* ((indentation (save-excursion (swift-mode:calculate-indent)))
(indentation-column
(save-excursion
(goto-char (swift-mode:indentation:point indentation))
(+ (current-column) (swift-mode:indentation:offset indentation))))
(current-indent
(save-excursion (back-to-indentation) (current-column))))
(if (<= (current-column) current-indent)
;; The cursor is on the left margin. Moving to the new indent.
(indent-line-to indentation-column)
;; Keeps current relative position.
(save-excursion (indent-line-to indentation-column)))
(when swift-mode:highlight-anchor
(swift-mode:highlight-anchor indentation))))
(defun swift-mode:calculate-indent ()
"Return the indentation of the current line."
(back-to-indentation)
(let ((parser-state (syntax-ppss)))
(cond
((nth 4 parser-state)
;; If the 4th element of `(syntax-ppss)' is non-nil, the cursor is on
;; the 2nd or following lines of a multiline comment, because:
;;
;; - The 4th element of `(syntax-ppss)' is nil on the comment starter.
;; - We have called `back-to-indentation`.
(swift-mode:calculate-indent-of-multiline-comment))
((eq (nth 3 parser-state) t)
(swift-mode:calculate-indent-of-multiline-string))
((looking-at "//")
(swift-mode:calculate-indent-of-single-line-comment))
(t
(swift-mode:calculate-indent-of-code)))))
(defun swift-mode:calculate-indent-of-multiline-comment ()
"Return the indentation of the current line inside a multiline comment."
(back-to-indentation)
(let ((comment-beginning-position (nth 8 (syntax-ppss)))
(starts-with-asterisk (eq (char-after) ?*)))
(forward-line -1)
(back-to-indentation)
(cond
((<= (point) comment-beginning-position)
;; The cursor was on the 2nd line of the comment.
(goto-char comment-beginning-position)
(forward-char)
;; If there are extra characters or spaces after asterisks, aligns with
;; the first non-space character or end of line. Otherwise, aligns with
;; the first asterisk.
(when (and
(looking-at "\\**[^*\n]+")
(not (and swift-mode:prepend-asterisk-to-comment-line
starts-with-asterisk)))
(skip-chars-forward "*")
(skip-syntax-forward " "))
(swift-mode:indentation (point) 0))
;; The cursor was on the 3rd or following lines of the comment.
((= (save-excursion
(forward-line)
(back-to-indentation)
(point))
(save-excursion
(goto-char comment-beginning-position)
(if (forward-comment 1)
(progn
(backward-char)
(skip-chars-backward "*")
(point))
-1)))
;; Before the closing delimiter. Aligns with the first asterisk of the
;; opening delimiter.
(goto-char comment-beginning-position)
(forward-char)
(swift-mode:indentation (point) 0))
;; Otherwise, aligns with a non-empty preceding line.
((and (bolp) (eolp))
;; The previous line is empty, so seeks a non-empty-line.
(swift-mode:calculate-indent-of-multiline-comment))
(t
;; The previous line is not empty, so aligns to this line.
(swift-mode:indentation (point) 0)))))
(defun swift-mode:calculate-indent-of-multiline-string ()
"Return the indentation of the current line inside a multiline string.
Also used for regexes."
(back-to-indentation)
(let ((string-beginning-position
(save-excursion (swift-mode:beginning-of-string))))
(if (and (looking-at "\\(\"\"\"\\|/\\)#*")
(equal (get-text-property (1- (match-end 0)) 'syntax-table)
(string-to-syntax "|")))
;; The last line.
(progn
(goto-char string-beginning-position)
(swift-mode:calculate-indent-of-expression
swift-mode:multiline-statement-offset))
(forward-line 0)
(backward-char)
(swift-mode:goto-non-interpolated-expression-bol)
(back-to-indentation)
(if (<= (point) string-beginning-position)
;; The cursor was on the 2nd line of the string, so aligns with
;; that line with offset.
(progn
(goto-char string-beginning-position)
(swift-mode:calculate-indent-of-expression
swift-mode:multiline-statement-offset))
;; The cursor was on the 3rd or following lines of the string, so
;; aligns with a non-empty preceding line.
(if (and (bolp) (eolp))
;; The cursor is on an empty line, so seeks a non-empty-line.
(swift-mode:calculate-indent-of-multiline-string)
(swift-mode:indentation (point) 0))))))
(defun swift-mode:goto-non-interpolated-expression-bol ()
"Back to the beginning of line that is not inside a interpolated expression."
(let ((string-beginning-position (nth 8 (syntax-ppss)))
(matching-parenthesis t))
(while (and matching-parenthesis
(< (line-beginning-position) string-beginning-position))
(setq matching-parenthesis
(get-text-property
string-beginning-position 'swift-mode:matching-parenthesis))
(when matching-parenthesis
(goto-char matching-parenthesis)
(setq string-beginning-position (nth 8 (syntax-ppss)))))
(forward-line 0)))
(defun swift-mode:calculate-indent-of-single-line-comment ()
"Return the indentation of the current line inside a single-line comment."
(cond
((save-excursion
(forward-line 0)
(bobp))
(swift-mode:indentation (point-min) 0))
((save-excursion
(forward-line -1)
(skip-syntax-forward " ")
(looking-at "//"))
(forward-line -1)
(skip-syntax-forward " ")
(swift-mode:indentation (point) 0))
(t
(swift-mode:calculate-indent-of-code))))
(defun swift-mode:calculate-indent-of-code ()
"Return the indentation of the current line outside multiline comments."
(back-to-indentation)
(let* ((previous-token (save-excursion (swift-mode:backward-token)))
(previous-type (swift-mode:token:type previous-token))
(previous-text (swift-mode:token:text previous-token))
(next-token (save-excursion (swift-mode:forward-token)))
(next-type (swift-mode:token:type next-token))
(next-text (swift-mode:token:text next-token))
(next-is-on-current-line
(<= (swift-mode:token:start next-token) (line-end-position))))
(cond
;; Beginning of the buffer
((eq previous-type 'outside-of-buffer)
(swift-mode:indentation (point-min) 0))
;; Before } on the current line
((and next-is-on-current-line (eq next-type '}))
(goto-char (swift-mode:token:end next-token))
(backward-list)
(swift-mode:calculate-indent-for-curly-bracket 0))
;; Before ) or ] on the current line
((and next-is-on-current-line (memq next-type '(\) \])))
(goto-char (swift-mode:token:end next-token))
(backward-list)
(swift-mode:calculate-indent-of-expression 0))
;; Before > as a close angle bracket on the current line
((and next-is-on-current-line
(save-excursion
(goto-char (swift-mode:token:start next-token))
(and (eq (char-after) ?>)
(progn (swift-mode:try-backward-generic-parameters)
(< (point) (swift-mode:token:start next-token))))))
(swift-mode:try-backward-generic-parameters)
(swift-mode:calculate-indent-of-expression 0))
;; Before end of a interpolated expression on the current line
((and next-is-on-current-line
(eq next-type 'string-chunk-after-interpolated-expression))
(goto-char (get-text-property
(swift-mode:token:start next-token)
'swift-mode:matching-parenthesis))
(forward-char 2)
(swift-mode:backward-string-chunk)
(swift-mode:calculate-indent-after-beginning-of-interpolated-expression
0))
;; Before , on the current line
((and next-is-on-current-line (eq next-type '\,))
(swift-mode:calculate-indent-of-prefix-comma))
;; After ,
((eq previous-type '\,)
(goto-char (swift-mode:token:start previous-token))
(swift-mode:calculate-indent-after-comma))
;; Before "case" or "default" on the current line, for switch statement
((and
next-is-on-current-line
(member next-text '("case" "default"))
(save-excursion
(let ((head
(swift-mode:backward-sexps-until
'(implicit-\; \; "switch" "enum" "for" "while" "if" "guard"))))
(or
(equal (swift-mode:token:text head) "switch")
(and
;; If we got a semicolon, the statement is either switch or enum:
;;
;; switch foo {
;; case 1:
;; if aaa {
;; }; // implicit semicolon
;; case 2:
;; }
(memq (swift-mode:token:type head) '(implicit-\; \;))
(equal (swift-mode:token:text
(swift-mode:backward-sexps-until '("switch" "enum")))
"switch"))))))
;; "case" is used for "switch", "enum", "for", "while", "if", and "guard".
;; Only switch statement has special indentation rule.
;;
;; switch foo {
;; default:
;; aaa
;; case A:
;; aaa
;; case B, C, D:
;; aaa
;; case E(1, 2, 3, (4, 5)) where aaa,
;; F(1, 2, 3, (4, 5)) where aaa:
;; aaa
;; case G: print(1); case H: print(2)
;; case I:
;; ...
;; }
;;
;; enum Foo {
;; case A
;; case B, C, D
;; indirect
;; case E(x: Int, y: Int)
;; }
;;
;; enum Foo: Int, A, B {
;; case A = 1, B = 2
;; case C = 3
;; }
;;
;; for
;; case let (x, y) in tuples {
;; }
;; if
;; case let (x, y) = tuple {
;; }
;; while
;; case let (x, y) = tuple {
;; }
;;
;; Searches sibling "case" at the beginning of a line. If found, aligns
;; with it.
;;
;; Otherwise, searches "switch" and align with it with offset.
(let ((parent (swift-mode:backward-sexps-until
'("switch") nil '("case" "default"))))
(if (equal (swift-mode:token:text parent) "switch")
;; Inside a switch-statement. Aligns with the "switch"
(if (swift-mode:bol-other-than-comments-p)
(swift-mode:align-with-current-line
swift-mode:switch-case-offset)
(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.
(swift-mode:align-with-current-line))))
;; Before "else" on the current line
((and next-is-on-current-line (equal next-text "else"))
(swift-mode:calculate-indent-before-else))
;; After "else"
;;
;; let a =
;; if x { 1 }
;; else
;; if y { 2 }
;; else { 3 }
;;
;; let a =
;; if x { 1 } else
;; if y { 2 } else
;; { 3 }
;;
;; let a = if x { 1 } else if y { 2 } else
;; { 3 }
((equal previous-text "else")
(goto-char (swift-mode:token:start previous-token))
(if (swift-mode:bol-other-than-comments-p)
(swift-mode:align-with-current-line
swift-mode:multiline-statement-offset)
(swift-mode:calculate-indent-before-else
swift-mode:multiline-statement-offset)))
;; After "if"
;;
;; let a = if
;; x
;; .foo() {
;; 1
;; } else {
;; 2
;; }
((equal previous-text "if")
(goto-char (swift-mode:token:start previous-token))
(swift-mode:align-with-current-line
swift-mode:multiline-statement-offset))
;; After "catch"
((equal previous-text "catch")
(swift-mode:find-parent-and-align-with-next
swift-mode:statement-parent-tokens
swift-mode:multiline-statement-offset))
;; After {
((eq previous-type '{)
(goto-char (swift-mode:token:start previous-token))
(swift-mode:calculate-indent-for-curly-bracket
swift-mode:basic-offset))
;; After (, [, or < as a open angle bracket
((memq previous-type '(\( \[ <))
(goto-char (swift-mode:token:start previous-token))
(swift-mode:calculate-indent-of-expression
swift-mode:parenthesized-expression-offset
swift-mode:parenthesized-expression-offset))
;; After beginning of a interpolated expression
((eq previous-type 'string-chunk-before-interpolated-expression)
(goto-char (swift-mode:token:start previous-token))
(swift-mode:calculate-indent-after-beginning-of-interpolated-expression
swift-mode:parenthesized-expression-offset))
;; Before "in" on the current line
((and next-is-on-current-line (equal next-text "in"))
;; When it is for-in statement, align with the token after "for":
;;
;; for
;; x
;; in
;; foo
;;
;; for x
;; in
;; foo
;;
;; When it is anonymous function, align with the token after {:
;;
;; foo {
;; x
;; in
;; ...
;; }
;;
;;
;; foo { x
;; in
;; ...
;; }
;;
;; foo { [
;; weak self
;; ]
;; (
;; x,
;; y
;; )
;; -> Void
;; in
;; a
;; }
(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"))
;; switch {
;; case let P(x)
;; where // Aligns with the pattern.
;; a
;; aaa
;; }
;; case let A
;; .P(x)
;; where // Aligns with the last line of the pattern.
;; a
;; aaa
;; }
;;
;; for case (x, y) in xys
;; where // Aligns with the next token of the "for" token.
;; aaa {
;; }
;;
;; for (x, y)
;; in
;; xys
;; where // Aligns with the next token of the "for" token.
;; aaa {
;; }
;;
;; do {
;; } catch let P(x)
;; where // Aligns with the pattern.
;; aaa
;;
;; func foo<A: AAA,
;; B: BBB
;; where // Aligns with the start of the type parameters.
;; ABC>() {
;; }
;;
;; class Foo<A,
;; B,
;; C>: AAA,
;; BBB,
;; CCC
;; where // Aligns with the "class" token.
;; ABC {
;; }
(let* ((parent (save-excursion (swift-mode:backward-sexps-until
(append swift-mode:statement-parent-tokens
'("case" "catch" "for")))))
(previous-of-parent (save-excursion
(goto-char (swift-mode:token:start parent))
(swift-mode:backward-token))))
(when (and
(equal (swift-mode:token:text parent) "case")
(equal (swift-mode:token:text previous-of-parent) "for"))
(setq parent previous-of-parent))
(cond
((member (swift-mode:token:text parent) '("case" "catch"))
(goto-char (swift-mode:token:end previous-token))
(swift-mode:backward-token-or-list)
(swift-mode:calculate-indent-of-expression
swift-mode:multiline-statement-offset
swift-mode:multiline-statement-offset))
((equal (swift-mode:token:text parent) "for")
(swift-mode:find-parent-and-align-with-next '("for")))
(t
(swift-mode:find-parent-and-align-with-next
(append swift-mode:statement-parent-tokens '(<))
swift-mode:multiline-statement-offset)))))
;; After "where"
((equal previous-text "where")
;; switch {
;; case let .P(x) where
;; A, // Aligns with the pattern.
;; let A
;; .Q(x) where // Aligns with the last line of the pattern.
;; A:
;; aaa
;; case let .P(x)
;; where
;; A, // Aligns with the "where" token.
;; let .Q(x)
;; where // Aligns with the "where" token.
;; A:
;; aaa
;; case let .P(x), let .Q(x) where // Aligns with the pattern.
;; a
;; }
;;
;; for case let (x, y) in xys where
;; aaa { // Aligns with the next token of the "for" token.
;; }
;;
;; for case let (x, y) in xys
;; where
;; aaa { // Aligns with the "where" token
;; }
;;
;; do {
;; } catch let .P(x) where
;; aaa // Aligns with the pattern.
;; do {
;; } catch let .P(x)
;; where
;; aaa // Aligns with the "where" token.
;;
;;
;;
;; func foo<A: AAA,
;; B: BBB where
;; ABC>() { // Aligns with the start of the type parameters.
;; }
;;
;; func foo<A: AAA,
;; B: BBB
;; where
;; ABC>() { // Aligns with the "where" token.
;; }
;;
;; class Foo<A,
;; B,
;; C> A,
;; B,
;; C where
;; ABC { // Aligns with the "class" token"
;; }
;;
;; class Foo<A,
;; B,
;; C>: A,
;; B,
;; C
;; where
;; ABC { // Aligns with the "where" token"
;; }
(goto-char (swift-mode:token:start previous-token))
(if (swift-mode:bol-other-than-comments-p)
(swift-mode:align-with-current-line
swift-mode:multiline-statement-offset)
(let ((parent (save-excursion
(swift-mode:backward-sexps-until
(append swift-mode:statement-parent-tokens
'("case" "catch"))))))
(if (member (swift-mode:token:text parent) '("case" "catch"))
(progn
(swift-mode:backward-token-or-list)
(swift-mode:calculate-indent-of-expression
swift-mode:multiline-statement-offset
swift-mode:multiline-statement-offset))
(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-parent-and-align-with-next
(remove '\; (remove 'implicit-\; swift-mode:statement-parent-tokens))
0
'(implicit-\; \;)))
;; After "in" for anonymous function parameters
((eq previous-type 'anonymous-function-parameter-in)
(goto-char (swift-mode:token:start previous-token))
(swift-mode:backward-sexps-until-open-curly-bracket)
(swift-mode:calculate-indent-for-curly-bracket
swift-mode:basic-offset))
;; After "in" for "for" statements
((equal previous-text "in")
;; Aligns with "in" if it is at the start of the line:
;;
;; for
;; x
;; in
;; foo
;;
;; for x
;; in
;; foo
;;
;; Otherwise, aligns with the next token of the "for".
;;
;; for x in
;; foo
;;
;; for
;; x in
;; foo
(goto-char (swift-mode:token:start previous-token))
(if (swift-mode:bol-other-than-comments-p)
(swift-mode:align-with-current-line)
(let ((parent (swift-mode:backward-sexps-until '("for" {))))
(swift-mode:align-with-next-token parent))))
;; After case ... : or default:
((eq previous-type 'case-:)
(goto-char (swift-mode:token:start previous-token))
(swift-mode:find-parent-and-align-with-next
swift-mode:statement-parent-tokens
(let ((relative-case-offset
(- swift-mode:basic-offset swift-mode:switch-case-offset)))
(if (<= relative-case-offset 0)
swift-mode:basic-offset
relative-case-offset))))
;; Before ; on the current line
((and next-is-on-current-line (eq next-type '\;))
(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-parent-and-align-with-next
swift-mode:statement-parent-tokens
swift-mode:multiline-statement-offset))
;; After attributes
((eq previous-type 'attribute)
(goto-char (swift-mode:token:end previous-token))
(swift-mode:backward-token-or-list)
(swift-mode:calculate-indent-of-expression
swift-mode:multiline-statement-offset
0
t))
;; Otherwise, it is continuation of the previous line
(t
(goto-char (swift-mode:token:end previous-token))
(swift-mode:backward-token-or-list)
(swift-mode:calculate-indent-of-expression
swift-mode:multiline-statement-offset)))))
(defun swift-mode:find-parent-and-align-with-next
(parents
&optional
offset
stop-at-eol-token-types
stop-at-bol-token-types
bol-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.
See `swift-mode:backward-sexps-until' for the details of
STOP-AT-EOL-TOKEN-TYPES and STOP-AT-BOL-TOKEN-TYPES.
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 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
stop-at-eol-token-types
stop-at-bol-token-types))
(parent-end (swift-mode:token:end parent))
(stopped-at-parent
(or (memq (swift-mode:token:type parent) parents)
(member (swift-mode:token:text parent) parents)
(eq (swift-mode:token:type parent) 'outside-of-buffer)))
(stopped-at-eol
(and
(not stopped-at-parent)
stop-at-eol-token-types
(or
(eq stop-at-eol-token-types 'any)
(memq (swift-mode:token:type parent)
stop-at-eol-token-types)
(member (swift-mode:token:text parent)
stop-at-eol-token-types)))))
(if stopped-at-parent
(swift-mode:align-with-next-token parent 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
(&optional
offset
bol-offset
after-attributes)
"Return indentation of the current expression.
the cursor is assumed to be on the previous line.
OFFSET is the offset. If it is omitted, assumed to be 0.
If scanning stops at eol, align with the next token with BOL-OFFSET.
If AFTER-ATTRIBUTES is nil, skip lines with only attributes at the start of
the expression."
(save-excursion
(let* ((parent-of-previous-line
(save-excursion
(swift-mode:goto-non-comment-bol-with-same-nesting-level)
(swift-mode:backward-token)))
(parent (swift-mode:find-parent-of-expression)))
(when (not after-attributes)
(goto-char (swift-mode:token:end parent))
(swift-mode:forward-attributes)
(swift-mode:goto-non-comment-bol-with-same-nesting-level)
(when (< (point) (swift-mode:token:end parent))
(goto-char (swift-mode:token:end parent)))
(setq parent (swift-mode:backward-token)))
;; When indenting a token after an attribute at the start of the
;; expression, aligns with it.
(when (and after-attributes
(save-excursion
(goto-char (swift-mode:token:end parent))
(forward-comment (point-max))
(eq (swift-mode:token:type (swift-mode:forward-token))
'attribute)))
(setq offset 0))
(if (<= (swift-mode:token:start parent-of-previous-line)
(swift-mode:token:start parent))
;; let x =
;; 1 + // here
;; 2 +
;; 3
;;
;; Aligns with the parent of the expression with offset.
(swift-mode:align-with-next-token parent offset)
;; let x =
;; 1 +
;; 2 + // here
;; 3 // or here
;;
;; Aligns with the previous line.
(swift-mode:align-with-next-token parent-of-previous-line
bol-offset)))))
(defun swift-mode:forward-attributes ()
"Skip forward comments, whitespaces, and attributes."
(while
(not
(eq (point)
(progn
(forward-comment (point-max))
(when (eq (char-after) ?@)
(swift-mode:forward-token-simple))
(point))))))
(defun swift-mode:calculate-indent-before-else (&optional offset)
"Return indentation before \"else\" token.
Assuming the cursor is before \"else\".
OFFSET is extra offset if given."
;; let x = if x { 1 }
;; else if y { 2 }
;; else { 3 }
;;
;; let x =
;; if x { 1 }
;; else if y { 2 }
;; else { 3 }
;;
;; let a = if x { 1 }
;; else
;; if y { 2 }
;; else { 3 }
;;
;; let a =
;; if x { 1 }
;; else
;; if y { 2 }
;; else { 3 }
(let ((parent (swift-mode:backward-sexps-until
(append
(remove 'implicit-\; swift-mode:statement-parent-tokens)
'("if" "guard")))))
(if (equal (swift-mode:token:text parent) "if")
(cond
;; Found "if" at the beginning of a line. Align with it.
;;
;; let a =
;; if x { 1 }
;; else
;; if y { 2 }
;; else { 3 }
((swift-mode:bol-other-than-comments-p)
(swift-mode:align-with-current-line offset))
;; Found "else if".
;;
;; let x =
;; if x { 1 }
;; else if y { 2 }
;; else { 3 }
;;
;; let x =
;; if x { 1 } else if y { 2 }
;; else { 3 }
((equal (swift-mode:token:text (save-excursion
(swift-mode:backward-token)))
"else")
(swift-mode:backward-token)
(if (swift-mode:bol-other-than-comments-p)
(swift-mode:align-with-current-line offset)
(swift-mode:calculate-indent-before-else offset)))
;; let x = if x { 1 }
;; else { 2 }
(t
(swift-mode:calculate-indent-of-expression
(or offset swift-mode:multiline-statement-offset))))
(swift-mode:align-with-current-line offset))))
(defun swift-mode:calculate-indent-for-curly-bracket (offset)
"Return indentation relating to curly brackets.
It is used for indentation after open curly brackets and for close brackets.
Assuming the cursor is on the open bracket.
OFFSET is the offset of the contents."
;; If the statement is multiline expression, aligns with the start of
;; the line on which the open bracket is:
;;
;; foo()
;; .then { x in
;; foo()
;; foo()
;; }
;; .then { x in
;; foo()
;; foo()
;; }
;;
;; rather than the start of the statement:
;;
;; foo()
;; .then { x in
;; foo()
;; foo()
;; }
;; .then { x in
;; foo()
;; foo()
;; }
;;
;; Otherwise, aligns with the start of the whole statement:
;;
;; for x in
;; xs
;; .foo() {
;; }
;;
;; rather than
;;
;; for x in
;; xs
;; .foo() {
;; }
;;
;; Note that curly bracket after binary operator is a part of
;; a multiline expression: