Scala in Emacs with ENSIME


As an amateur Clojure developer, I am particularly fond of CIDER. It brings everything I need to program with Clojure right inside my favourite text editor.

Therefore, when I approached Scala one of the first things I did was looking for something similar to CIDER. I hate leaving Emacs for programming tasks, so having it ready for Scala was paramount. Fortunately, it did not take me long to find my way: ENSIME was right around the corner.

ENSIME supports the famous sbt build tool, and the documentation gently guides you through all the necessary steps to achieve a working setup. It is really a matter of minutes.

A nice trick I grabbed straight from Sam Halliday init.el is:

(remove-hook 'post-self-insert-hook
             'scala-indent:indent-on-parentheses)

(sp-local-pair 'scala-mode "(" nil
               :post-handlers '(("||\n[i]" "RET")))
(sp-local-pair 'scala-mode "{" nil
               :post-handlers '(("||\n[i]" "RET") ("| " "SPC")))

This code let the awesome Smartparens take care of parentheses in Scala buffers. Another handy solution comes from Sebastian Wiesner:

(defun mu-scala-pop-to-sbt (new-frame)
    "Open SBT REPL for this project, optionally in a NEW-FRAME.

Select the SBT REPL for the current project in a new window.  If
the REPL is not yet running, start it.  With prefix arg, select
the REPL in a new frame instead."
(interactive "P")
;; Start SBT when no running, taken from `sbt:command'
(when (not (comint-check-proc (sbt:buffer-name)))
  (sbt:run-sbt))
(let ((display-buffer-overriding-action
       (if new-frame '(display-buffer-pop-up-frame) nil)))
  (pop-to-buffer (sbt:buffer-name))))

(with-eval-after-load 'scala-mode
  (bind-key "C-c m s" #'mu-scala-pop-to-sbt scala-mode-map))

I just started my journey with Scala but I can safely say ENSIME could be the perfect companion.