Fork me on GitHub
#cider
<
2021-04-09
>
Setzer2206:04:52

Hi again! Is there a similar hook for cider-eval-region/last-sexp, etc?

dpsutton06:04:31

there's not. and hooks on every eval seem kinda tough to manage if you venture into other namespaces. i'd go with that register snippet i put above and send the command to instrument functions as needed

bozhidar06:04:11

I was actually thinking of general eval hook, as it kind of makes sense, but there was never much demand for it.

dpsutton06:04:16

C-M-x to eval something and then C-c C-j x i to send your instrument form to the repl

bozhidar06:04:43

The reason why there's a hook for load-file as that we needed this for cider-auto-test-mode. Demand drives supply. 🙂

dpsutton06:04:33

i'm gonna add this to my local version of inf-clojure and test it out

bozhidar06:04:36

@dpsutton What exactly is the use case for this register trick - you want to save some snippets of code so you can easily recall them in the REPL?

dpsutton06:04:46

yeah. exactly. for things of this nature, send (stest/instrument [insert-tree* delete-min* find-min*])` something you might keep in a register

dpsutton06:04:18

i'd add this to a register as well:

(do
  (require '[clojure.string :as str])
  (require '[clojure.core.server :as server])
  (clojure.main/repl
   :prompt (fn [] (printf "%s=> " (peek (str/split (str *ns*) #"\."))))
   :read server/repl-read)

  (require '[vlaaad.reveal :as reveal])
  (add-tap (reveal/ui)))

dpsutton06:04:07

and i have a function that calls (inf-clojure--send-string inf-proc "(apply require clojure.main/repl-requires)") but i could just toss that in a register and send it

dpsutton06:04:07

another thing to keep in a register while working in the repl: (doto 'the-ns-test (require :reload) (clojure.test/run-tests))

dpsutton06:04:18

just forms you might want to send repeatedly without scrolling around a bunch between a form calling your tests and the functions you are working on

Setzer2207:04:37

Thanks for the suggestion @dpsutton. This register thing looks neat. But in my case I already have a dedicated keybinding to reinstrument my code which pretty much achieves the same purpose. The whole point of my question was to trigger this on every eval so that I wouldn't forget and uninstrument my code by mistake: I have a habit of re-evaluating defns with cider-eval-defun-at-point, but this right now removes instrumentation. I take from your comment that you consider this "run some code on every eval" to be a bad idea 😅, but I don't see why it would cause issues, as long as the code I'm sending does not assume a particular namespace (i.e. only uses fully-qualified symbols known to be available)

Setzer2207:04:45

> I was actually thinking of general eval hook, as it kind of makes sense, but there was never much demand for it. @bozhidar I, for one, would very much appreciate this 😄, but I also understand if you think it's a bad idea. I can always do some hack on my side to get this working for me

dpsutton14:04:42

that's fair. sorry i pushed so hard for a different solution

dpsutton14:04:50

things you could do while waiting to see if an eval hook shows up: - add advice (advice-add cider-eval-last-sexp :after (lambda (&rest r) (that-function-you-already-have-to-instrument)) (i haven't tested this and have never used advice so not sure if this syntax is correct but the gist is there - make a new function that calls cider-eval-last-sexpand then calls the other function. You could make a function personal/instrument that adds your new functions into the cider maps so that it is used everywhere and then a way to turn it off by replacing the standard bindings - add advice to cider-interactive-eval to call your instrumenting function - you could advice/redefine cider-interactive-eval-handler. couple places in there you could a) add a hook and use it, just call out and run your own function