vlaaad / reveal

Read Eval Visualize Loop for Clojure
https://vlaaad.github.io/reveal/
MIT License
597 stars 21 forks source link

Editor integration instructions using .nrepl.edn does not work for Emacs CIDER #38

Open ikappaki opened 2 years ago

ikappaki commented 2 years ago

Hi,

the .nrepl.edn Nrepl-based editor integration instructions unfortunately do not work for Emacs CIDER.

Nrepl-based editors

For development workflows that require nREPL (e.g. Calva, Emacs with Cider) Reveal has a middleware that will show evaluation results produced by nrepl: vlaaad.reveal.nrepl/middleware, you will need to add it to your middleware list. The minimum required version of nrepl is 0.6.0.

Example of using this middleware with command line nrepl entry point:

...

Alternatively, you can create .nrepl.edn file in your project directory that will be picked up by nrepl. Example .nrepl.edn file:

{:middleware [vlaaad.reveal.nrepl/middleware]}

...

To reproduce

  1. Create an empty deps.edn file:
    {}
  2. In the same directory create a .nrepl.edn file with reveal middleware:
    {:middleware [vlaaad.reveal.nrepl/middleware]}
  3. Open the deps.edn project file and jack in to the REPL with M-x cider-jack-in-clj RET.
  4. Cider brings up an nREPL and connects to it, but there is no reveal middleware injected, i.e. no reveal window pops up.

This is because cider makes uses the --middleware command line option when invoking nREPL which takes precedence over the corresponding :middleware [vlaaad.reveal.nrepl/middleware option in .nrepl:

Settings passed via the command-line interface will take precedence over settings specified via the configuration files.

Luckily, cider has a couple of configuration variables that can be set to download extra dependencies and inject nREPL middlewares (cider-jack-in-dependencies and cider-jack-in-nrepl-middlewares respectively), that can be used to load reveal as part of the REPL upbringing process (called jack-in in cider's nomenclature).

Perhaps you might want to add the following in the documentation.

To configure CIDER to download and inject revaal in the nREPL middleware list:

First add a function in your Emacs init file that configures CIDER to download and inject the given reveal version in the nREPL middleware list at jack in:

(defun reveal-cider-add! (version)
  "Injects vlaaad/reveal VERSION to cider's jack-in middleware list.

   It only takes effect on the current buffer."
  (require 'cider)
  (make-local-variable 'cider-jack-in-dependencies)
  (make-local-variable 'cider-jack-in-nrepl-middlewares)  
  (cider-add-to-alist 'cider-jack-in-dependencies "vlaaad/reveal" version)
  (add-to-list 'cider-jack-in-nrepl-middlewares "vlaaad.reveal.nrepl/middleware"))

Make sure you either eval the above function or restart Emacs for it to take effect and then open your Clojure project file (e.g. deps.edn) and invoke M-x add-file-local-variable-prop-line RET eval RET (reveal-cider-add! "1.3.270") RET so that the function is called every time the file is opened. It will add the following magic line as a comment at the top of the file:

;; -*- eval: (reveal-cider-add! "1.3.270"); -*-
...

Jacking in from the project file should inject the reveal middleware and bring up the reveal window at REPL startup. The above command can be added to any file you usually jack in from, not just the project file.

Alternatively, you can instruct Emacs to run the function from any Clojure in the project. To do so open a file at your project root directory (e.g. deps.edn), and invoke M-x add-dir-local-variable RET clojure-mode RET eval RET (reveal-cider-add! "1.3.270") RET. This will create a .dir-locals.el file that instructs Emacs to run the cider configuration function for any file that loads clojure-mode in the project directory tree:

;;; Directory Local Variables
;;; For more information see (info "(emacs) Directory Variables")

((clojure-mode . ((eval . (reveal-cider-add! "1.3.270")))))

You might need to reopen the Clojure files for the above changes to take effect. You will be prompted if you would like to eval the function when opening the file.

See Specifying File Variables and Directory Local Variables in the Emacs manual for more information.

There is yet a third, more intrusive way, to have cider always download and inject the reveal middleware in every jack in invocation irrespective of project file or directory, by adding the following into your emacs init file:

(require 'cider)
(cider-add-to-alist 'cider-jack-in-dependencies "vlaaad/reveal" "1.3.270")
(add-to-list 'cider-jack-in-nrepl-middlewares "vlaaad.reveal.nrepl/middleware")

I hope this is useful!

Thanks

manenko commented 1 year ago

I solved the issue a bit differently, though the idea is similar: https://github.com/clojure-emacs/cider/issues/2927#issuecomment-1194489852