-
Notifications
You must be signed in to change notification settings - Fork 43
/
kotlin-mode-indent.el
1951 lines (1721 loc) · 66.9 KB
/
kotlin-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
;;; kotlin-mode-indent.el --- Major mode for kotlin, indentation -*- lexical-binding: t; -*-
;; Copyright (C) 2019 taku0
;; Authors: taku0 (http://github.com/taku0)
;; 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 'rx)
(require 'cl-lib)
(require 'kotlin-mode-lexer)
;;; Customizations
(defcustom kotlin-tab-width 4
"Amount of indentation for block contents.
Example:
class Foo {
func foo() {} // offset of this line
}"
:type 'integer
:group 'kotlin
:safe 'integerp)
(defcustom kotlin-mode-parenthesized-expression-offset 4
"Amount of indentation inside parentheses and square brackets.
Example:
foo(
1 // offset of this line
)"
:type 'integer
:group 'kotlin
:safe 'integerp)
(defcustom kotlin-mode-multiline-statement-offset 4
"Amount of indentation for continuations of expressions.
Example:
val x = 1 +
2 // offset of this line"
:type 'integer
:group 'kotlin
:safe 'integerp)
(defcustom kotlin-mode-prepend-asterisk-to-comment-line t
"Automatically insert a asterisk to each comment line if non-nil.
Example: if the enter key is pressed when the point is after A below,
/*
* A
*/
an asterisk is inserted to the newline:
/*
* A
*
*/"
:type 'boolean
:group 'kotlin
:safe 'booleanp)
(defcustom kotlin-mode-insert-space-after-asterisk-in-comment t
"Automatically insert a space after asterisk in comment if non-nil.
Example: if an asterisk is inserted before A below,
/*
A
*/
a space is inserted after asterisk:
/*
* A
*/"
:type 'boolean
:group 'kotlin
:safe 'booleanp)
(defcustom kotlin-mode-auto-close-multiline-comment t
"If non-nil, `indent-new-comment-line' automatically close multiline comment.
Example: when the enter key is pressed after unclosed comment below,
/**
a closing delimiter is inserted automatically:
/**
* // cursor is here
*/"
:type 'boolean
:group 'kotlin
:safe 'booleanp)
(defcustom kotlin-mode-fix-comment-close t
"Fix \"* /\" in incomplete multiline comment to \"*/\" if non-nil.
Example:
/*
*
* // when a slash is inserted here
/*
*
*/ // it become like this
/*
*
* / // rather than like this."
:type 'boolean
:group 'kotlin
:safe 'booleanp)
(defcustom kotlin-mode-break-line-before-comment-close t
"If non-nil, break line before the closing delimiter of multiline comments.
Example: if line break is inserted before A below,
/** A */
it becomes like this:
/**
* A
*/
rather than like this:
/**
* A */"
:type 'boolean
:group 'kotlin
:safe 'booleanp)
(defcustom kotlin-mode-indent-nonempty-line-in-multiline-string nil
"If non-nil, indent nonempty line in multiline string.
`indent-according-to-mode' is no-op otherwise."
:type 'boolean
:group 'kotlin
:safe 'booleanp)
(defcustom kotlin-mode-highlight-anchor nil
"Highlight anchor point for indentation if non-nil.
Intended for debugging."
:type 'boolean
:group 'kotlin
:safe 'booleanp)
;;; Constants and variables
(defconst kotlin-mode--statement-parent-tokens
'(\( \[ { anonymous-function-parameter-arrow when-expression-arrow
bare-else \(\)-before-control-structure-body \; implicit-\;)
"Parent tokens for statements.
Parent tokens are tokens before the beginning of statements.")
(defconst kotlin-mode--expression-parent-tokens
(append kotlin-mode--statement-parent-tokens
'(\, "where" string-chunk-before-template-expression))
"Parent tokens for expressions.
Parent tokens are tokens before the beginning of expressions.")
(defvar-local kotlin-mode--anchor-overlay nil)
(defvar-local kotlin-mode--anchor-overlay-timer nil)
;;; Indentation struct
(defclass kotlin-mode--indentation ()
((position :initarg :position
:type number
:accessor kotlin-mode--indentation-position
:documentation "the position of the anchor point, such as
the start of the previous line or the start of the class declaration.")
(offset :initarg :offset
:type number
:initform 0
:accessor kotlin-mode--indentation-offset
:documentation "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
`kotlin-tab-width'."))
"Indentation.")
;;; Indentation logic
(defun kotlin-mode--indent-line ()
"Indent the current line."
(let* ((indentation (save-excursion (kotlin-mode--calculate-indent)))
(indentation-column
(save-excursion
(goto-char (kotlin-mode--indentation-position indentation))
(+ (current-column) (kotlin-mode--indentation-offset indentation))))
(current-indent
(save-excursion (back-to-indentation) (current-column))))
(if (<= (current-column) current-indent)
;; The point is on the left margin.
;; Move the point to the new leftmost non-comment char.
(indent-line-to indentation-column)
;; Keep current relative position from leftmost non-comment char.
(save-excursion (indent-line-to indentation-column)))
(when kotlin-mode-highlight-anchor
(kotlin-mode-highlight-anchor indentation))))
(defun kotlin-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 point 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`.
(kotlin-mode--calculate-indent-of-multiline-comment))
((eq (nth 3 parser-state) t)
(kotlin-mode--calculate-indent-of-multiline-string))
((looking-at "//")
(kotlin-mode--calculate-indent-of-single-line-comment))
(t
(kotlin-mode--calculate-indent-of-code)))))
(defun kotlin-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
;; 2nd line of the comment
((<= (point) comment-beginning-position)
;; The point was on the 2nd line of the comment.
(goto-char comment-beginning-position)
(forward-char)
;; If there are extra characters or spaces after asterisks, align with
;; the first non-space character or end of line. Otherwise, align with
;; the first asterisk.
(when (and
(looking-at
(rx (seq (zero-or-more "*") (one-or-more (not (any "\n*"))))))
(not (and kotlin-mode-prepend-asterisk-to-comment-line
starts-with-asterisk)))
(skip-chars-forward "*")
(skip-syntax-forward " "))
(make-instance 'kotlin-mode--indentation :position (point) :offset 0))
;; The point was on the 3rd or following lines of the comment.
;; Before closing delimiter
((= (save-excursion
;; Back to the original line.
(forward-line)
(back-to-indentation)
(point))
(save-excursion
;; Goto just before the closing delimiter.
(goto-char comment-beginning-position)
(if (forward-comment 1)
(progn
;; slash
(backward-char)
(skip-chars-backward "*")
(point))
-1)))
;; Before the closing delimiter. Align with the first asterisk of the
;; opening delimiter.
;;
;; TODO: If there are multiple asterisks on the closing
;; delimiter, and the middle lines have no leading asterisks,
;; align with the slash of the opening delimiter
;;
;; Example:
;;
;; /*********
;; aaa
;; **********/
(goto-char comment-beginning-position)
(forward-char)
(make-instance 'kotlin-mode--indentation :position (point) :offset 0))
;; Otherwise, align with a non-empty preceding line.
;; The previous line is empty
((and (bolp) (eolp))
;; Seek a non-empty-line.
(while (and (bolp) (eolp) (not (bobp)))
(forward-line -1))
(forward-line)
(kotlin-mode--calculate-indent-of-multiline-comment))
;; The previous line is not empty
(t
;; Align to this line.
(make-instance 'kotlin-mode--indentation :position (point) :offset 0)))))
(defun kotlin-mode--calculate-indent-of-multiline-string ()
"Return the indentation of the current line inside a multiline string."
(back-to-indentation)
(let ((string-beginning-position
(save-excursion (kotlin-mode--beginning-of-string))))
(if (looking-at "\"\"\"")
;; Indenting the closing delimiter. Align with the start of containing
;; expression with extra indentation.
;;
;; Example:
;;
;; val someString = """
;; Hello.
;; """
;;
;; When aligning to opening delimiter, align without offset:
;; foo("""
;; aaa
;; """)
;;
;; Possible alternative indentation:
;; val someString = """
;; Hello.
;; """
(progn
(goto-char string-beginning-position)
(let ((indentation
(kotlin-mode--calculate-indent-of-expression
kotlin-mode-multiline-statement-offset)))
(if (= (kotlin-mode--indentation-position indentation)
string-beginning-position)
(make-instance 'kotlin-mode--indentation
:position string-beginning-position
:offset 0)
indentation)))
;; Other than the closing delimiter.
(if (and (not (eolp))
(not kotlin-mode-indent-nonempty-line-in-multiline-string))
;; The user prefers to keep indentations inside multiline string.
(make-instance 'kotlin-mode--indentation :position (point) :offset 0)
;; The user prefers to indent lines inside multiline string.
(beginning-of-line)
(backward-char)
(kotlin-mode--goto-non-template-expression-bol)
(back-to-indentation)
(cond
;; 2nd line of string
((<= (point) string-beginning-position)
;; The point was on the 2nd line of the string. Align
;; with the start of containing expression with extra
;; indentation.
(goto-char string-beginning-position)
(kotlin-mode--calculate-indent-of-expression
kotlin-mode-multiline-statement-offset))
;; The point was on the 3rd or following lines of the string.
;; Align with a non-empty preceding line.
;; The previous line is empty
((and (bolp) (eolp))
;; Seek a non-empty-line.
(while (and (bolp) (eolp) (not (bobp)))
(forward-line -1))
(forward-line)
(kotlin-mode--calculate-indent-of-multiline-string))
;; The previous line is not empty
(t
;; Align to this line.
(make-instance 'kotlin-mode--indentation
:position (point)
:offset 0)))))))
(defun kotlin-mode--goto-non-template-expression-bol ()
"Back to the beginning of line that is not inside a template expression."
(let ((chunk-beginning-position (nth 8 (syntax-ppss)))
(matching-bracket t))
(while (and matching-bracket
(< (line-beginning-position) chunk-beginning-position))
(setq matching-bracket
(get-text-property
chunk-beginning-position 'kotlin-property--matching-bracket))
(when matching-bracket
(goto-char matching-bracket)
(setq chunk-beginning-position (nth 8 (syntax-ppss)))))
(beginning-of-line)))
(defun kotlin-mode--calculate-indent-of-single-line-comment ()
"Return the indentation of the current line inside a single-line comment."
(cond
;; When the comment is beginning of the buffer, indent to the column 0.
((save-excursion
(beginning-of-line)
(bobp))
(make-instance 'kotlin-mode--indentation :position (point-min) :offset 0))
;; If the previous line is also a single-line comment, align with it.
((save-excursion
(forward-line -1)
(skip-syntax-forward " ")
(looking-at "//"))
(forward-line -1)
(skip-syntax-forward " ")
(make-instance 'kotlin-mode--indentation :position (point) :offset 0))
;; Otherwise, indent like a expression.
(t
(kotlin-mode--calculate-indent-of-code))))
(defun kotlin-mode--calculate-indent-of-code ()
"Return the indentation of the current line outside comments or string."
(back-to-indentation)
(let* ((previous-token (save-excursion (kotlin-mode--backward-token)))
(previous-type (kotlin-mode--token-type previous-token))
(previous-text (kotlin-mode--token-text previous-token))
(next-token (save-excursion (kotlin-mode--forward-token)))
(next-type (kotlin-mode--token-type next-token))
(next-text (kotlin-mode--token-text next-token))
(next-is-on-current-line
(<= (kotlin-mode--token-start next-token) (line-end-position))))
(cond
;; Beginning of the buffer
((eq previous-type 'outside-of-buffer)
(make-instance 'kotlin-mode--indentation :position (point-min) :offset 0))
;; Before } on the current line.
;; Align with the head of the statement.
((and next-is-on-current-line (eq next-type '}))
(goto-char (kotlin-mode--token-end next-token))
(backward-list)
(kotlin-mode--calculate-indent-after-open-curly-brace 0))
;; Before ) or ] on the current line.
;; Align with the head of the expression.
((and next-is-on-current-line
(memq next-type '(\) \)-before-control-structure-body \])))
(goto-char (kotlin-mode--token-end next-token))
(backward-list)
(kotlin-mode--calculate-indent-of-expression 0))
;; Before close angle bracket for generics (>) on the current line.
((and next-is-on-current-line
(equal next-text ">")
(save-excursion
(goto-char (kotlin-mode--token-end next-token))
(eq (kotlin-mode--token-type (kotlin-mode--backward-token-or-list))
'<>)))
(goto-char (kotlin-mode--token-end next-token))
(kotlin-mode--backward-token-or-list)
(kotlin-mode--calculate-indent-of-expression 0))
;; Before end of a template expression on the current line.
((and next-is-on-current-line
(eq next-type 'string-chunk-after-template-expression))
(goto-char (get-text-property
(kotlin-mode--token-start next-token)
'kotlin-property--matching-bracket))
;; Skip "${"
(forward-char 2)
(kotlin-mode--backward-string-chunk)
(kotlin-mode--calculate-indent-after-beginning-of-template-expression
0))
;; Before { on the current line
((and next-is-on-current-line (eq next-type '{))
(kotlin-mode--calculate-indent-after-open-curly-brace 0))
;; Before "else" on the current line
((and next-is-on-current-line (equal next-text "else"))
(kotlin-mode--calculate-indent-of-else))
;; Before "where" on the current line
((and next-is-on-current-line (equal next-text "where"))
(kotlin-mode--find-parent-and-align-with-next
kotlin-mode--statement-parent-tokens
kotlin-mode-multiline-statement-offset))
;; Before "catch" or "finally" on the current line
((and next-is-on-current-line (member next-text '("catch" "finally")))
(kotlin-mode--find-parent-and-align-with-next
kotlin-mode--statement-parent-tokens
0
'()
'("catch" "finally")
0))
;; Before "=" on the current line
;;
;; fun <T> foo(): T
;; where
;; T: A,
;; T: B
;; = bar()
((and next-is-on-current-line (equal next-text "="))
(kotlin-mode--find-parent-and-align-with-next
(remove '\, (remove "where" kotlin-mode--expression-parent-tokens))
kotlin-mode-multiline-statement-offset))
;; Before "," on the current line
((and next-is-on-current-line (eq next-type '\,))
(kotlin-mode--calculate-indent-of-prefix-comma))
;; After ","
((eq previous-type '\,)
(goto-char (kotlin-mode--token-start previous-token))
(kotlin-mode--calculate-indent-after-comma))
;; After "catch". Align with "try".
((equal previous-text "catch")
(kotlin-mode--find-parent-and-align-with-next
kotlin-mode--statement-parent-tokens
kotlin-mode-multiline-statement-offset))
;; After {
((eq previous-type '{)
(goto-char (kotlin-mode--token-start previous-token))
(kotlin-mode--calculate-indent-after-open-curly-brace
kotlin-tab-width))
;; After ( or [
((memq previous-type '(\( \[))
(goto-char (kotlin-mode--token-start previous-token))
(kotlin-mode--calculate-indent-of-expression
kotlin-mode-parenthesized-expression-offset
kotlin-mode-parenthesized-expression-offset))
;; After open angle bracket for generics (<)
((and (equal previous-text "<")
(save-excursion
(goto-char (kotlin-mode--token-start previous-token))
(eq (kotlin-mode--token-type (kotlin-mode--forward-token-or-list))
'<>)))
(goto-char (kotlin-mode--token-start previous-token))
(kotlin-mode--calculate-indent-of-expression
kotlin-mode-parenthesized-expression-offset
kotlin-mode-parenthesized-expression-offset))
;; After beginning of a template expression
((eq previous-type 'string-chunk-before-template-expression)
(goto-char (kotlin-mode--token-start previous-token))
(kotlin-mode--calculate-indent-after-beginning-of-template-expression
kotlin-mode-parenthesized-expression-offset))
;; After ; or implicit-\;
((memq previous-type '(\; implicit-\;))
(goto-char (kotlin-mode--token-start previous-token))
(kotlin-mode--calculate-indent-after-semicolon))
;; After "->" of lambda parameters
((eq previous-type 'anonymous-function-parameter-arrow)
(goto-char (cdr (kotlin-mode--find-containing-brackets
(kotlin-mode--token-start previous-token))))
(kotlin-mode--calculate-indent-after-open-curly-brace
kotlin-tab-width))
;; Before "->" of lambda parameters on the current line
((and next-is-on-current-line
(eq next-type 'anonymous-function-parameter-arrow))
(if (eq (kotlin-mode--token-type (kotlin-mode--backward-token-or-list))
'{)
(kotlin-mode--calculate-indent-after-open-curly-brace
kotlin-tab-width)
(goto-char (cdr (kotlin-mode--find-containing-brackets
(kotlin-mode--token-start next-token))))
(kotlin-mode--align-with-next-token (kotlin-mode--forward-token))))
;; After "->" of when expression
((eq previous-type 'when-expression-arrow)
(goto-char (kotlin-mode--token-start previous-token))
(kotlin-mode--find-parent-and-align-with-next
(cl-remove-if
(lambda (e)
(memq e '(when-expression-arrow
bare-else \(\)-before-control-structure-body)))
kotlin-mode--statement-parent-tokens)
kotlin-tab-width))
;; Before "->" of when expression on the current line
((and next-is-on-current-line (eq next-type 'when-expression-arrow))
(if (equal (kotlin-mode--token-text previous-token) "else")
(kotlin-mode--align-with-token
previous-token
kotlin-tab-width)
(kotlin-mode--find-parent-and-align-with-next
(cl-remove-if
(lambda (e)
(memq e '(when-expression-arrow
bare-else \(\)-before-control-structure-body)))
kotlin-mode--statement-parent-tokens)
kotlin-tab-width)))
;; After "where"
;;
;; class Foo<T>
;; where
;; T: A // align with "where" with offset
;;
;; class Foo<T> where
;; T: A // align with "class" with offset
((equal previous-text "where")
(goto-char (kotlin-mode--token-start previous-token))
(if (kotlin-mode--bol-other-than-comments-p)
(kotlin-mode--align-with-token
previous-token
kotlin-mode-multiline-statement-offset)
(kotlin-mode--find-parent-and-align-with-next
kotlin-mode--statement-parent-tokens
kotlin-mode-multiline-statement-offset)))
;; After if, when, for, or while
((member previous-text '("if" "when" "for" "while"))
(kotlin-mode--find-parent-and-align-with-next
kotlin-mode--statement-parent-tokens
kotlin-mode-multiline-statement-offset))
;; After do
((equal previous-text "do")
(kotlin-mode--align-with-token
previous-token
(if (equal next-text "while") 0 kotlin-tab-width)))
;; After else
((equal previous-text "else")
(kotlin-mode--align-with-token
previous-token
kotlin-tab-width))
;; After "if ()", "while ()", or "for ()"
((eq previous-type '\)-before-control-structure-body)
(kotlin-mode--backward-token-or-list)
(kotlin-mode--find-parent-and-align-with-next
kotlin-mode--statement-parent-tokens
kotlin-tab-width
'()
'("else")
kotlin-tab-width))
;; Before ; on the current line
((and next-is-on-current-line (eq next-type '\;))
(kotlin-mode--find-parent-and-align-with-next
(remove '\; (remove 'implicit-\; kotlin-mode--statement-parent-tokens))
0
'(\; implicit-\;)))
;; Before "in" on the current line
((and next-is-on-current-line (equal next-text "in"))
(kotlin-mode--calculate-indent-before-in))
;; After "in"
;;
;; Examples:
;; for (
;; x
;; in
;; xs
;; ) {}
;;
;; for (x
;; in
;; xs) {[]}
;;
;; when (x) {
;; in
;; xs -> y in
;; ys
;; in
;; zs -> 1
;; }
;;
;; Foo<
;; in
;; X,
;; @AAA in
;; X
;; >
;;
;; val x = 1 in
;; array
((equal previous-text "in")
(let* ((type-and-parent
(save-excursion
(goto-char (kotlin-mode--token-start previous-token))
(kotlin-mode--find-parent-of-in)))
(type (car type-and-parent)))
(if (or (memq type '(for <>))
(save-excursion
(goto-char (kotlin-mode--token-start previous-token))
(not (kotlin-mode--bol-other-than-comments-p))))
;; for-statement and type parameters, or "in" is not at
;; the beginning of the line.
(progn
;; Indent like a expression
(goto-char (kotlin-mode--token-start previous-token))
(kotlin-mode--calculate-indent-of-expression
kotlin-mode-multiline-statement-offset))
;; After "in" at the beginning of the line.
;;
;; Example:
;;
;; when (x) {
;; in
;; xs -> 1
;; in
;; ys -> 1
;; }
;;
;; Pretend a semicolon exists before "in"
(goto-char (kotlin-mode--token-start previous-token))
(kotlin-mode--align-with-next-token
(kotlin-mode--backward-token)
kotlin-mode-multiline-statement-offset))))
;; Before "while" on the current line
((and next-is-on-current-line (equal next-text "while"))
(let ((do-token (save-excursion (kotlin-mode--find-do-for-while))))
(if do-token
(kotlin-mode--align-with-token do-token)
(kotlin-mode--calculate-indent-after-semicolon))))
;; Inside annotation
((or (and (eq previous-type 'annotation)
(or
;; @file
;; : // ← here
(eq next-type ':)
;; @file:A
;; .B // ← here
;; @file:A
;; <A> // ← here
(member next-text '("." "<"))))
;; @file:
;; [ // ← here
(and (eq previous-type ':)
(eq next-type '\[)
(save-excursion
(goto-char (kotlin-mode--token-start previous-token))
(eq (kotlin-mode--token-type (kotlin-mode--backward-token))
'annotation)))
;; @file:
;; A // ← here
;; @file:A
;; .
;; B // ← here
;; @file:A
;; <
;; B
;; > // ← here
(and (or (eq next-type 'atom) (equal next-text ">"))
(eq (kotlin-mode--token-type
(save-excursion
(kotlin-mode--extend-annotation-token-backward
next-token)))
'annotation)))
(let ((start-of-annotation
(cond
((eq previous-type 'annotation)
(kotlin-mode--token-start previous-token))
((eq previous-type ':)
(save-excursion
(goto-char (kotlin-mode--token-start previous-token))
(kotlin-mode--token-start (kotlin-mode--backward-token))))
(t (kotlin-mode--token-start
(save-excursion (kotlin-mode--extend-annotation-token-backward
next-token))))))
(start-of-previous-line
(save-excursion
(kotlin-mode--backward-token-or-list t)
(kotlin-mode--goto-non-comment-bol-with-same-nesting-level t)
(point))))
(goto-char (max start-of-annotation start-of-previous-line))
(kotlin-mode--align-with-current-line
(if (< start-of-annotation start-of-previous-line)
;; 3rd or following lines of the annotation.
;; Align with previous line without offset.
0
;; 2nd line of the annotation.
;; Align with the start of the annotation with offset.
kotlin-mode-multiline-statement-offset))))
;; After annotations or labels
((memq previous-type '(annotation label))
(goto-char (kotlin-mode--token-start previous-token))
;; Align with preceding annotation or label at the beginning of
;; a line, or indent like an expression if the annotations are in
;; middle of a line.
(let ((token previous-token))
(while (and (memq (kotlin-mode--token-type token) '(annotation label))
(not (kotlin-mode--bol-other-than-comments-p)))
(setq token (kotlin-mode--backward-token)))
(unless (memq (kotlin-mode--token-type token) '(annotation label))
;; The annotations are in middle of a line.
(goto-char (kotlin-mode--token-end token))))
(if (kotlin-mode--bol-other-than-comments-p)
;; The annotations are at the beginning of a line.
(kotlin-mode--align-with-current-line)
;; The annotations are in middle of a line.
(kotlin-mode--align-with-next-token
(kotlin-mode--find-parent-of-expression))))
;; Otherwise, it is continuation of the previous line
(t
(goto-char (kotlin-mode--token-end previous-token))
(kotlin-mode--backward-token-or-list)
(kotlin-mode--calculate-indent-of-expression
kotlin-mode-multiline-statement-offset)))))
(defun kotlin-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 the expression or the statement.
OFFSET is the offset. If it is omitted, assumed to be 0.
See `kotlin-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 (kotlin-mode--backward-sexps-until
parents
stop-at-eol-token-types
stop-at-bol-token-types))
(parent-end (kotlin-mode--token-end parent))
(stopped-at-parent
(or (memq (kotlin-mode--token-type parent) parents)
(member (kotlin-mode--token-text parent) parents)
(eq (kotlin-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 (kotlin-mode--token-type parent)
stop-at-eol-token-types)
(member (kotlin-mode--token-text parent)
stop-at-eol-token-types)))))
(if stopped-at-parent
(kotlin-mode--align-with-next-token parent offset)
(when stopped-at-eol
(goto-char parent-end)
(forward-comment (point-max)))
(kotlin-mode--align-with-current-line bol-offset)))))
(defun kotlin-mode--calculate-indent-of-expression
(&optional
offset
bol-offset)
"Return indentation of the current expression.
the point 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."
(save-excursion
(let* ((pos (point))
(parent-of-previous-line
(save-excursion
(kotlin-mode--goto-non-comment-bol-with-same-nesting-level)
(kotlin-mode--backward-token)))
(parent-of-expression (kotlin-mode--find-parent-of-expression)))
;; Skip annotations
(goto-char (kotlin-mode--token-end parent-of-expression))
(kotlin-mode--forward-annotations)
(kotlin-mode--goto-non-comment-bol-with-same-nesting-level)
(when (or (< (point) (kotlin-mode--token-end parent-of-expression))
(< pos (point)))
(goto-char (kotlin-mode--token-end parent-of-expression)))
(setq parent-of-expression (kotlin-mode--backward-token))
(if (<= (kotlin-mode--token-start parent-of-previous-line)
(kotlin-mode--token-start parent-of-expression))
;; let x =
;; 1 + // indenting here
;; 2 +
;; 3
;;
;; Aligns with the parent of the expression with offset.
(kotlin-mode--align-with-next-token parent-of-expression offset)
;; let x =
;; 1 +
;; 2 + // indenting here
;; 3 // or here
;;
;; Aligns with the previous line.
(kotlin-mode--align-with-next-token parent-of-previous-line
bol-offset)))))
(defun kotlin-mode--forward-annotations ()
"Skip forward comments, whitespaces, labels, and annotations."
(let (token)
(while (progn
(forward-comment (point-max))
(setq token (kotlin-mode--forward-token))
(memq (kotlin-mode--token-type token) '(annotation label))))
(goto-char (kotlin-mode--token-start token))))
(defun kotlin-mode--backward-annotations ()
"Skip backward comments, whitespaces, labels, and annotations."
(let (token)
(while (progn
(forward-comment (- (point)))
(setq token (kotlin-mode--backward-token))
(memq (kotlin-mode--token-type token) '(annotation label))))
(goto-char (kotlin-mode--token-end token))))
(defun kotlin-mode--calculate-indent-after-open-curly-brace (offset)
"Return indentation after open curly braces.
Assuming the point is on the open brace.
OFFSET is the offset of the contents.
This function is also used for close curly braces."
;; If the statement is multiline expression, aligns with the start of
;; the line on which the open brace is:
;;
;; foo()
;; .then { x ->
;; foo()
;; foo()
;; }
;; .then { x ->
;; foo()
;; foo()
;; }
;;
;; rather than
;;
;; foo()
;; .then { x ->
;; foo()
;; foo()
;; }
;; .then { x ->
;; foo()
;; foo()
;; }
;;
;; Otherwise, aligns with the start of the whole statement:
;;
;; class Foo:
;; Bar {
;; fun foo() {}
;; }
;;
;; rather than
;;
;; class Foo:
;; Bar {
;; fun foo() {}
;; }
;;
;; POSSIBLE IMPROVEMENT: those behavior should be configurable.
;;