Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 215
nequitans Avatar asked Jan 20 '12 17:01

nequitans


1 Answers

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

like image 78
Rogach Avatar answered Sep 29 '22 06:09

Rogach