Scala

Developing Scala in Atom with Ensime

When I began Java software development way back in the time of Java 1.3 I remember using JDeveloper for a time, before switching to the early versions of Eclipse. I stayed with Eclipse for a very long time (about 8 years) before switching to IntelliJ about 4 years ago. I’ve always been a big fan of IntelliJ, not just for programming Java, but I also kept using it when I started with Scala a couple of years ago. Last months, though, I started noticing that I actually only use a small amount of all the IntelliJ functionality and was slowly getting a bit annoyed with the way IntelliJ slowed down to a crawl for certain Scala projects. Combined with the fact that IntelliJ tripped over some Shapeless, Spray, Scalaz stuff, made me start looking for an alternative leightweight Scala editor.

Since I already am using Atom for taking notes (nvAtom in combination with a synced dropbox folder works great!), and I tried Ensime a couple of months ago, I thought I’d gave that combination another try. In this article you can read the steps I’ve taken, the plugins I’ve installed to get to a fairly good working Ensime / Atom combination.

Getting and installing Ensime

I’ve waited with this article until Scala 2.11.8 came out, since it solves a number of issues with imports and implicits when using Ensime. So for the best experience of using Ensime, upgrade your projects to Scala 2.11.8. So with that out of the way lets get started with installing Ensime.

ensime_1

To install Ensime, just follow the instructions on the site (http://ensime.github.io/editors/atom/installation/), and you’ll get a atom which you can use to run Ensime.

Preparing and importing a project

Once installed, make sure you point to your local sbt installation, and you’re more or less ready to start using ensime. The last thing we need to do, is install an sbt plugin which is needed to generate some information ensime needs. Follow the installation instructions from here (http://ensime.github.io/build_tools/sbt/) and once done open a console and navigate to the project you want to import. If you haben’t already done so, update the Scala version to 2.11.8. In that directory call `sbt gen-ensime`.

➜  graphql-sangria  sbt gen-ensime
[info] Set current project to graphql-sangria (in build file:/Users/jos/dev/git/graphql-sangria/)
[info] ENSIME update. Please vote for https://github.com/sbt/sbt/issues/2266
[info] ENSIME updateClassifiers. Please vote for https://github.com/sbt/sbt/issues/1930
[warn] Multiple dependencies with the same organization/name but different versions. To avoid conflict, pick one version:
[info] Resolving org.scala-lang#scalap;2.11.7 ...
[info] ENSIME processing graphql-sangria (graphql-sangria)
➜  graphql-sangria  ls -lat
total 32
-rw-r--r--   1 jos  staff  9327 Mar 19 09:54 .ensime
drwxr-xr-x  57 jos  staff  1938 Feb 22 09:28 ..
drwxr-xr-x   6 jos  staff   204 Jan 20 07:15 .ensime_cache
drwxr-xr-x   9 jos  staff   306 Jan 19 20:22 .
drwxr-xr-x  12 jos  staff   408 Jan  5 14:17 .idea
-rw-r--r--   1 jos  staff   410 Jan  2 14:45 build.sbt
drwxr-xr-x   4 jos  staff   136 Jan  2 13:24 src
drwxr-xr-x   6 jos  staff   204 Jan  2 13:24 target
drwxr-xr-x   6 jos  staff   204 Jan  2 13:23 project
➜  graphql-sangria

Note that I use the following sbt wrapper (https://github.com/paulp/sbt-extras), which drastically reduces the output of SBT only to the necessary, and provides a lot of additional feature that make working with SBT a little easier. So when you run this, you might see some more information. The import thing to notice here, is that we’ve got a `.ensime` and a `.ensime_cache` directory now, which contains Ensime specific files.

In Atom open up a new window, add that folder and you can use ctrl-cmd-p `Ensime: start` to start ensime.

ensime_2

The first time will take some time while dependencies are being downloaded, but eventually Ensime will finish downloading and start the server. At this point you can use Ensime to check your Scala code (either while your typing or whenever you save). At the bottom it will show you the issues with the current file or with the complete project. For instance in the sample project, I’ve got a number of unused imports:

ensime_3

It also provides import suggestions:

ensime_4

And shows specific errors.

ensime_5

So for a lot of projects, you can already use Atom for your Scala development. There are, however, also a couple of issues with Ensime.

What doesn’t work that great yet

I’m an avid IntelliJ user, and one of the features I noticed I used a lot when I started using Atom, was the auto-import function of IntelliJ. Ensime/Atom provides something similar, but it is by far not as useful as the IntelliJ one. With Atom/Ensime you can import a suggestion, but (at the moment) it just selects one, instead of allowing you to chose. More often than not the wrong one gets imported.

Even though the import feature is an annoying one, it’s just a matter of time before it is added, since Ensime does provide multiple hints, it’s just that the Atom/Ensime plugin doesn’t support it yet.

There are a number of other feature, where IntelliJ provides more helpful information, but nothing that really stands in the way of working, you’ll just be browsing the source/docuemntation a little more, which is easy to do (`Ensime: go to doc`):

ensime_6

Or just `ctrl-click` to go to the declaration.

Some stuff that was blocking

While Ensime/Atom is quickly getting better, at the moment, for some projects, there could be some really blocking issues. If you use a project that uses macros or some compiler plugins, you might get false errors. I was going to show a project where false errors where reported, but with the latest version the errors apparently don’t occur anymore in my spray/shapeless project. So I took one of my other projects (which uses a lot of Scalaz stuff), where IntelliJ has issues with as a test to see how Ensime/Atom handles that. While it takes some time for Ensime to compile everything and get to a stable state, it handles everything correctly!

Regardless of this recent suprising succes, there is still a big change, you’ll run into false errors. This is an issue where the Ensime team is aware of (see issue: https://github.com/ensime/ensime-server/issues/673), and which apparently is very difficult to solve. People however are working on this by providing a plugin which returns the correct signatures for generated code, to make the editors happy. More information on this can be found here: https://github.com/fommil/imaginary-friend

Additional plugins which make working with Ensime easier

With the Ensime plugin you can probably already get a lot of work done using the standard functionality in Atom. However, I’ve installed the two suggested plugins from the Ensime/Atom site and they really make working with multiple projects and navigating through your code a lot easier. So if you intend to start using Atom and Ensime more. These two are very useful to install:

  1. Project manager: Allows you to manage multiple projects
  2. Last cursor position: Navigate back to where you where

Lastly, I suggest you install the term3 package, with this package you can open a terminal window directly in your Atom, which is nice when you’ve got some quick sbt commands to execute, or do some git actions (which is pretty much the way I also work in IntelliJ)

Conclusion

Development of the Ensime server goes quickly, as I’ve just noticed myself. A month ago there were a whole lot of issues with spray/shapeless which seems to have been solved now (whether this is through Ensime improvements or scala 2.11.8 I don’t know). The Atom plugin has some rough edges, but is already really useful. One of the biggest advantages is how leightweight Atom is in comparison with IntelliJ. So I’ll start working with Atom more and more, but will probably come back to IntelliJ for certain type of projects.

Reference: Developing Scala in Atom with Ensime from our JCG partner Jos Dirksen at the Smart Java blog.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button