VSCode/Calva

VSCode / Calva with side window for REPL output.

Introduction

In my previous blog post Configuring VSCode/Calva for Clojure programming I experimented if I can configure VSCode/Calva with the same look-and-feel and developer experience as my Emacs/Cider and IntelliJ/Cursive setups. And I could. In this new blog post I continue this VSCode/Calva journey describing some new configurations to make my VSCode/Calva setup as productive for Clojure programming as possible.

Joyride

After I published my previous VSCode/Calva blog post, Peter Strömberg, the Calva maintainer provided me an excellent hint in the Clojurians Slack: Joyride. Using Joyride you can extend your VSCode experience using Clojurescript. This is really nice! I haven’t yet created any Joyride scripts of my own, but I immediately copy-pasted one excellent script from Sean Corfield’s vscode-calva-setup repo: clojuredocs.cljs. This script opens a Clojure function documentation in a VSCode tab - the same functionality I found so useful in Cursive. So, I added the same keyboard shortcut to this functionality in the VSCode/Calva as I have in my IntelliJ/Cursive setup - now my muscle memory opens the documentation the same way in both editors when I have the cursor in some Clojure function. See an example of opening the take-nth function’s documentation using Joyride in the main picture of this blog post. Thanks Peter and Sean!

One important thing to mention about Joyride Peter told me later on: Joyride is a general ”Hack VS Code” extension, and not just for Calva or Clojure coding. Even if Calva does have a (slowly) growing API.

REPL Output Window Floating

I was specifically missing one feature in the Cursive side: to be able to have the REPL output window floating - this way I can have the REPL output window in my side monitor and not to allocate precious space for it in the VSCode editor in my main monitor. This is how I do it:

  1. There is File: Open Active File in New Window functionality in VSCode. Just provide a keyboard shortcut for it and use the shortcut for the output.calva-repl file. This way you get the REPL output as a separate VSCode instance that you can move to your side monitor.
  2. This new VSCode does not auto-scroll the view itself, so you need something more: AutoScroll extension by Pejman Nikram. Install this extension and activate it in the VSCode instance in which you have the file output.calva-repl open (at the bottom of VSCode status bar you should see a short AS symbol => click it).

Now, when you send S-expressions for evaluation to your REPL, you can see the S-expression and the result in your REPL output in your side monitor. See an example of having the REPL output window next to your VSCode editor in the main picture of this blog post.

One thing to understand with VSCode is that VSCode keeps everything in the same window. The Calva REPL/Output window is just a regular file, so we can make the REPL Output window “float” as explained in this chapter.

Manipulating S-expressions

In IntelliJ/Cursive I have different kill functionalities for S-expressions:

  1. Kill S-expressions until the end of this surrounding S-expression (ctrl+shift+k).
  2. Kill next S-expression only (ctrl+alt+k).
  3. Copy next S-expression (but do not actually kill it) (alt+shift+k).

The idea to use k comes from the fact that I as an old-timer Emacs user, have ctrl+k in my muscle memory for killing everything from the cursor to the end of line, and therefore it is nice to use k with various modifiers for semantically similar things in Clojure programming.

Calva comes out-of-the box with the first two functionalities:

  1. Kill S-expressions until the end of this surrounding S-expression: Calva Paredit: Kill/Delete Right.
  2. Kill next S-expression only: Calva Paredit: Kill/Delete Sexp Forward.

(NOTE: for #1 to work you need one space between your cursor and the next S-expression, or Calva deletes just the next S-expression.)

But Calva does not provide “Copy next S-expression” out-of-the-box. I asked about this feature in the Clojurians slack, and Peter Strömberg instructed me that this “Copy next S-expression” functionality is pretty easy to provide with Joyride (another reason to start using it). A copy-paste of Peter Strömberg’s instructions in the Clojurians slack / Calva channel 8.10.2022 (with minor clarifications of my own):

Place a function like this in your Joyride user_activate.cljs file (in Linux: ~/.config/joyride/scripts/user_activate.cljs):

(defn copy-forward-sexp []
  (p/do 
   (vscode/commands.executeCommand "paredit.selectForwardSexp")
   (vscode/commands.executeCommand "editor.action.clipboardCopyAction")
   (vscode/commands.executeCommand "cursorUndo")))

Then add this shortcut VSCode’s keybindings.json file:

    {
        "key": "shift+alt+k",
        "command": "joyride.runCode",
        "args": "(copy-forward-sexp)"       
    },

Or you can just put the whole sequence into the keybinding:

    {
        "key": "shift+alt+k",
        "command": "joyride.runCode",
        "args": "(require '[\"vscode\" :as vscode]) (p/do (vscode/commands.executeCommand \"paredit.selectForwardSexp\") (vscode/commands.executeCommand \"editor.action.clipboardCopyAction\") (vscode/commands.executeCommand \"cursorUndo\"))"       
    },

Peter Strömberg also informed me that you don’t need to restart VSCode or Calva for starting to use new Joyride functions. You can evaluate the function in the Joyride REPL, or in the VSCode command: Joyride: Run User Script and select user_activate.cljs.

Now I have the final k functionality in my VSCode/Calva setup. Nice!

Conclusions

VSCode is a pretty good editor. There are a lot of extensions and ways to provide new functionality using the editor. In this blog post I did some further VSCode/Calva configurations that I found useful for my Clojure programming. Especially Joyride seems to be a really powerfull extension point for Calva. I hope you find some of these configurations useful, too.

The writer is working at Metosin using Clojure in cloud projects. If you are interested to start a Clojure project in Finland or you are interested to get Clojure training in Finland you can contact me by sending email to my Metosin email address or contact me via LinkedIn.

Kari Marttila

Kari Marttila’s Home Page in LinkedIn: https://www.linkedin.com/in/karimarttila/