Search code examples
scalaemacsindentationauto-indent

Scala mode indentation in Emacs


When writing Scala code in Emacs, I notice the following indentation issue:

List(1,2,3).foreach{ x =>

Then press enter.

Then close the bracket, and this is what ends up happening:

List(1,2,3).foreach{ x =>
                  }

Although this is one particular example, this issue appears in a variety of ways when auto-indenting in Emacs.

An answer to either of these two questions would be appreciated:

  1. How can this issue be fixed so that the brace gets put in the proper place and anything within the braces is indented one level to the right?

  2. Is it possible to disable this type of auto-indentation (i.e. like 'set noautoindent' in vi). I tried solutions like the ones suggested here: Disable auto indent globally in Emacs without success.

Thanks in advance!


Solution

  • I tried to solve this by editing scala-mode-indent.el file. It breaks indent in some of the other situations, but at least you will not have all those indents half-screen forward.

    Comment out this line:

    ;; (scala-indentation-from-following)
    

    And modify scala-indentation-from-preceding:

    (defun scala-indentation-from-preceding ()
       ;; Return suggested indentation based on the preceding part of the
       ;; current expression. Return nil if indentation cannot be guessed.
       (save-excursion
       (scala-backward-spaces)
       (and 
         (not (bobp))
       (if (eq (char-syntax (char-before)) ?\()
          (scala-block-indentation)
          (progn
            (when (eq (char-before) ?\))
            (backward-sexp)
            (scala-backward-spaces))
            t
           ;;(scala-looking-at-backward scala-expr-start-re)
    
          ))
        (if (scala-looking-at-backward scala-expr-start-re)
          (+ (current-indentation) scala-mode-indent:step)
          (current-indentation)
        ))))
    

    As I said, it still remains broken after that. I plan to write a better support shortly, maybe in a week or two.

    EDIT:

    If you want to disable scala indentation completely, comment out the line in scala-mode.el

    ;; indent-line-function          'scala-indent-line