clojuretips & tricks

Connect Cursive to Figwheel REPL

I’ve used Intellij for many years so for me Cursive is the natural choice for a Clojure development environment. A couple of months ago I started working on my first project that involved Clojurescript and while Cursive has great support for Clojurescript it wasn’t totally obvious to me how to connect it to a Figwheel REPL. So this is why I’m sharing a step-by-step guide on how to achieve this. This guide assumes that you have a Leiningen project configured to use Figwheel. This is not hard if you base your project on something like Luminus but you can also read the full `lein-figwheel` documentation here. Here goes:

  1. Start Figwheel from the terminal (`lein figwheel`) and wait until it says that it has opened an nREPL port (for example `7002`)
  2. From the menu in Intellij select “Run” -> “Debug…” (or simply press (`alt`+`shift`+`F9`)) and then select “Edit Configurations…”
  3. Press the + button and add a remote Clojure REPL:
  4. Give it a name (it doesn’t matter which), for example “Clojurescript REPL” and instruct it to connect to `localhost` on port `7002`:
  5. After clicking on “Debug” you should now be connected to the figwheel repl and you can start fiddling around:
  6. Next we should use the `figwheel-sidecar.repl-api` namespace like this: `(use ‘figwheel-sidecar.repl-api)` and then we need to run the `cljs-repl` function defined in this namespace (`(cljs-repl)`):It should print roughly the same text that you saw when starting figwheel from the terminal in step 1. And now you’re good to go! You can now for example start evaluating functions defined in your own Clojurescript code that is running in the browser.

Example

Let’s actually try some evaluation now that we’re connected to the REPL. But before we can do this we also need to start our application. From inside Intellij right-click on the Leiningen `project.clj` file and click on “Debug ‘REPL for…'”
Once loaded, start the application from the REPL (if using Luminus you can do `(start)`). Now navigate to the website in your browser (for example `http://localhost:3000`).

Image that we have a clojurescript function like this that we want to evaluate from the figwheel REPL:

(ns my-app)

(defn remove-attr=
  "Remove a map from a seq if the seq has a map with a key equal to val."
  [key val coll]
  (filter (fn [other-val] (not= (key other-val) val)) coll))

To do that go to the REPL window in Intellij that is named “Remote: <namespace>” and change the namespace to `my-app`:

(in-ns 'myapp)

Next let’s try to run the function:

(remove-attr= :name "John" [{:name "John"} {:name "Doe"}])
=> ({:name "Doe"})

Hopefully you got the same output as me!

That’s it!

Leave a Reply

Your email address will not be published. Required fields are marked *