Hello! I'm new to Clojure. I use Calva & VSCode. I use clj-reload and find myself regularly carrying the code
(comment
(reload!))
with me across all files and evaluating it.
Is there a way to create a shortcut that will send (reload!) into the REPL?Yep, it's done like that
[
{
"key": "alt+shift+r",
"command": "calva.runCustomREPLCommand",
"args": "(reload!)"
}
]This project isn't ready for a formal release yet but I wanted to float it here first, since Calva has such a focus on being beginner-friendly:
https://github.com/seancorfield/rephrase 0.1.0-SNAPSHOT -- Rephrase exceptions to be more beginner-friendly
It can be used as nREPL middleware by adding org.corfield.rephrase.nrepl/wrap-rephrase to the list of middleware when starting a REPL. I don't know if that's something that can easily be configured for Calva. I know you can copy the jack-in command to the clipboard and then run it manually to start a REPL, e.g., :
clojure -Sdeps '{:deps {nrepl/nrepl {:mvn/version,"1.7.0"},cider/cider-nrepl {:mvn/version,"0.59.0"}}}' -M -m nrepl.cmdline --middleware "[cider.nrepl/cider-middleware]"
The change here would be to use:
clojure -Sdeps '{:deps {nrepl/nrepl {:mvn/version,"1.7.0"},cider/cider-nrepl {:mvn/version,"0.59.0"},org.corfield/rephrase {:mvn/version "0.1.0-SNAPSHOT"}}}' -M -m nrepl.cmdline --middleware "[cider.nrepl/cider-middleware,org.corfield.rephrase.nrepl/wrap-rephrase]"
Then connect to it from Calva or Leiningen or... more in 🧵> lein repl :connect localhost:42671
Connecting to nREPL at localhost:42671
REPL-y 0.5.1, nREPL 1.7.0
Clojure 1.12.5-rc1
OpenJDK 64-Bit Server VM 26-ea+29-2803
Reflection warning, NO_SOURCE_PATH:1:1580 - reference to field ns can't be resolved.
Docs: (doc function-name-here)
(find-doc "part-of-name-here")
Source: (source function-name-here)
Javadoc: (javadoc java-object-or-class-here)
Exit: Control+D or (exit) or (quit)
Results: Stored in vars *1, *2, *3, an exception in *e
user=> (/ 1 :two)
Expected a number, but was given a keyword, at user/eval9452 (REPL:1) - runtime error (unexpected type).Calva's inline evaluation display only shows the first line of an exception, so rephrase deliberately puts the cause first, then the location, and then the "summary" (which is the whole of line 1 in Clojure by default), so errors can be read more easily inline.
The mappings are all in an EDN file https://github.com/seancorfield/rephrase/blob/main/resources/org/corfield/rephrase/config.edn I plan to add a user-level file that will merge in, so that folks can develop their own additional mappings, with a view to those being added to the core library over time.
It would be great if Calva added a setting to auto-add rephrase to the jack-in command (and a version under the existing nREPL version stuff), so it could be easily enabled for beginners (and experts, if you want!) 🙂 🙂
More examples:
user=> (/)
/ was called with no arguments, but it requires at least one argument, at user/eval18704 (REPL:1) - runtime error (incorrect number of arguments).
user=> (/ 1)
1
user=> (/ 1 0)
Tried to divide by zero, at user/eval18708 (REPL:1) - runtime error (ArithmeticException).
user=> (:k)
The keyword :k was used as a function, with no arguments, but it expects one or two arguments, at user/eval18710 (REPL:1) - runtime error (incorrect number of arguments).
user=> (:k 1 2 3)
The keyword :k was used as a function, with 3 arguments, but it expects one or two arguments, at user/eval18712 (REPL:1) - runtime error (incorrect number of arguments).
user=> (1 2 3)
Expected a function, but was given a number, at user/eval18714 (REPL:1) - runtime error (unexpected type).
user=> (/ 1 :zero)
Expected a number, but was given a keyword, at user/eval18716 (REPL:1) - runtime error (unexpected type).Follow-up here or in #rephrase
Yes, would be nice with a setting to enable this. Meanwhile, I think it should work adding it via a connect sequence with extraNReplMiddleware
That would need the group/artifact, the version, and the function to add to the middleware list, right?
I don’t remember right now what’s involved….
Ah, extraNReplMiddleware is an array of strings to add middleware, but not for the dependency itself...
There would need to be another option added for the group/artifact and version, for additional dependencies for jack-in.
Yeah, but to experiment with it you’d add that in deps.edn and use the custom connect sequence to get the middleware into place. For the feature as such, I think it should just be a toggle in settings and Calva would inject both the dependencies and the middleware.
Well, for experimenting right now, I still start my REPL with a repl script that uses the :dev/repl alias from my dot-clojure repo, which adaptively starts a REPL based on what is on the classpath 🙂
The doc for stacktraces in the output window says
You can also print the stack trace for any error message printed to the REPL Window via the Codelens button below it.
but I don't see a Codelens for the stacktrace in the output window anymore (see attached).
Print Last Stacktrace works fine. Is this something that should be updated in the docs, or am I somehow missing the button?
https://calva.io/repl-window/#stack-tracesThe code lens should be in the REPL Window. Your screenshot shows the Output terminal. Is that the confusion?
This could very well be my misunderstanding. I also don't see it n (what I think is) the REPL window. Or should I be looking for another REPL window? Here's a screenshot of what I see
(That editor comes from Calva: Show/Open REPL Window)
Ha! I just realized that I accidentally put the period in the wrong place and was generating a different exception (IllegalArgumentException)... But it's still missing even when "fixing" that
That looks like the REPL Window. Could be that the codelens feature is broken.
I'm not sure if it's just me (I have a dev version of clojure-lsp, maybe that's causing a problem)
clojure-lsp is not involved in this, so shouldn’t matter. It’s probably broken in general. We should remove the feature, I think….
It’s a cute feature, but not really super necessary.
I was going to suggest that too... but... I had to google how to show exception stack traces when I couldn't find the button. It's easy enough to google though
We could surface the command in the REPL menu, maybe. So if there’s a stack trace the REPL statusbar item could show some indication and the menu could have that option.
That would make it more generally available and not hidden in a REPL Window codelens.
Dear Calva friends: calva https://github.com/BetterThanTomorrow/calva/releases/tag/v2.0.586 • Fix: https://github.com/BetterThanTomorrow/calva/pull/2936, thanks @djblue! ❤️ 🙏 • https://github.com/BetterThanTomorrow/calva/issues/3103, preparing Calva for the nrepl-server-adding update of #glojure and #gloat
Thanks for the quick response and release @pez!