Configuring Eclipse for Clojure

27.05.2014 Permalink

In preparation of the upcoming Eclipse Demo Camp in Bonn, I recently played around with Counterclockwise (CCW) and Eclipse Luna settings to enable a workflow within Eclipse that is similar to what I'm used to in Emacs.

It's really not too complicated if you have an idea what you want to have at the end, because CCW already provides almost everything out-of-the-box. This is my wishlist: To summarize, I was able to get everything on this list, only unit test execution is more cumbersome in Eclipse. Here's a screenshot:

Looks pretty cool compared to standard Eclipse, eh?

Follow the instructions

To get the software in place, Now after installation and Eclipse restart, you need to change some preferences. Go to Eclipse Preferences: That's it. You're ready to play.

Starting a project and getting into the flow

You can simply type Ctrl-N, enter "Clojure" and select the "Clojure Project" option. Enter the project name, for example "helloworld". The dialog also allows you to select a Leiningen template, but you can ignore that. Open the source file in src/helloworld/core.clj. Type Alt-Shift-X C to start the REPL. (If you're Mac user please see shortcuts below.) Drag the REPL view to the top-right and make the right side wide enough.

Type in the REPL input area (foo "Clojure") and press Ctrl-ENTER. You should see the output appearing in the upper part of the REPL view. Switch to the editor with Ctrl-F7, change the println expression, and hit right there Ctrl-ENTER to recompile the foo function. You should see #'helloworld.core/foo appearing in the REPL output area.

Now, switch back to the REPL with Ctrl-F7 and recall the last expression with Ctrl-Up. Press Ctrl-ENTER to evaluate the expression. The REPL output should reflect your change.

Continue.

Keep your hands on the keyboard, please!

I know, programming is about thinking, not typing, but fast feedback using the REPL is a crucial part of the Clojure workflow. You'll have to learn to control your tools solely by keyboard, otherwise exploration of ideas will get so time-consuming that you tend to give up too early instead of finding the best way.

Here are the shortcuts you should really train yourself:

PCMacFunction
Movement between views / tabs
Ctrl-F7Cmd-F7Change active view
Ctrl-PageUpCtrl-PageUpPrevious Tab
Ctrl-PageDownCtrl-PageDownNext Tab
REPL interaction from editor
Alt-Shift-X CAlt-Cmd-CStart REPL
Ctrl-Alt-SAlt-Cmd-SLoad whole editor contents
Ctrl-Alt-NAlt-Cmd-NSwitch REPL to files namespace
Ctrl-EnterCmd-EnterEval (toplevel or selected) sexp
In the REPL
Ctrl-EnterCmd-EnterEval sexp
Ctrl-UpCtrl-PRecall previous command in REPL history
Ctrl-DownCtrl-NRecall next command in REPL history
Selecting expressions
Alt-Shift-RightShift-Cmd-RightExpand selection to next sexp
Alt-Shift-LeftShift-Cmd-LeftExpand selection to previous sexp
Alt-Shift-UpShift-Cmd-UpExpand selection to enclosing sexp
Alt-Shift-DownShift-Cmd-DownNarrow selection
Structural editing
Ctrl-0 SCmd-0 SForward slurp
Ctrl-0 BCmd-0 BForward barf
Ctrl-9 SCmd-9 SBackward slurp
Ctrl-9 BCmd-9 BBackward barf
Alt-RAlt-RRaise selected sexp
Alt-SAlt-SSplit sexp
Alt-JAlt-JJoin sexps
Ctrl-ICmd-IReindent selected sexp
Cursor movement
F3F3Goto declaration
Ctrl-JCmd-JIncremental search
Ctrl-LCmd-LGoto line
Ctrl-Alt-AAlt-Cmd-AJump before toplevel sexp
Ctrl-Alt-EAlt-Cmd-EJump after toplevel sexp
Ctrl-Shift-PShift-Cmd-PJump to matching bracket
Leiningen
Alt-L LAlt-L LMini lein command line

To learn more about existing shortcuts go to Eclipse Preferences / General / Keys and type in "Clojure" as filter text. This should give you a list of all Clojure relevant shortcuts.

Unit test execution

I wasn't able to find any clojure.test integration in Eclipse. Which brings us back to the REPL. If you're about to create new tests then you can certainly use (run-tests) in the test namespace. In order to execute tests as a regression for a specific namespace, e.g. helloworld.core-test from wherever you are, you could issue a command like this:
(do (require 'clojure.test 'helloworld.core-test)
    (clojure.test/run-tests 'helloworld.core-test))
Since this is a little awkward to type in, you can help yourself with an injection in your ~/.lein/profiles.clj file as demonstrated in this Gist. This allows you to use
(user/test-ns 'helloworld.core-test)
instead, and through the REPL command history you can quickly re-execute it anytime. Or you put a similar injection into your project.clj that already "knows" all test namespaces.

Recap

Eclipse is originally a Java IDE, which brings its plethora of tools, views and stuff that tends to distract you from the code. Therefore you need to rearrange the perspective a bit to bring the REPL and your code into the focus again. Emacs with the right setup allows for an almost clutter-free look, which I prefer over Eclipse. Emacs also excels when it comes to keyboard control (basically it offers no other reasonable option). But after all: if you trained yourself to use the keyboard in Eclipse as much as possible then you will experience the same flow with Eclipse that I have with Emacs.