-
Notifications
You must be signed in to change notification settings - Fork 2
/
swift3-mode-indent.el
1475 lines (1348 loc) · 46.8 KB
/
swift3-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
;;; swift3-mode-indent.el --- Major-mode for Apple's Swift programming language, indentation. -*- lexical-binding: t -*-
;; Copyright (C) 2014-2016 taku0, Chris Barrett, Bozhidar Batsov, Arthur Evstifeev
;; Authors: taku0 (http://github.com/taku0)
;; Chris Barrett <chris.d.barrett@me.com>
;; Bozhidar Batsov <bozhidar@batsov.com>
;; Arthur Evstifeev <lod@pisem.net>
;;
;; Version: 2.1.1
;; Package-Requires: ((emacs "24.4"))
;; Keywords: languages swift
;; 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 'swift3-mode-lexer)
;;;###autoload
(defcustom swift3-mode:basic-offset 4
"Amount of indentation for block contents."
:type 'integer
:group 'swift3
:safe 'integerp)
;;;###autoload
(defcustom swift3-mode:parenthesized-expression-offset 2
"Amount of indentation inside parentheses and square brackets."
:type 'integer
:group 'swift3
:safe 'integerp)
;;;###autoload
(defcustom swift3-mode:multiline-statement-offset 2
"Amount of indentation for continuations of expressions."
:type 'integer
:group 'swift3
:safe 'integerp)
;;;###autoload
(defcustom swift3-mode:switch-case-offset 0
"Amount of indentation for case labels in switch statements."
:type 'integer
:group 'swift3
:safe 'integerp)
;;;###autoload
(defcustom swift3-mode:insert-space-after-asterisk-in-comment t
"Automatically insert a space after asterisk in comment if non-nil."
:type 'boolean
:group 'swift3
:safe 'booleanp)
;;;###autoload
(defcustom swift3-mode:auto-close-multiline-comment t
"If non-nil, `indent-new-comment-line' automatically close multiline comment."
:type 'boolean
:group 'swift3
:safe 'booleanp)
;;;###autoload
(defcustom swift3-mode:fix-comment-close t
"Fix \"* /\" in incomplete multiline comment to \"*/\" if non-nil."
:type 'boolean
:group 'swift3
:safe 'booleanp)
(defconst swift3-mode:statement-parent-tokens
'(implicit-\; \; case-: { \( \[ anonymous-function-parameter-in)
"Parent tokens for statements.")
(defconst swift3-mode:expression-parent-tokens
(append swift3-mode:statement-parent-tokens
'(\, < "where" "if" "guard" "while"))
"Parent tokens for expressions.")
(defun swift3-mode:indent-line ()
(let ((indent (save-excursion (swift3-mode:calculate-indent)))
(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 indent)
;; Keeps current relative position.
(save-excursion (indent-line-to indent)))))
(defun swift3-mode:calculate-indent ()
(back-to-indentation)
(if (nth 4 (syntax-ppss))
;; 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`.
(swift3-mode:calculate-indent-of-multiline-comment)
(swift3-mode:calculate-indent-of-code)))
(defun swift3-mode:calculate-indent-of-multiline-comment ()
(back-to-indentation)
(let ((comment-beginning-position (nth 8 (syntax-ppss))))
(forward-line -1)
(back-to-indentation)
(if (<= (point) comment-beginning-position)
;; The cursor was on the 2nd line of the comment, so aligns with
;; the asterisk of the comment starter.
(progn
(goto-char comment-beginning-position)
(forward-char)
(current-column))
;; The cursor was on the 3rd or following lines of the comment, so aligns
;; with a non-empty preceding line.
(if (eolp)
;; The cursor is on an empty line, so seeks a non-empty-line.
(swift3-mode:calculate-indent-of-multiline-comment)
(current-column)))))
(defun swift3-mode:calculate-indent-of-code ()
(back-to-indentation)
(let* ((previous-token (save-excursion (swift3-mode:backward-token)))
(previous-type (swift3-mode:token:type previous-token))
(previous-text (swift3-mode:token:text previous-token))
(next-token (save-excursion (swift3-mode:forward-token)))
(next-type (swift3-mode:token:type next-token))
(next-text (swift3-mode:token:text next-token))
(next-is-on-same-line
(<= (swift3-mode:token:end next-token) (line-end-position))))
(cond
;; Beginning of the buffer
((eq previous-type 'outside-of-buffer)
0)
;; Before } on the same line
((and next-is-on-same-line (eq next-type '}))
(goto-char (swift3-mode:token:end next-token))
(backward-list)
(swift3-mode:calculate-indent-after-open-curly-brace 0))
;; Before ) or ] on the same line
((and next-is-on-same-line (memq next-type '(\) \])))
(goto-char (swift3-mode:token:end next-token))
(backward-list)
(swift3-mode:calculate-indent-of-expression
swift3-mode:expression-parent-tokens
0
;; Stops scanning at BOL:
;;
;; foo
;; .bar(
;; 1
;; )
;;
;; rather than
;;
;; foo
;; .bar(
;; 1
;; )
'any))
;; Before , on the same line
((and next-is-on-same-line (eq next-type '\,))
(swift3-mode:calculate-indent-of-prefix-comma))
;; After ,
((eq previous-type '\,)
(goto-char (swift3-mode:token:start previous-token))
(swift3-mode:calculate-indent-after-comma))
;; Before "in" on the same line
((and next-is-on-same-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
;; }
(swift3-mode:calculate-indent-of-expression '("for" {)))
;; Before "case" or "default" on the same line, for switch statement
((and
next-is-on-same-line
(member next-text '("case" "default"))
(save-excursion
(equal (swift3-mode:token:text
(swift3-mode:backward-sexps-until
'("switch" "enum" "for" "while" "if" "guard")))
"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 aligh with it with offset.
(let ((parent (swift3-mode:backward-sexps-until
'("switch") nil '("case" "default"))))
(if (equal (swift3-mode:token:text parent) "switch")
;; Inside a switch-statement. Aligns with the "switch"
(swift3-mode:calculate-indent-of-expression
swift3-mode:statement-parent-tokens
swift3-mode:switch-case-offset)
;; Other cases. Aligns with the previous case.
(swift3-mode:align-with-current-line))))
;; Before "where" on the same line
((and next-is-on-same-line (equal next-text "where"))
;; switch {
;; case let P(x)
;; where
;; a,
;; let Q(x)
;; where
;; a:
;; aaa
;; }
;;
;; for case (x, y) in xys
;; where
;; aaa {
;; }
;;
;; do {
;; } catch let P(x)
;; where
;; aaa
;;
;; func foo<A: AAA,
;; B: BBB
;; where
;; ABC>() {
;; }
;;
;; class Foo<A,
;; B,
;; C>: AAA,
;; BBB,
;; CCC
;; where
;; ABC {
;; }
(let ((parent (save-excursion (swift3-mode:backward-sexps-until
(append swift3-mode:statement-parent-tokens
'("case"))))))
(swift3-mode:calculate-indent-of-expression
(append swift3-mode:statement-parent-tokens
'(< "case" "catch" "for")
(if (equal (swift3-mode:token:text parent) "case") '(\,) '()))
swift3-mode:multiline-statement-offset)))
;; After {
((eq previous-type '{)
(goto-char (swift3-mode:token:start previous-token))
(swift3-mode:calculate-indent-after-open-curly-brace
swift3-mode:basic-offset))
;; After ( or [
((memq previous-type '(\( \[))
(goto-char (swift3-mode:token:start previous-token))
(swift3-mode:calculate-indent-of-expression
swift3-mode:expression-parent-tokens
swift3-mode:parenthesized-expression-offset
;; Stops scanning at BOL:
;;
;; foo
;; .bar(
;; 1
;; )
;;
;; rather than
;;
;; foo
;; .bar(
;; 1
;; )
'any
nil
swift3-mode:parenthesized-expression-offset))
;; After "where"
((equal previous-text "where")
;; switch {
;; case let P(x) where
;; A,
;; let Q(x) where
;; A:
;; aaa
;; case let P(x)
;; where
;; a,
;; let Q(x)
;; where
;; a:
;; aaa
;; case let P(x), let Q(x) where
;; a
;; }
;;
;; for case let (x, y) in xys where
;; aaa {
;; }
;;
;; for case let (x, y) in xys
;; where
;; aaa {
;; }
;;
;; do {
;; } catch let P(x) where
;; aaa
;; do {
;; } catch let P(x)
;; where
;; aaa
;;
;;
;;
;; func foo<A: AAA,
;; B: BBB where
;; ABC>() {
;; }
;;
;; func foo<A: AAA,
;; B: BBB
;; where
;; ABC>() {
;; }
;;
;; class Foo<A,
;; B,
;; C> A,
;; B,
;; C where
;; ABC {
;; }
;;
;; class Foo<A,
;; B,
;; C>: A,
;; B,
;; C
;; where
;; ABC {
;; }
(goto-char (swift3-mode:token:start previous-token))
(if (swift3-mode:bol-other-than-comments-p)
(swift3-mode:align-with-current-line
swift3-mode:multiline-statement-offset)
(let ((parent (save-excursion
(swift3-mode:backward-sexps-until
(append swift3-mode:statement-parent-tokens
'("case"))))))
(swift3-mode:calculate-indent-of-expression
(append swift3-mode:statement-parent-tokens
'(< "case" "catch" "for")
(if (equal (swift3-mode:token:text parent) "case") '(\,) '()))
swift3-mode:multiline-statement-offset))))
;; After implicit-\; or ;
((memq previous-type '(implicit-\; \;))
(goto-char (swift3-mode:token:start previous-token))
(swift3-mode:calculate-indent-of-expression
(remove '\; (remove 'implicit-\; swift3-mode:statement-parent-tokens))
0
'(implicit-\; \;)))
;; After "in" for anonymous function parameters
((eq previous-type 'anonymous-function-parameter-in)
(goto-char (swift3-mode:token:start previous-token))
(swift3-mode:backward-sexps-until '({))
(swift3-mode:calculate-indent-after-open-curly-brace
swift3-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 (swift3-mode:token:start previous-token))
(if (swift3-mode:bol-other-than-comments-p)
(swift3-mode:align-with-current-line)
(let ((parent (swift3-mode:backward-sexps-until '("for" {))))
(swift3-mode:align-with-next-token parent))))
;; After case ... : or default:
((eq previous-type 'case-:)
(goto-char (swift3-mode:token:start previous-token))
(swift3-mode:calculate-indent-of-expression
swift3-mode:statement-parent-tokens
swift3-mode:basic-offset))
;; Before ; on the same line
((and next-is-on-same-line (eq next-type '\;))
(swift3-mode:calculate-indent-of-expression
(remove '\; (remove 'implicit-\; swift3-mode:statement-parent-tokens))
0
'(implicit-\; \;)))
;; After if, guard, while
((member previous-text '("if" "guard" "while"))
(swift3-mode:calculate-indent-of-expression
swift3-mode:statement-parent-tokens
swift3-mode:multiline-statement-offset))
;; After attributes at the beginning of a statement, without arguments
((and
(string-prefix-p "@" previous-text)
(memq (save-excursion
(goto-char (swift3-mode:token:start previous-token))
(swift3-mode:token:type (swift3-mode:backward-token)))
swift3-mode:statement-parent-tokens))
;; Aligns with the attribute.
(goto-char (swift3-mode:token:start previous-token))
(swift3-mode:align-with-next-token (swift3-mode:backward-token)))
;; After attributes at the beginning of a statement, with arguments
((and
(eq previous-type '\))
(save-excursion
(backward-list)
(and
(string-prefix-p
"@"
(swift3-mode:token:text (swift3-mode:backward-token)))
(memq (swift3-mode:token:type (swift3-mode:backward-token))
swift3-mode:statement-parent-tokens))))
(backward-list)
(swift3-mode:backward-token)
(swift3-mode:align-with-next-token (swift3-mode:backward-token)))
;; Otherwise, it is continuation of the previous line
(t
(goto-char (swift3-mode:token:end previous-token))
(swift3-mode:backward-token-or-list)
(swift3-mode:calculate-indent-of-expression
swift3-mode:expression-parent-tokens
swift3-mode:multiline-statement-offset
'any)))))
(defun swift3-mode:calculate-indent-of-expression
(parents
&optional
offset
stop-at-eol-token-types
stop-at-bol-token-types
bol-offset)
"Return start column of the current expressions or statement plus OFFSET.
If OFFSET is omitted, it is assumed to be 0.
PARENTS is a list of token types that precedes an expression or a statement.
See `swift3-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 is the symbol `any', the cursor is assumed to be
on the previous line."
(save-excursion
(let* ((parent (swift3-mode:backward-sexps-until
parents
stop-at-eol-token-types
stop-at-bol-token-types))
(parent-end (swift3-mode:token:end parent))
(stopped-at-parent
(or (memq (swift3-mode:token:type parent) parents)
(member (swift3-mode:token:text parent) parents)
(eq (swift3-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 (swift3-mode:token:type parent)
stop-at-eol-token-types)
(member (swift3-mode:token:text parent)
stop-at-eol-token-types)))))
(when (or stopped-at-parent stopped-at-eol)
(goto-char parent-end)
(forward-comment (point-max)))
;; Now, the cursor is at the first token of the expression.
(if stopped-at-parent
;; The cursor is at the start of the entire expression.
;; Aligns with the start of the expression with offset.
(swift3-mode:align-with-next-token parent offset)
;; The cursor is at the middle of the expression.
;; Aligns with this line with bol-offset.
(swift3-mode:align-with-current-line bol-offset)))))
(defun swift3-mode:calculate-indent-after-open-curly-brace (offset)
"Return indentation after open curly braces.
Assuming the cursor is on the open parenthesis.
OFFSET is the offset of the contents.
This function is also used for close-curly-brace."
;; If the statement is multiline expression, aligns with the start of
;; the line on which the open brace is:
;;
;; foo()
;; .then { x in
;; foo()
;; foo()
;; }
;; .then { x in
;; foo()
;; foo()
;; }
;;
;; rather than
;;
;; 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 brace after binary operator is a part of
;; a multiline expression:
;;
;; for x in
;; xs
;; +++ { x in
;; // this is not the body of the for-statement.
;; } {
;; // The body of the for-statement.
;; }
(let ((pos (point))
next-token
is-declaration-or-control-statement-body)
(if (save-excursion
(eq (swift3-mode:token:type (swift3-mode:backward-token))
'binary-operator))
;; for x in
;; xs
;; +++ { x in
;; // this is not the body of the for statement.
;; } {
;; ...
;; }
(setq is-declaration-or-control-statement-body nil)
(save-excursion
(swift3-mode:backward-sexps-until swift3-mode:statement-parent-tokens)
(swift3-mode:forward-token)
(setq next-token (swift3-mode:forward-token-or-list))
(while (<= (point) pos)
(cond
((member
(swift3-mode:token:text next-token)
'("for" "while" "repeat" "if" "else" "defer" "do" "catch"
"get" "set" "willSet" "didSet" "func" "init" "subscript"
"enum" "struct" "class" "extension" "prefix" "postfix" "infix"
"precedencegroup"))
(setq is-declaration-or-control-statement-body t)
(goto-char (1+ pos)))
((and
(equal (swift3-mode:token:text next-token) "protocol")
(not (equal (swift3-mode:token:text
(save-excursion (swift3-mode:forward-token)))
"<")))
(setq is-declaration-or-control-statement-body t)
(goto-char (1+ pos)))
((equal (swift3-mode:token:text next-token) "var")
;; There are several cases:
;;
;; var foo = bar
;; .then { x in
;; x
;; }
;; .then { x in
;; x
;; }
;;
;; var foo = bar
;; .baz {
;; willSet {
;; ...
;; }
;; }
;;
;; var foo {
;; get {
;; ...
;; }
;; }
;;
;; Note that the 1st and the 2nd cases cannot be distinguished
;; without inspecting the contents of the block.
;; We indent the 2nd case like the 1st case for now.
;; Future implementation may use more sophisticated logic.
(goto-char pos)
(setq is-declaration-or-control-statement-body
(equal (swift3-mode:token:text
(swift3-mode:backward-sexps-until '("var" "=")))
"var"))
(goto-char (1+ pos)))
(t
;; Suppose indenting the A token below.
;;
;; foo {
;; A
;;
;; This function is called on the open curly brace.
;; If the close curly brace doesn't exist,
;; swift3-mode:forward-token-or-list results in
;; "Unbalanced parentheses" error.
;; So if the point is just before the open curly brace,
;; exits immediately.
(forward-comment (point-max))
(if (< (point) pos)
(setq next-token (swift3-mode:forward-token-or-list))
(goto-char (1+ pos))))))))
(swift3-mode:calculate-indent-of-expression
swift3-mode:statement-parent-tokens
offset
(if is-declaration-or-control-statement-body nil 'any)
nil
offset)))
(defun swift3-mode:calculate-indent-of-prefix-comma ()
"Return indentation for prefix comma.
Example:
let x = [ 1
, 2
, 3
]
class Foo: A
, B
, C
case A
, B
, C
:
var x = 1
, y = 2
, z = 3
This is also known as Utrecht-style in the Haskell community."
(let* ((comma-type-and-statement-parent (swift3-mode:detect-type-of-comma))
(comma-type (nth 0 comma-type-and-statement-parent))
(statement-parent (nth 1 comma-type-and-statement-parent)))
(if (eq comma-type 'condition-list)
(swift3-mode:calculate-indent-of-prefix-comma-of-condition-list
statement-parent)
(let* ((parents (swift3-mode:parents-of-comma comma-type))
(parent (swift3-mode:backward-sexps-until parents
nil
'(\,)))
(parent-end (swift3-mode:token:end parent))
(stopped-at-parent
(or (memq (swift3-mode:token:type parent) parents)
(member (swift3-mode:token:text parent) parents)
(eq (swift3-mode:token:type parent) 'outside-of-buffer))))
(if stopped-at-parent
(progn
;; Aligns with the end of the parent.
(goto-char parent-end)
(backward-char)
(current-column))
;; Aligns with the previous comma.
(swift3-mode:align-with-current-line))))))
(defun swift3-mode:calculate-indent-of-prefix-comma-of-condition-list
(statement-parent)
;; The comma is in a condition-list but not in an enum-case-pattern-list.
;;
;; Example:
;;
;; if case let P(x)
;; , let Q(x) // comma for enum-case-pattern-list
;; , let R(x) = a // comma for enum-case-pattern-list
;; , let x = x // comma for condition-list
;; , foo == bar // comma for condition-list
;;
;; We scan from beginning of the statement and remembers last anchor token,
;; i.e. "if", "guard", "while", or comma at the beginning of the line.
(let ((pos (point))
next-token
(anchor statement-parent)
in-case-pattern-list)
(goto-char (swift3-mode:token:end statement-parent))
(setq next-token (swift3-mode:forward-token-or-list))
(while (< (point) pos)
(cond
((equal (swift3-mode:token:text next-token) "case")
(setq in-case-pattern-list t))
((equal (swift3-mode:token:text next-token) "=")
(setq in-case-pattern-list nil))
((member (swift3-mode:token:text next-token) '("if" "guard" "while"))
(setq anchor next-token))
((and (not in-case-pattern-list)
(eq (swift3-mode:token:type next-token) '\,)
(save-excursion
(goto-char (swift3-mode:token:start next-token))
(swift3-mode:bol-other-than-comments-p)))
(setq anchor next-token)))
(setq next-token (swift3-mode:forward-token-or-list)))
(if (eq (swift3-mode:token:type anchor) '\,)
;; Aligns with the previous comma.
(progn
(goto-char (swift3-mode:token:start anchor))
(swift3-mode:align-with-current-line))
;; Aligns with the end of the anchor
(goto-char (swift3-mode:token:end anchor))
(backward-char)
(current-column))))
(defun swift3-mode:calculate-indent-after-comma ()
"Return indentation after comma.
Assuming the cursor is on the comma."
(let* ((comma-type-and-statement-parent (swift3-mode:detect-type-of-comma))
(comma-type (nth 0 comma-type-and-statement-parent))
(statement-parent (nth 1 comma-type-and-statement-parent)))
(if (eq comma-type 'condition-list)
(swift3-mode:calculate-indent-after-comma-of-condition-list
statement-parent)
(swift3-mode:align-with-next-token
(swift3-mode:backward-sexps-until
(swift3-mode:parents-of-comma comma-type)
'(\,))))))
(defun swift3-mode:calculate-indent-after-comma-of-condition-list
(statement-parent)
;; The comma is in a condition-list but not in an enum-case-pattern-list.
;;
;; Example:
;;
;; if
;; case let P(x), // comma for enum-case-pattern-list
;; let Q(x), // comma for enum-case-pattern-list
;; let R(x) = a, // comma for condition-list
;; let x = x, // comma for condition-list
;; foo == bar
;;
;; We scan from beginning of the statement and remembers last parent token,
;; i.e. "if", "guard", "while", or comma at the end of the line.
(let ((pos (point))
next-token
(parent statement-parent)
in-case-pattern-list)
(goto-char (swift3-mode:token:end statement-parent))
(setq next-token (swift3-mode:forward-token-or-list))
(while (< (point) pos)
(cond
((equal (swift3-mode:token:text next-token) "case")
(setq in-case-pattern-list t))
((equal (swift3-mode:token:text next-token) "=")
(setq in-case-pattern-list nil))
((member (swift3-mode:token:text next-token) '("if" "guard" "while"))
(setq parent next-token))
((and (not in-case-pattern-list)
(eq (swift3-mode:token:type next-token) '\,)
(swift3-mode:eol-other-than-comments-p))
(setq parent next-token)))
(setq next-token (swift3-mode:forward-token-or-list)))
(swift3-mode:align-with-next-token parent)))
(defun swift3-mode:detect-type-of-comma ()
"Return type of comma token under the cursor.
Comma type is a list where:
0th element is one of the following:
- tuple-or-array (inside () or [])
- type-parameters-or-requirements (inside <>)
- enum-case-pattern-list (e.g. if case P, Q, R = x)
- condition-list (e.g. if let x = x, let y = y)
- variable-declarations (e.g. let x = 1, y = 2)
- switch-case-or-enum-case-item-list (e.g. switch {case P, Q, R: a} or
enum {case A, B, C})
- class-like-declarations (supertypes of class, or where clause after
super types)
1st element is the token before the beginning of the statement.
"
;; Various examples:
;;
;; let x = ( // simple case
;; 1,
;; 2,
;; 3
;; )
;;
;; let x = [ // simple case
;; 1,
;; 2,
;; 3
;; ]
;;
;; let x: Foo<A, B> = a, // "let" is not a part of elements
;; y: Foo<A, B> = b,
;; z: Foo<A, B> = c
;;
;; switch foo {
;; case (let x, // "let" is a part of an element
;; Y,
;; Z):
;; aaa
;; }
;;
;; class Foo<A where A: B, // "where" is not a part of elements
;; A: C,
;; A: D> {
;; }
;;
;; switch foo {
;; case A(x) where p(x), // "case" is not a part of elements
;; B(x) where p(x), // "where" is a part of an element
;; C(x) where p(x):
;; aaa
;; }
;;
;; if // or guard or while
;; let x = x, // "let" is a part of an element
;; let y = y,
;; let z = z,
;; case P(a, b, c), // "case" is a part of an element of condition-list
;; Q(a, b, c) = abc, // "case" is not a part of elements of
;; // enum-case-pattern-list
;; case (a, b, c) = abc,
;; aaa {
;;
;; bbb
;; }
;;
;; See also SE-0099 and SE-0043:
;; https://github.com/apple/swift-evolution/blob/master/proposals/0099-conditionclauses.md
;; https://github.com/apple/swift-evolution/blob/master/proposals/0043-declare-variables-in-case-labels-with-multiple-patterns.md
;;
;; class Foo<T>: A,
;; B,
;; C
;; where
;; T: A,
;; T: B,
;; T: C {
;; }
;;
;; extension _ArrayType
;; where
;; Generator.Element: A,
;; Generator.Element: B,
;; Generator.Element: C {
;; }
;;
;; func foo<T> -> Int
;; where
;; T: A,
;; T: B,
;; T: C {
;; }
;;
;; enum Foo {
;; case A(x: Int),
;; B(y: Int),
;; C(z: Int)
;; case D(x: Int)
;; , E(y: Int)
;; , F(z: Int)
;; }
;;
;; enum Foo: Int {
;; case A,
;; B,
;; C = 2