forked from hvesalai/emacs-scala-mode
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathscala-mode-indent.el
1128 lines (1022 loc) · 42.1 KB
/
scala-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
;;; scala-mode.el - Major mode for editing scala, indenting
;;; Copyright (c) 2012 Heikki Vesalainen
;;; For information on the License, see the LICENSE file
(require 'scala-mode-syntax)
(require 'scala-mode-lib)
(eval-when-compile
(defvar scala-indent:effective-run-on-strategy)
(defvar scala-indent:previous-indent-pos))
(defcustom scala-indent:step 2
"The number of spaces an indentation step should be. The actual
indentation will be one or two steps depending on context."
:type 'integer
:safe #'integerp
:group 'scala)
(defcustom scala-indent:indent-value-expression nil
"Whether or not to indent multi-line value expressions, with
one extra step. When true, indenting will be
val x = try {
some()
} catch {
case e => other
} finally {
clean-up()
}
When nil, the same will indent as
val x = try {
some()
} catch {
case e => other
} finally {
clean-up()
}
"
:type 'boolean
:group 'scala)
(defcustom scala-indent:align-parameters nil
"Whether or not to indent parameter lists so that next
parameter lines always align under the first parameter. When
non-nil, indentation will be
def foo(x: Int, y: List[Int]
z: Int)
val x = foo(1, List(1, 2, 3) map (i =>
i + 1
), 2)
When nil, the same will indent as
def foo(x: Int, y: List[Int]
z: Int)
val x = foo(1, List(1, 2, 3) map (i =>
i + 1
), 2)
"
:type 'boolean
:safe #'booleanp
:group 'scala)
(defcustom scala-indent:align-forms nil
"Whether or not to align 'else', 'yield', 'catch', 'finally'
below their respective expression start. When non-nil, identing
will be
val x = if (foo)
bar
else
zot
when nil, the same will indent as
val x = if (foo)
bar
else
zot
"
:type 'boolean
:group 'scala)
(defconst scala-indent:eager-strategy 0
"See `scala-indent:run-on-strategy'")
(defconst scala-indent:operator-strategy 1
"See `scala-indent:run-on-strategy'")
(defconst scala-indent:reluctant-strategy 2
"See `scala-indent:run-on-strategy'")
(defconst scala-indent:keywords-only-strategy 3
"A strategy used internally by indent engine")
(defcustom scala-indent:default-run-on-strategy 2
"What strategy to use for detecting run-on lines, i.e. lines
that continue a statement from the previous line. Possible values
are:
'reluctant', which marks only lines that begin with -- or
that follow a line that ends with -- a reserved word that cannot start
or end a line, such as 'with'.
'operators', which extends the previous strategy by marking also
lines that begin with -- or that follow a line that ends with --
an operator character. For example, '+', '-', etc.
'eager', which marks all rows which could be run-ons, i.e. which
are not ruled out by the language specification.
"
:type `(choice (const :tag "eager" ,scala-indent:eager-strategy)
(const :tag "operators" ,scala-indent:operator-strategy)
(const :tag "reluctant" ,scala-indent:reluctant-strategy))
:group 'scala)
(make-variable-buffer-local 'scala-indent:effective-run-on-strategy)
(defcustom scala-indent:add-space-for-scaladoc-asterisk t
"When non-nil, a space will be added after a scaladoc asterisk,
when it is added to an empty line."
:type 'boolean
:safe #'booleanp
:group 'scala)
(defcustom scala-indent:use-javadoc-style nil
"When non-nil, multi-line comments are indented according to Javadoc
style (i.e. indented to the first asterisk). This overrides the
Scaladoc behavior of indenting comment lines to the second asterisk."
:type 'boolean
:safe #'booleanp
:group 'scala)
(defcustom scala-indent:use-cycle-indent nil
"When non-nil, indentation will cycle from the new indent
strategy indent, the last known indent, and the left margin on
subsequent indent-line calls."
:type 'boolean
:safe #'booleanp
:group 'scala)
(defun scala-indent:run-on-strategy ()
"Returns the currently effecti run-on strategy"
(or scala-indent:effective-run-on-strategy
scala-indent:default-run-on-strategy
scala-indent:eager-strategy))
(defun scala-indent:toggle-effective-run-on-strategy ()
"If effective run-on strategy is not set, it is set as follows:
- if default is eager or operators, then it is set to reluctant
- if default is reluctant, then it is set to eager. If it is set,
it is nilled."
(if scala-indent:effective-run-on-strategy
(setq scala-indent:effective-run-on-strategy nil)
(let ((new-strategy
(cond ((= (scala-indent:run-on-strategy)
scala-indent:reluctant-strategy)
scala-indent:eager-strategy)
((or (= (scala-indent:run-on-strategy)
scala-indent:operator-strategy)
(= (scala-indent:run-on-strategy)
scala-indent:eager-strategy))
scala-indent:reluctant-strategy))))
(setq scala-indent:effective-run-on-strategy new-strategy))))
(defun scala-indent:reset-effective-run-on-strategy ()
(setq scala-indent:effective-run-on-strategy nil))
(defun scala-indent:rotate-run-on-strategy ()
(interactive)
(let ((new-strategy
(cond ((= scala-indent:default-run-on-strategy
scala-indent:reluctant-strategy)
scala-indent:operator-strategy)
((= scala-indent:default-run-on-strategy
scala-indent:operator-strategy)
scala-indent:eager-strategy)
((= scala-indent:default-run-on-strategy
scala-indent:eager-strategy)
scala-indent:reluctant-strategy))))
(setq scala-indent:default-run-on-strategy new-strategy)
; (message "scala-indent:default-run-on-strategy set to %s" scala-indent:default-run-on-strategy)
))
(defun scala-indent:backward-sexp-to-beginning-of-line ()
"Skip sexps backwards until reaches beginning of line (i.e. the
point is at the first non whitespace or comment character). It
does not move outside enclosing list. Returns the current point or
nil if the beginning of line could not be reached because of
enclosing list."
(let ((code-beg (scala-lib:point-after
(scala-syntax:beginning-of-code-line))))
(ignore-errors
(while (> (point) code-beg)
(scala-syntax:backward-sexp)
(skip-syntax-backward ".")
(when (< (point) code-beg)
;; moved to previous line, set new target
(setq code-beg (scala-lib:point-after
(scala-syntax:beginning-of-code-line))))))
(unless (> (point) code-beg)
(point))))
(defun scala-indent:align-anchor ()
"Go to beginning of line, if a) `scala-indent:align-parameters' is nil or
`scala-indent:backward-sexp-to-beginning-of-line' is non-nil. This has the
effect of staying within lists if `scala-indent:align-parameters' is non-nil."
(when (or (scala-indent:backward-sexp-to-beginning-of-line)
(not scala-indent:align-parameters))
(back-to-indentation)))
(defun scala-indent:value-expression-lead (start anchor &optional not-block-p)
;; calculate an indent lead. The lead is one indent step if there is a '='
;; between anchor and start, otherwise 0.
(if (and scala-indent:indent-value-expression
(ignore-errors
(save-excursion
(let ((block-beg (if not-block-p
start
(nth 1 (syntax-ppss start)))))
(goto-char anchor)
(scala-syntax:has-char-before ?= block-beg)))))
scala-indent:step 0))
;;;
;;; Run-on
;;;
(defconst scala-indent:mustNotTerminate-keywords-re
(regexp-opt '("extends" "match" "with") 'words)
"Some keywords which occur only in the middle of an expression")
(defconst scala-indent:mustNotTerminate-line-beginning-re
(concat "\\(" scala-indent:mustNotTerminate-keywords-re
"\\|:\\(" scala-syntax:after-reserved-symbol-re "\\)\\)")
"All keywords and symbols that cannot terminate an expression
and must be handled by run-on. Reserved-symbols not included.")
(defconst scala-indent:mustTerminate-re
(concat "\\([,;]\\|=>?" scala-syntax:end-of-code-line-re
"\\|\\s(\\|" scala-syntax:empty-line-re "\\)")
"Symbols that must terminate an expression or start a
sub-expression, i.e the following expression cannot be a
run-on. This includes only parenthesis, '=', '=>', ',' and ';'
and the empty line")
(defconst scala-indent:mustNotContinue-re
(regexp-opt '("abstract" "catch" "case" "class" "def" "do" "else" "final"
"finally" "for" "if" "implicit" "import" "lazy" "new" "object"
"override" "package" "private" "protected" "return" "sealed"
"throw" "trait" "try" "type" "val" "var" "while" "yield" "inline")
'words)
"Words that we don't want to continue the previous line")
(defconst scala-indent:mustBeContinued-line-end-re
(concat "\\(" scala-syntax:other-keywords-unsafe-re
"\\|:" scala-syntax:end-of-code-line-re "\\)")
"All keywords and symbols that cannot terminate an expression
and are in fact a sign of run-on. Reserved-symbols not included.")
(defun scala-indent:run-on-p (&optional point strategy)
"Returns t if the current point is in the middle of an expression"
;; use default strategy if none given
(when (not strategy) (setq strategy (scala-indent:run-on-strategy)))
(save-excursion
(when point (goto-char point))
(unless (eobp)
;; NOTE: of course this 'cond' could be written as one big boolean
;; expression, but I doubt that would be so readable and maintainable
(cond
;; NO: this line starts with close parenthesis
((= (char-syntax (char-after)) ?\))
nil)
;; NO: the previous line must terminate
((save-excursion
(scala-syntax:skip-backward-ignorable)
(or (bobp)
(scala-syntax:looking-back-empty-line-p)
(scala-syntax:looking-back-token scala-indent:mustTerminate-re)))
nil)
;; YES: in a region where newlines are disabled
((and (scala-syntax:newlines-disabled-p)
(not (= strategy scala-indent:keywords-only-strategy)))
t)
;; NO: this line starts with a keyword that starts a new
;; expression (e.g. 'def' or 'class')
((looking-at scala-indent:mustNotContinue-re)
nil)
;; NO: this line is the start of value body
;; ((scala-indent:body-p) ;; TODO did I delete this function when I shouldn't have?
;; TODO or even if I did, maybe it just doesn't matter because the
;; heuristics that union this algorithm with the other will compensate?
;; nil)
;; YES: eager strategy can stop here, everything is a run-on if no
;; counter evidence
((= strategy scala-indent:eager-strategy)
t)
;; YES: this line must not terminate because it starts with a
;; middle of expression keyword
((looking-at scala-indent:mustNotTerminate-line-beginning-re)
t)
;; YES: end of prev line must not terminate
((let ((case-fold-search nil))
(scala-syntax:looking-back-token
scala-indent:mustBeContinued-line-end-re))
t)
;; YES: this line starts with type param
((= (char-after) ?\[)
t)
;; YES: this line starts with open paren and the expression
;; after all parens is a run-on
((and (= (char-after) ?\()
(save-excursion (scala-syntax:forward-parameter-groups)
(scala-syntax:skip-forward-ignorable)
(or (= (char-after) ?=)
(= (char-after) ?{)
(scala-indent:run-on-p nil strategy))))
t)
;; NO: that's all for keywords-only strategy
((= strategy scala-indent:keywords-only-strategy)
nil)
;; YES: this line starts with punctuation
((= (char-after) ?\.)
t)
;; YES: prev line ended with punctuation
((scala-syntax:looking-back-token ".*[.]")
t)
;; NO: that's all for reluctant-strategy
((= strategy scala-indent:reluctant-strategy)
nil)
;; YES: this line starts with opchars
((save-excursion
(< 0 (skip-chars-forward scala-syntax:opchar-group)))
t)
;; YES: prev line ends with opchars
((save-excursion
(scala-syntax:skip-backward-ignorable)
(> 0 (skip-chars-backward scala-syntax:opchar-group)))
t)
;; NO: else nil (only operator strategy should reach here)
(t nil)))))
(defun scala-indent:run-on-line-p (&optional point strategy)
"Returns t if the current point (or point at 'point) is on a
line that is a run-on from a previous line."
(save-excursion
(when point (goto-char point))
(scala-syntax:beginning-of-code-line)
(scala-indent:run-on-p nil strategy)))
(defun scala-indent:goto-run-on-anchor (&optional point strategy)
"Moves back to the point whose column will be used as the
anchor relative to which indenting for current point (or point
'point') is calculated. Returns the new point or nil if the point
is not on a run-on line."
(when (scala-indent:run-on-line-p point strategy)
(when point (goto-char point))
(scala-syntax:beginning-of-code-line)
(while (and (scala-indent:run-on-line-p nil strategy)
(scala-syntax:skip-backward-ignorable)
(scala-indent:backward-sexp-to-beginning-of-line)))
(scala-indent:align-anchor)
(point)))
(defconst scala-indent:double-indent-re
;; used to include with but given...with is a counterexample
(concat (regexp-opt '("extends" "forSome") 'words)
"\\|:\\(" scala-syntax:after-reserved-symbol-re "\\)"))
(defconst scala-indent:forms-align-re
(regexp-opt '("yield" "then" "else" "catch" "finally") 'words))
(defun scala-indent:forms-align-p (&optional point)
"Returns `scala-syntax:beginning-of-code-line' for the line on
which current point (or point 'point') is, if the line starts
with one of 'yield', 'then', 'else', 'catch' and 'finally', otherwise
nil. Also, the previous line must not be with '}'"
(save-excursion
(when point (goto-char point))
(scala-syntax:beginning-of-code-line)
(when (looking-at scala-indent:forms-align-re)
(goto-char (match-beginning 0))
(point))))
(defun scala-indent:for-enumerators-p (&optional point)
"Returns the point after opening parentheses if the current
point (or point 'point') is in a block of enumerators. Return nil
if not in a list of enumerators or at the first enumerator."
(unless point (setq point (point)))
(save-excursion
(goto-char point)
(scala-syntax:beginning-of-code-line)
(let ((state (syntax-ppss point)))
(unless (or (eobp) (= (char-syntax (char-after)) ?\)))
(when (and state (nth 1 state))
(goto-char (nth 1 state))
(when (scala-syntax:looking-back-token scala-syntax:for-re)
(forward-char)
(forward-comment (buffer-size))
(when (< (point) point)
(1+ (nth 1 state)))))))))
;;;
;;; Block
;;;
(defun scala-indent:goto-block-anchor (&optional point)
"Moves back to the point whose column will be used as the
anchor for calculating block indent for current point (or POINT).
Returns point or (point-min) if not inside a block."
(when-let ((block-beg (nth 1 (syntax-ppss
(scala-lib:point-after (beginning-of-line))))))
;; Check if the opening paren is the first on the line, if so, it is the
;; anchor. If not, then go back to the start of the line
(goto-char block-beg)
(if (= (point) (scala-lib:point-after
(scala-syntax:beginning-of-code-line)))
(point)
(goto-char (or (scala-syntax:looking-back-token
scala-syntax:body-start-re 3)
(point)))
(scala-syntax:backward-parameter-groups)
(when (scala-indent:backward-sexp-to-beginning-of-line)
(scala-indent:goto-run-on-anchor nil
scala-indent:keywords-only-strategy))
(scala-indent:align-anchor)
(point))))
(defun scala-indent:analyze-syntax-stack (stack)
"A kind of tokenize step of the hand-wavy parse"
(pcase stack
;; <hitting the beginning of a block when starting in the middle> { (
(`(?\{) 'left-boundary) ;; too aggressive?
(`(?\{ ,_ . ,_) 'left-boundary)
(`(?\( ,_ . ,_) 'left-boundary)
;; <dot chaining>
(`(?\n ?.) 'dot-chain)
(`(?\n ?. . ,_) 'dot-chain)
;; =
(`(= ?\n . ,_) 'decl-lhs)
((and `(= ,_ . ,tail) (guard (memq ?\n tail))) 'after-decl)
(`(= ,_ . ,_) 'decl-inline-lhs)
;; =>
(`(=> ?\n . ,_) 'arrow-lhs)
((and `(=> ,_ . ,tail) (guard (memq ?\n tail))) 'after-arrow)
(`(=> ,_ . ,_) 'arrow-lhs)
;; <-
(`(<- . ,_) 'generator)
;; case
(`(case . ,_) 'case)
;; class
((and `(class . ,tail) (guard (memq ': tail))) 'block)
(`(class . ,_) 'decl)
;; def
((and `(def . ,tail) (guard (memq '= tail)))
(if (memq ?\n tail) 'after-decl 'block))
(`(def . ,_) 'decl)
;; do
(`(do ,_ . ,_) 'block)
;; else
(`(else ?\n . ,_) 'else-conseq)
(`(else) 'else)
(`(else . ,_) 'else-inline)
;; enum
((and `(enum . ,tail) (guard (memq ': tail))) 'block)
(`(enum . ,_) 'decl)
;; final
(`(final) 'decl)
;; for
(`(for) 'for-comp)
(`(for . ,_) 'for-body)
;; given
(`(given . ,_) 'decl)
;; if
(`(if ?\n . ,_) 'if-cond)
(`(if . ,_) 'if)
;; implicit
(`(implicit) 'decl)
;; import
((and `(import . ,tail) (guard (memq ?\n tail))) 'after-decl)
(`(import . ,_) 'decl)
;; match
(`(,_ match) 'match)
;; object
((and `(object . ,tail) (guard (memq ': tail))) 'block)
(`(object . ,_) 'decl)
;; override
(`(override) 'decl)
;; package
(`(package . ,_) 'decl)
;; sealed
(`(sealed) 'decl)
;; then
(`(then ?\n . ,_) 'then-conseq)
(`(then) 'then)
(`(then . ,_) 'then-inline)
;; trait
((and `(trait . ,tail) (guard (memq ': tail))) 'block)
(`(trait . ,_) 'decl)
;; val
((and `(val . ,tail) (guard (memq '= tail)))
(if (memq ?\n tail) 'after-decl 'block))
(`(val . ,_) 'decl)
;; var
(`(var . ,_) 'decl)
;; while
(`(while) 'decl)
;; with
(`(with) 'block)
;; yield
(`(yield . ,_) 'yield-from-comp)
))
(defun scala-indent:relative-indent-by-elem (syntax-elem)
"TODO document"
(pcase syntax-elem
;; after-decl
(`(after-decl else) -2)
(`(after-decl) 0)
;; arrow-lhs
(`(arrow-lhs) 2)
(`(arrow-lhs case . ,_) 0) ;; within match
(`(arrow-lhs dot-chain) 4)
(`(arrow-lhs . ,_) :maintain)
;; block
(`(block) 2)
(`(block . ,_) 2)
;; case
(`(case) :maintain)
(`(case case) 0) ;; e.g. in enums
(`(case ,_) 2)
;; decl
(`(decl decl) 0)
(`(decl decl decl-inline-lhs) 0)
(`(decl else) -2)
(`(decl . ,_) 2)
;; decl-lhs
(`(decl-lhs decl . ,_) 0)
(`(decl-lhs dot-chain) 4)
(`(dot-chain dot-chain) 0)
(`(decl-lhs for-comp) 0)
(`(decl-lhs generator) 0)
(`(decl-lhs yield-from-comp) -2)
(`(decl-lhs) 2)
(`(decl-lhs . ,_) 0)
;; else
(`(else ,_) 2)
;; else-conseq
(`(else-conseq) 2)
(`(else-conseq . ,_) :maintain)
;; else-inline
(`(else-inline . ,_) 0)
;; for-body
(`(for-body . ,_) 2)
;; for-comp
(`(for-comp yield-from-comp) 0)
;; generator
(`(generator yield-from-comp) -2)
(`(generator . ,_) 0)
;; if
(`(if then) 0)
(`(if then-inline) 0)
(`(if . ,_) 2)
;; if-cond
(`(if-cond then) 0)
(`(if-cond) 2)
;; left-boundary
(`(left-boundary dot-chain) 4)
;; match
(`(match case . ,_) 2)
;; then
(`(then else) 0)
(`(then else-inline) 0)
(`(then ,_) 2)
;; then-conseq
(`(then-conseq else) 0)
(`(then-conseq) 2)
(`(then-conseq ,_) 2)
;; then-inline
(`(then-inline else) 0)
(`(then-inline else-inline) 0)
;; yield-from-comp
(`(yield-from-comp) 0)
;; <fallbacks>
(`(,_ then) -2)
(`(,_ else) -2)
))
(defun scala-indent:find-analysis-start (&optional point)
"Find a place to start tokenizing in a consistent manner"
(save-excursion
(when point (goto-char point))
(let (stack)
;; Always look at a token on the current for starters
(when (> (current-indentation) (current-column))
(scala-syntax:forward-token))
(if (= (line-beginning-position) (line-end-position))
;; Handle blank lines
(progn
(scala-syntax:backward-sexp-forcing)
(setq stack (cons ?\n stack)))
;; (beginning-of-thing 'sexp) gets confused by `.'
(unless (looking-at-p "\\.")
;; Avoid double-reading current symbol
(beginning-of-thing 'sexp)))
;; handle the occurence of case in various contexts
(or (save-excursion
(when-let ((_ (looking-at-p (concat "case *"
scala-syntax:class-or-object-re)))
(point (progn (forward-to-word 1) (point)))
(class-or-object (sexp-at-point)))
;; This throws away the stack we've built up above. The assumption
;; here is that this case is mutually exclusive with those above.
(scala-indent:skip-back-over-modifiers point
(list class-or-object))))
(list (point) stack)))))
(defun scala-indent:analyze-context (point &optional init-stack)
"TODO document"
(save-excursion
(goto-char point)
(let (result
last-indentation
(stack init-stack))
(while (and (not result) (> (point) 1))
(setq stack
(if (looking-at-p "\\.")
(cons ?. stack)
(let ((s (or (sexp-at-point) (char-after))))
(backward-char)
(if (looking-at-p "\\.")
;; Try hard to notice dot-chaining
(cons ?. (cons s stack))
(if (looking-at-p "\"")
;; A little hack in case we are inside of a string
stack
(forward-char)
(cons s stack))))))
(setq result
(scala-indent:analyze-syntax-stack stack))
(when (and (not result)
(save-excursion (= (point)
(scala-syntax:beginning-of-code-line))))
(setq stack (cons ?\n stack))
(setq result
(scala-indent:analyze-syntax-stack stack))
(when result
(setq last-indentation (current-indentation))
(scala-syntax:backward-sexp-forcing)))
(unless result
(setq last-indentation (current-indentation))
(while (looking-at-p "\\.") (backward-char))
;; ")." is a funny case where we actually do want to be on the dot
(if (looking-at-p ")") (forwar-char))
(scala-syntax:backward-sexp-forcing)))
(let* ((x (or (scala-indent:skip-back-over-modifiers (point) stack)
(list (point) stack)))
(point (nth 0 x))
(stack (nth 1 x)))
(list result
(line-number-at-pos)
(current-indentation)
last-indentation
point
stack)))))
(defun scala-indent:full-stmt-less-than-line (syntax-elem stopped-point)
(and
(consp syntax-elem)
;; read a full statement
(pcase (car syntax-elem)
('after-decl t)
('after-arrow t))
(save-excursion
(goto-char stopped-point)
;; but that statement took up less than a line
(> (current-column) (current-indentation)))))
(defun scala-indent:continue-lookback? (syntax-elem
ctxt-line
line-no
stopped-point
end-stack)
(or (and (= ctxt-line line-no) (> line-no 1)
;; If we keep reading for this reason, we've accepted the
;; existing tokens and so need to clear the stack
(list syntax-elem ;; syntax-elem
nil ;; stack
(save-excursion ;; point
(goto-char stopped-point)
(scala-syntax:backward-sexp-forcing)
(point))))
(when (scala-indent:full-stmt-less-than-line syntax-elem stopped-point)
;; If we read a full statement that was only part of a line,
;; drop it and try again for more context
(list (cdr syntax-elem) ;; syntax-elem
end-stack ;; restart with the existing stack
(save-excursion ;; point
(goto-char stopped-point)
(scala-syntax:backward-sexp-forcing)
(point))))
;; We know we have a dot-chain, but we need to get more context to know
;; how to position it
(when (equal syntax-elem '(dot-chain))
(list syntax-elem ;; syntax-elem
nil ;; stack
stopped-point ;; point
))))
(defun scala-indent:skip-back-over-modifiers (point stack)
(if-let* ((head (car stack))
(_ (memq head '(trait class object)))
(new-point point)
(new-sexp t)
(new-stack stack))
(save-excursion
(goto-char new-point)
(scala-syntax:backward-sexp-forcing)
(setq new-sexp (sexp-at-point))
(while (memq new-sexp
'(final sealed case open abstract implicit private))
(setq new-point (point))
(setq new-stack (cons new-sexp new-stack))
(scala-syntax:backward-sexp-forcing)
(setq new-sexp (sexp-at-point)))
(list new-point new-stack))))
(defun scala-indent:whitespace-biased-indent (&optional point)
"Whitespace-syntax-friendly heuristic indentation engine.
The basic idea is to look back a relatively short distance (one semantic line
back with some hand-waving) to parse the context based on a two-level
tokenization. The parser is not anything like well-formalized, but it can start
at an arbitrary point in the buffer, and except in pathological cases, look at
relatively few lines in order to make a good guess; and it is tolerant to a
certain amount of incorrect or in-progress syntactic forms."
(let* ((initResult (scala-indent:find-analysis-start point))
(point (car initResult))
(stack (cadr initResult))
(line-no (line-number-at-pos point))
(analysis (scala-indent:analyze-context point stack))
(syntax-elem (list (nth 0 analysis)))
(ctxt-line (nth 1 analysis))
(ctxt-indent (nth 2 analysis))
(prev-indent (nth 3 analysis))
(stopped-point (nth 4 analysis))
(end-stack (nth 5 analysis))
)
(message "analysis: %s" analysis)
(while (when-let ((x (scala-indent:continue-lookback?
syntax-elem ctxt-line line-no stopped-point end-stack)))
(setq syntax-elem (nth 0 x))
(setq stack (nth 1 x))
(setq point (nth 2 x))
t)
(setq analysis (scala-indent:analyze-context point stack))
(setq syntax-elem (cons (nth 0 analysis) syntax-elem))
(setq ctxt-line (nth 1 analysis))
(setq ctxt-indent (nth 2 analysis))
(setq prev-indent (nth 3 analysis))
(let ((old-stopped-point stopped-point))
(setq stopped-point (nth 4 analysis))
(when (eq old-stopped-point stopped-point)
(message
"Whitespace-friendly indentation algorithm not making progress :(")
(error "Got stuck at %s" stopped-point)))
(setq end-stack (nth 5 analysis)))
(when-let ((_ (< ctxt-line line-no))
(relative (scala-indent:relative-indent-by-elem syntax-elem)))
(list (if (eq :maintain relative)
(current-indentation)
(+ (if (eq ?\n (car end-stack))
;; Oops, moved a bit too far back while determining
;; context. Don't really want to determine our indentation
;; based on the line whose newline we are looking at, but
;; rather the next one.
prev-indent
ctxt-indent)
relative))
stopped-point))))
(defun scala-indent:resolve-block-step (start anchor)
"Resolves the appropriate indent step for block line at position
'start' relative to the block anchor 'anchor'."
(let
((lead (scala-indent:value-expression-lead start anchor)))
(cond
;; at end of buffer
((= start (point-max)) (+ scala-indent:step lead))
;; block close parentheses line up with anchor in normal case
((= (char-syntax (char-after start)) ?\))
(+ 0 lead))
;; case-lines indent normally, regardless of where they are
((scala-syntax:looking-at-case-p start)
(+ scala-indent:step lead))
;; other than case-line in case-block get double indent
((save-excursion
(goto-char (1+ (or (nth 1 (syntax-ppss start)) 0)))
(forward-comment (buffer-size))
(and (scala-syntax:looking-at-case-p)
(> (line-number-at-pos) (line-number-at-pos anchor))
(> start (match-beginning 0))))
(+ (* 2 scala-indent:step) lead))
;; normal block line
(t (+ scala-indent:step lead)))))
;;;
;;; Indentation engine
;;;
(defun scala-indent:block-biased-indent (point)
"TODO."
(save-excursion
(when point (goto-char point))
(let* ((pos (scala-syntax:beginning-of-code-line))
(anchor (scala-indent:goto-block-anchor point)))
(when anchor
(when (/= anchor (point))
(error (format "Assertion error: anchor=%d, point=%d" anchor (point))))
(list
(+ (current-column)
(save-excursion
(scala-indent:resolve-block-step pos anchor)))
anchor
)
))))
(defun scala-indent:reconcile (whitespace block)
(let ((ws-indent (nth 0 whitespace))
(ws-lookback-point (nth 1 whitespace))
(blk-indent-point (nth 0 block))
(blk-lookback (nth 1 block)))
(cond
;; Nothing to reconcile
((eq ws-indent blk-indent-point) ws-indent)
;; Counterintuitive as it may be, the algorithm that had to look the
;; farthest back (and so has the smallest lookback point) is least likely
;; to have gotten the answer right. This is because both algorithms have
;; bias toward not giving up; but the more remote they get from their
;; starting point, the more likely it is that they did not understand the
;; local syntax, and are going to suggest a large and unpleasant change in
;; indentation. Or from another perspective: we want to bias toward local
;; correctness. If they stopped on the same character, then we know from
;; the behavior of the block algorithm that it is a parenthetical
;; character; in which case the block algorithm most likely got the right
;; answer.
((> ws-lookback-point blk-lookback) ws-indent)
(t blk-indent-point))))
(defun scala-indent:calculate-indent-for-line (&optional point)
"Calculate the appropriate indent for the current point or POINT.
Returns the new column, or nil if the indent cannot be determined."
(let ((whitespace (ignore-errors
(scala-indent:whitespace-biased-indent point)))
(block (scala-indent:block-biased-indent point)))
(pcase (cons whitespace block)
(`(nil . ,x) (nth 0 x))
(`(,x . nil) (nth 0 x))
(`(,x . ,y) (scala-indent:reconcile x y)))
))
(defun scala-indent:indent-line-to (column)
"Indent the line to column and move cursor to the indent
column, if it was at the left margin."
(when column
(if (<= (current-column) (current-indentation))
(indent-line-to column)
(save-excursion (indent-line-to column)))))
(make-variable-buffer-local 'scala-indent:previous-indent-pos)
(defun scala-indent:remove-indent-from-previous-empty-line ()
"Handles removing of whitespace from a previosly indented code
line that was left empty (i.e. whitespaces only). Also clears the
scala-indent:previous-indent-pos variable that controls the process."
(when (and scala-indent:previous-indent-pos
(/= scala-indent:previous-indent-pos (point)))
(save-excursion
(beginning-of-line)
(if (= scala-indent:previous-indent-pos
(point))
(setq scala-indent:previous-indent-pos
(when (looking-at "^\\s +$") (point)))
(goto-char scala-indent:previous-indent-pos)
(when (looking-at "^\\s +$")
(delete-region (match-beginning 0) (match-end 0)))
(setq scala-indent:previous-indent-pos nil)))))
(defun scala-indent:indent-code-line (&optional strategy)
"Indent a line of code. Expect to be outside of any comments or
strings"
(if strategy
(setq scala-indent:effective-run-on-strategy strategy)
(if (eq last-command this-command)
(scala-indent:toggle-effective-run-on-strategy)
(scala-indent:reset-effective-run-on-strategy)))
; (message "run-on-strategy is %s" (scala-indent:run-on-strategy))
(scala-indent:indent-line-to (scala-indent:calculate-indent-for-line))
(scala-lib:delete-trailing-whitespace)
(setq scala-indent:previous-indent-pos
(save-excursion
(beginning-of-line)
(when (looking-at "^\\s +$") (point)))))
(defvar-local scala-indent:cycle-indent-stack (list)
"The automatically buffer local scala indent cycle stack.
The stack is initialized as (left-margin, (current-indentation))
when the custom var \"scala-indent:use-cycle-indent\" is non-nil
and \"scala-indent:indent-line\" is called. Subsequent
\"scala-indent:indent-line\" calls pop the indentation value from
the stack, until it is empty, resetting the indentation cycle.")
(defun scala-indent:cycle-indent-stack-push (indentation)
"Pushes an integer value onto the \"scala-indent:cycle-indent-stack\".
Will fail if INDENTATION is not an integer"
(if (integerp indentation)
(add-to-list 'scala-indent:cycle-indent-stack indentation)
(error "\"scala-indent:cycle-indent-stack-push\": Invalid INDENTATION argument %s"
indentation)))
(defun scala-indent:cycle-indent-stack-pop ()
"Gets the top value of the \"scala-indent:cycle-indent-stack\" stack.
Modifies the stack in-place."
(pop (buffer-local-value 'scala-indent:cycle-indent-stack (current-buffer))))
(defun scala-indent:cycle-indent-stack-depth ()
"The current depth of the \"scala-indent:cycle-indent-stack\" stack"
(length (buffer-local-value 'scala-indent:cycle-indent-stack (current-buffer))))
(defun scala-indent:cycle-indent-stack-emptyp (x)
"Check if the \"scala-indent:cycle-indent-stack\" is empty.
Returns t if the \"scala-indent:cycle-indent-stack\" is empty,
nil otherwise."
(= (length (buffer-local-value 'scala-indent:cycle-indent-stack (current-buffer))) 0))
(defun scala-indent:cycle-indent-line (&optional strategy)
"Cycle scala indentation using optionally passed STRATEGY.
When the \"scala-indent:cycle-indent-stack\" is empty, push 0 and
the current indentation onto the stack, then indent according to
the optionally passed STRATEGY. Indent to the top of
\"scala-indent:cycle-indent-stack\" when non-empty."
(interactive "*")
(cond ((scala-indent:cycle-indent-stack-emptyp nil)
(scala-indent:cycle-indent-stack-push (current-indentation))
(scala-indent:cycle-indent-stack-push 0)
(call-interactively 'scala-indent:strategy-indent-line t))
(t (scala-indent:indent-line-to (scala-indent:cycle-indent-stack-pop)))))
;; the previously-named scala-indent:indent-line
(defun scala-indent:strategy-indent-line (&optional strategy)
"Indent lines according to the OPTIONAL scala indentation STRATEGY."
(interactive "*")
(let ((state (save-excursion (syntax-ppss (line-beginning-position)))))
(if (nth 8 state) ;; 8 = start pos of comment or string
(scala-indent:indent-line-to
(cond ((integerp (nth 4 state)) ;; 4 = nesting level of multi-line comment
(scala-indent:scaladoc-indent (nth 8 state)))
((eq t (nth 3 state)) ;; 3 = t for multi-line string
(or (save-excursion
(beginning-of-line)
(when (and (looking-at "\\s *|")
(progn (goto-char (nth 8 state))
(looking-at "\\(\"\"\"\\)|")))
(goto-char (match-end 1))
(current-column)))
(current-indentation)))
(t (current-indentation))))
(scala-indent:indent-code-line strategy)))
)
(defun scala-indent:indent-line (&optional strategy)
"Indent the current line with cycling.
If the custom var \"scala-indent:use-cycle-indent\" is non-nil,
cycle-indent using the optionally passed STRATEGY. Indent using
the optionally passed STRATEGY without cycling otherwise."
(interactive "*")
(if scala-indent:use-cycle-indent
(call-interactively t 'scala-indent:cycle-indent-line)
(call-interactively t 'scala-indent:strategy-indent-line)))
(defun scala-indent:indent-with-reluctant-strategy ()
(interactive "*")