Sunday, May 2, 2010

Emacs and sbt

After selecting sbt as my new build-environment I still needed an editor to code in. First I went with TextMate with a Scala Bundle (don't remember which one, I installed that some time ago). While it offers just code highlighting and some code templates it works just fine. I didn't miss code completion all that much (probably because it didn't work very well in Eclipse either). But what I really missed was  compilation-error highlighting. I spent some time coding with the sbt-iTerm window beside the TextMate-editor and using Command+L (enter line number) to jump to the errors that showed up in iTerm. Works, but nothing to write home about..

I remembered a nice video about Aemon Cannon showing his ensime-Emacs mode (video on youtube). He shows syntax highlighting (actually that's provided by the scala-mode), code completion, compilation errors, type inspection and package overviews. Quite impressing, isn't it? And what I liked best about it, is that it directly uses scala itself (running in a "server-process") to provide those features. So they're accurate and not "guessed" by a second parser implementation. Also all the stuff happens in the background, so it doesn't slow down your typing. Great stuff!

So here's how I installed it

  • Install/Update to Emacs 23 for Mac OS X according to this blog. Ensime needs the Emacs 23.
  • Spent some time with various Emacs tutorials to refresh my Emacs-skills :)
  • Install the scala-mode for emacs as described on scala-lang.org
  • Download Ensime and unpack it to some path (i.e. to ~/scala/emacs/ensime)
  • Make sure you got a 'java' and a 'scala' on the path (i.e. ln -s /Applications/scala/bin/scala /usr/local/bin/scala)
  • add some lines to .emacs as described in the Ensime readme
  • create a .ensime file in your project root. Mine looks as follows (current version):
(
  :server-root "/Users/ms/scala/emacs/ensime/"
  :server-cmd  "bin/server.sh"
  :server-host "localhost"
  :server-env ()

  :project-package "ch.inventsoft.scalabase"
  :source ("src/main/scala")
  :exclude-source ()
  :classpath ("lib/jsr166y.jar"
        "lib_managed/scala_2.8.0-SNAPSHOT/compile/logback-classic-0.9.20.jar"
        "lib_managed/scala_2.8.0-SNAPSHOT/compile/logback-core-0.9.20.jar"
        "lib_managed/scala_2.8.0-SNAPSHOT/compile/slf4j-api-1.5.11.jar"
        "lib_managed/scala_2.8.0-SNAPSHOT/plugin/continuations-2.8.0-SNAPSHOT.jar"
        "project/boot/scala-2.8.0-SNAPSHOT/lib/scala-compiler.jar"
        "project/boot/scala-2.8.0-SNAPSHOT/lib/scala-library.jar"
        "lib_managed/scala_2.8.0-SNAPSHOT/test/scalatest-1.0.1-for-scala-2.8.0.RC1-SNAPSHOT.jar")        
)
  • fire up emacs, open a scala file of your project. If it doesn't have syntax highlighting enter 'M-x scala-mode'
  • Enter 'M-x ensime' and answer the questions.
  • See the Ensime readme for all the available shortcuts (Tab, C-c t, C-c p)
  • Feel happy :)

I still got some problems with the inspector (didn't figure out how to have it open in a separate window, probably due to my incomplete emacs know-how), but overall it works just great. With sbt ~test-quick executing all tests as I change the code it provides very fast feedback.

And it'll get even better. I just read on the scala-Maillist, that Cannon's proposal for Google's summer of code has been accepted. Looking forward to that! Great job, Aemon

No comments:

Post a Comment