Fork me on GitHub
#cider
<
2023-03-06
>
drcode03:03:13

Hi everyone, I'm not super familiar with the internals of cider/nrepl/etc, but I have a clojure project with a domain-specific language, and would like to be able to type DSL commands into the nrepl, and have custom clojure code that checks if it's a DSL command first, before forwarding it to the usual cider/clojure reader/evaluator. I'd appreciate any quick pointers for how I would best go about doing this, if any of you folks understand these tools a bit better. Thanks!

thheller07:03:37

sounds like you want a nrepl middleware. something that is sorted in before eval and can delegate to the regular eval, or just do something with the request itself. example: https://github.com/thheller/shadow-cljs/blob/af1a77099e69b09c3799a8b64e169cd24d379a72/src/main/shadow/cljs/devtools/server/nrepl.clj#L44-L50

thheller07:03:46

so the nrepl msg you get has a :op key. :op "eval" for evals

Carsten Behring08:03:13

Do you have an "internal DSL" (so a DSL expressed in Clojure syntax) or an external DSL (not expressed in Clojure syntax) ?

vemv09:03:30

Probably it should work like a clojurescript repl which can be understood as a "nested repl", i.e. there's a JVM Clojure repl in the above layer, and a clojurescript repl down the stack. The idea is that you're either in one repl or the other, but you don't try complecting two evaluation modes into one. You could perfectly have two repls in your emacs, each with its own history, font-locking, autocompletions, etc. Understanding https://github.com/nrepl/piggieback might be useful https://github.com/gfredericks/debug-repl also comes to mind IDK this stuff by heart but at least it should point in a good direction :)

thheller10:03:01

another option I have done with cursive is wrapping what gets send to the REPL in the first place. so instead of "eval form before caret" I have a custom REPL command that sends (tap> ~form-before-caret). While this is a cursive feature https://cursive-ide.com/userguide/repl.html#repl-commands I'm sure emacs/cider could do something similar. very flexible and doesn't require messing with nrepl in any way 🙂

drcode16:03:43

@U05224H0W thanks for the links, working through them now. As for your "wrapping in the first place suggestion" that's actually what I've been doing for the last couple of years with my dsl and wanted to see if I could make it a little fancier now

drcode16:03:04

@U7CAHM72M external dsl, not clojure syntax

drcode16:03:19

@U45T93RA6 thanks for those links, the gfredericks one is interesting, since he's a friend of mine and I can pester him for tips if need be :)