Skip to content

Commit 238d4ea

Browse files
authored
Merge pull request #7 from maemre/scala3
Various indentation fixes: typos, differentiating new lines, support for `do` and `extension`
2 parents e22ef62 + 5eef13b commit 238d4ea

File tree

1 file changed

+28
-8
lines changed

1 file changed

+28
-8
lines changed

scala-mode-indent.el

+28-8
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,9 @@ and the empty line")
250250
(regexp-opt '("abstract" "catch" "case" "class" "def" "do" "else" "final"
251251
"finally" "for" "if" "implicit" "import" "lazy" "new" "object"
252252
"override" "package" "private" "protected" "return" "sealed"
253-
"throw" "trait" "try" "type" "val" "var" "while" "yield" "inline")
253+
"throw" "trait" "try" "type" "val" "var" "while" "yield" "inline"
254+
"extension"
255+
)
254256
'words)
255257
"Words that we don't want to continue the previous line")
256258

@@ -370,7 +372,7 @@ is not on a run-on line."
370372
"\\|:\\(" scala-syntax:after-reserved-symbol-re "\\)"))
371373

372374
(defconst scala-indent:forms-align-re
373-
(regexp-opt '("yield" "then" "else" "catch" "finally") 'words))
375+
(regexp-opt '("do" "yield" "then" "else" "catch" "finally") 'words))
374376

375377
(defun scala-indent:forms-align-p (&optional point)
376378
"Returns `scala-syntax:beginning-of-code-line' for the line on
@@ -434,10 +436,17 @@ Returns point or (point-min) if not inside a block."
434436
;; <hitting the beginning of a block when starting in the middle> { (
435437
(`(?\{) 'left-boundary) ;; too aggressive?
436438
(`(?\{ ,_ . ,_) 'left-boundary)
437-
(`(?\( ,_ . ,_) 'left-boundary)
439+
; (`(?\( ,_ . ,_) 'left-boundary)
438440
;; <dot chaining>
439441
(`(?\n ?.) 'dot-chain)
440442
(`(?\n ?. . ,_) 'dot-chain)
443+
;; extension
444+
;;
445+
;; FIXME: This is a hack that just checks if the previous line contains
446+
;; extension. The check for extension should check whether this is a
447+
;; single-def extension and for balanced parentheses, etc. to determine
448+
;; whether we emit a block token.
449+
((and `(extension . ,tail) (guard (memq ?\n tail))) 'block)
441450
;; =
442451
(`(= ?\n . ,_) 'decl-lhs)
443452
((and `(= ,_ . ,tail) (guard (memq ?\n tail))) 'after-decl)
@@ -511,6 +520,7 @@ Returns point or (point-min) if not inside a block."
511520
(`(with) 'block)
512521
;; yield
513522
(`(yield . ,_) 'yield-from-comp)
523+
(`(do . ,_) 'yield-from-comp)
514524
))
515525

516526
(defun scala-indent:relative-indent-by-elem (syntax-elem)
@@ -537,7 +547,7 @@ Returns point or (point-min) if not inside a block."
537547
(`(decl else) -2)
538548
(`(decl . ,_) 2)
539549
;; decl-lhs
540-
(`(decl-lhs decl . ,_) 0)
550+
(`(decl-lhs decl . ,_) 2)
541551
(`(decl-lhs dot-chain) 4)
542552
(`(dot-chain dot-chain) 0)
543553
(`(decl-lhs for-comp) 0)
@@ -653,7 +663,7 @@ Returns point or (point-min) if not inside a block."
653663
(setq last-indentation (current-indentation))
654664
(while (looking-at-p "\\.") (backward-char))
655665
;; ")." is a funny case where we actually do want to be on the dot
656-
(if (looking-at-p ")") (forwar-char))
666+
(if (looking-at-p ")") (forward-char))
657667
(scala-syntax:backward-sexp-forcing)))
658668
(let* ((x (or (scala-indent:skip-back-over-modifiers (point) stack)
659669
(list (point) stack)))
@@ -736,10 +746,17 @@ tokenization. The parser is not anything like well-formalized, but it can start
736746
at an arbitrary point in the buffer, and except in pathological cases, look at
737747
relatively few lines in order to make a good guess; and it is tolerant to a
738748
certain amount of incorrect or in-progress syntactic forms."
739-
(let* ((initResult (scala-indent:find-analysis-start point))
749+
(let* ((line-no
750+
;; Get the line number while taking blanks into account. This allows
751+
;; differentiating between indenting at a blank line and re-indenting
752+
;; at the line right before it.
753+
(line-number-at-pos
754+
(save-excursion
755+
(when point (goto-char point))
756+
(point))))
757+
(initResult (scala-indent:find-analysis-start point))
740758
(point (car initResult))
741759
(stack (cadr initResult))
742-
(line-no (line-number-at-pos point))
743760
(analysis (scala-indent:analyze-context point stack))
744761
(syntax-elem (list (nth 0 analysis)))
745762
(ctxt-line (nth 1 analysis))
@@ -756,7 +773,10 @@ certain amount of incorrect or in-progress syntactic forms."
756773
(setq point (nth 2 x))
757774
t)
758775
(setq analysis (scala-indent:analyze-context point stack))
759-
(setq syntax-elem (cons (nth 0 analysis) syntax-elem))
776+
(setq syntax-elem
777+
(if (nth 0 analysis)
778+
(cons (nth 0 analysis) syntax-elem)
779+
syntax-elem))
760780
(setq ctxt-line (nth 1 analysis))
761781
(setq ctxt-indent (nth 2 analysis))
762782
(setq prev-indent (nth 3 analysis))

0 commit comments

Comments
 (0)