IntelliJ (Cursive) REPL evaluation in namespace

2k views Asked by At

I use IntelliJ in order to write Clojure. I use the built-in REPL to run my code. I don't know why, but after I run my project, I always have to do “Switch REPL NS to current file” and then "Load file in REPL" in order to use the REPL for main namespace. I want to ask whether there is a way avoid these clicks and working with the REPL immediately after the run is over.

1

There are 1 answers

2
NielsK On BEST ANSWER

In Cursive, there are two ways of sending forms to the currently active REPL:

  1. From the REPL command line at the bottom of the REPL window, press the key combination associated with the key binding Execute Current Statement (default Ctrl-Enter)
  2. From the File Editor window, put the caret in a form, press the key combination associated with the key binding Send top form to REPL (default Ctrl-Shift-P, I rebound it to Ctrl-Enter)

From the REPL command line, forms will always be evaluated in the current namespace of the REPL (user by default). If you want to evaluate the form in another namespace, you will first have to switch to that namespace by either:

  • an (ns namespace) command
  • pressing the the key combination associated with the key binding Switch REPL NS to current file (default Ctrl-Shift-N)

However, when you send the form from the file editor, it can infer which namespace you want it executed in from the file itself. In the background, it will:

  1. switch the active REPL namespace to the file namespace
  2. evaluate the selected form in the REPL
  3. switch back to the previously active REPL namespace.

This way you won't ever have to switch the namespaces in the REPL, and there's far less chances of namespace mix-ups.

However, it will only do that if the setting Evaluate forms in REPL namespace is unchecked. If not, it will evaluate the form in the current REPL namespace, just as if you entered it through the REPL commandline, needing the same manual switching of namespaces.