Fork me on GitHub
#calva
<
2024-02-11
>
Ingy döt Net16:02:58

I have a bunch of things like this in my keybindings.json

{ "key": "ctrl+t ctrl+t",
  "command": "calva.runCustomREPLCommand",
  "args": "(yamltest.core/run)" },
They only seem to do stuff when a .clj file is the active pane. I'd like to have them trigger from any pane and I'll keep track of what clj file they were last triggered from and apply them as if they came from there. Is this possible?

pez16:02:41

Not sure what you mean with > I’ll keep track of what clj file they were last triggered from and apply them as if they came from there But otherwise I think we could make this possible. There are a lot of code paths in Calva that check if it’s a Clojure file, but I think that most often this is not necessary.

pez16:02:05

Or, most often… Anyway, it is not always necessary. And you point at a case where we shouldn’t care if it’s a Clojure file or not, I think. It will be necessary for some of what’s going on with custom repl commands (like figuring out current ns and such). But other than that we should execute the command.

Ingy döt Net16:02:46

I use that binding to run the tests that apply to the clj file I'm in. But the tests are defined in a YAML file. So when I change a test I need to switch to the right clj file to run it. I can keep track of the last clj file I ran tests for and use that namespace if I'm in some other file.

pez16:02:21

For now, I think Joyride is the way to solve your use case.

Ingy döt Net16:02:28

I'm generally just testing a single clj file for whatever I'm working on, but I might be changing any number of other files when I want to run the tests.

pez16:02:31

Issue welcome for making custom repl commands work in non-clojure files.

Ingy döt Net16:02:58

ok. I need to remember if I've used joyride before...

Bob B16:02:59

I haven't looked at the code, but looking at the docs, there are some fields for the custom REPL commands (e.g. ns, repl) that default to values associated with the current editor. Since those aren't set in this case, that could be a stumbling block (if Calva's code is checking the non-clj editor for which repl session and ns to use). I'd guess it might also be behind the "current editor is a clojure file" wall, but if it's not, then adding those props might make some commands work.

Ingy döt Net16:02:56

Thanks @U013JFLRFS8, I'll try that out. (Unless @U0ETXRFEW thinks it's a deadend...)

Ingy döt Net17:02:01

Would be cool if the ns value could be a function that returned a namespace. :)

Bob B17:02:49

having now tried it, I think it might be a deadend

pez17:02:54

Yeah, I’d like that.

pez17:02:07

With Joyride it would be something like so:

{ "key": "ctrl+t ctrl+t",
  "command": "joyride.runCode",
  "args": "(require '[\"$v1\" :as calva]) (calva/repl.evaluateCode \"clj\" \"(yamltest.core/run)\")" }
The require is only needed once, and may be something you do on some other shortcut, or as part of your user or workspace activation script.

pez17:02:32

Also, you will probably want to host most of the code in Joyride scripts, instead of in the keybinding JSON.

Bob B17:02:07

because adding an ns and a repl doesn't appear to change the behavior

Ingy döt Net17:02:31

a bit off topic, but do you know a clean way to eval a form in a particular namespace? something like (within-ns foo (...))

Ingy döt Net17:02:25

(where foo is a namespace)

Ingy döt Net17:02:15

I don't recall seeing anything like that before

Ingy döt Net17:02:54

Figured out:

user=> (binding [*ns* (the-ns 'foo.bar)] *ns*)
#namespace[foo.bar]

Ingy döt Net17:02:02

That's exactly what I wanted 🙂

pez19:02:52

Providing an ns with the custom repl command snippet should do it, right?

pez19:02:13

And with the Joyride option you can provide the ns with the Calva API call: https://calva.io/api/#replevaluatecode