Question

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!

Was it helpful?

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

OTHER TIPS

I've coded up a simple piece of code - it makes emacs keep indent level for most time, and indents two spaces to the right when the previous non-empty line ends with "{", "(", ">", "=".

Add file besi.el to your load path.

(provide 'besi)

(defun besi-indent-line ()
  "Indent current line"
  (interactive)
  (scala-indent-line-to (besi-indent)))

(defun besi-indent ()
  (save-excursion
    (forward-comment -100000)
    (backward-char 1)
    (if (or (looking-at "{") (looking-at "=") (looking-at ">") (looking-at "("))
      (+ (current-indentation) 2)
      (current-indentation))))

(defun besi-newline ()
  (interactive)
  (newline-and-indent))

Edit line in scala-mode.el

indent-line-function          'besi-indent-line

and line in scala-mode-ui.el

("\r"                       'besi-newline)

Also add a line (require 'besi) to the start of scala-mode.el.

Uploaded it to github for easy reference - besi

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top