Skip to content

Commit 7d2ed59

Browse files
authored
Add enrich-classpath integration (#3075)
1 parent 7228402 commit 7d2ed59

File tree

4 files changed

+92
-21
lines changed

4 files changed

+92
-21
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,17 @@
44

55
### New features
66

7+
* Integrate [enrich-classpath](https://github.com/clojure-emacs/enrich-classpath) by default for Leiningen projects.
8+
* This enables functionality related to Java sources, javadocs or parsing thereof.
9+
* This can slightly slow down jack-in for the _first_ time for a given project; later on the related work will be cached.
710
* [#2831](https://github.com/clojure-emacs/cider/issues/2831): Add xref integration, configured with customizable variables cider-use-xref and cider-xref-fn-depth.
811
* [#3017](https://github.com/clojure-emacs/cider/issues/3017): Annotate company completion kinds.
912
* [#3040](https://github.com/clojure-emacs/cider/pull/3040): Support invoking `cider-clojuredocs` within the `*clojuredocs*` buffer.
1013
* Make it possible to specify the version of `cider-nrepl` to use with `cider-jack-in`. See `cider-injected-middleware-version`.
1114
* Make it possible to specify the version of nREPL to use with `cider-jack-in`. See `cider-injected-nrepl-version`.
1215
* Upgrade `cider-nrepl`, `Orchard` and `clj-suitable` for pulling their latest bugfixes.
1316
* Add support for babaska projects to `cider-jack-in`.
17+
* Introduce `cider-jack-in-lein-middlewares` defcustom.
1418

1519
### Bugs fixed
1620

cider.el

Lines changed: 52 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,16 @@ Should be newer than the required version for optimal results."
455455
:package-version '(cider . "1.2.0")
456456
:safe #'stringp)
457457

458+
(defcustom cider-enrich-classpath t
459+
"Whether to use git.io/JiJVX for adding sources and javadocs to the classpath.
460+
461+
This is done in a clean manner, without interfering with classloaders.
462+
463+
Only available for Leiningen projects at the moment."
464+
:type 'boolean
465+
:group 'cider
466+
:safe #'booleanp)
467+
458468
(defcustom cider-jack-in-auto-inject-clojure nil
459469
"Version of clojure to auto-inject into REPL.
460470
If nil, do not inject Clojure into the REPL. If `latest', inject
@@ -484,25 +494,41 @@ want to inject some middleware only when within a project context.)")
484494
(cider-add-to-alist 'cider-jack-in-lein-plugins
485495
"cider/cider-nrepl" cider-injected-middleware-version)
486496

497+
(defvar cider-jack-in-lein-middlewares nil
498+
"List of Leiningen :middleware values to be injected at jack-in.
499+
500+
Necessary for plugins which require an explicit middleware name to be specified.
501+
502+
Can also facilitate using middleware in a specific order.")
503+
(put 'cider-jack-in-lein-middlewares 'risky-local-variable t)
504+
487505
(defvar cider-jack-in-cljs-lein-plugins nil
488506
"List of Leiningen plugins to be injected at jack-in.
489507
Added to `cider-jack-in-lein-plugins' (which see) when doing
490508
`cider-jack-in-cljs'.")
491509
(put 'cider-jack-in-cljs-lein-plugins 'risky-local-variable t)
492510

493-
(defun cider-jack-in-normalized-lein-plugins ()
511+
(defun cider-jack-in-normalized-lein-plugins (&optional project-type)
494512
"Return a normalized list of Leiningen plugins to be injected.
495513
See `cider-jack-in-lein-plugins' for the format, except that the list
496-
returned by this function does not include keyword arguments."
497-
(thread-last cider-jack-in-lein-plugins
498-
(seq-filter
499-
(lambda (spec)
500-
(if-let* ((pred (plist-get (seq-drop spec 2) :predicate)))
501-
(funcall pred spec)
502-
t)))
503-
(mapcar
504-
(lambda (spec)
505-
(seq-take spec 2)))))
514+
returned by this function does not include keyword arguments.
515+
516+
PROJECT-TYPE will be observed, for avoiding injecting plugins
517+
where it doesn't make sense."
518+
(let* ((corpus (if (and cider-enrich-classpath
519+
(eq project-type 'lein))
520+
(append cider-jack-in-lein-plugins
521+
'(("mx.cider/enrich-classpath" "1.4.1")))
522+
cider-jack-in-lein-plugins)))
523+
(thread-last corpus
524+
(seq-filter
525+
(lambda (spec)
526+
(if-let* ((pred (plist-get (seq-drop spec 2) :predicate)))
527+
(funcall pred spec)
528+
t)))
529+
(mapcar
530+
(lambda (spec)
531+
(seq-take spec 2))))))
506532

507533
(defvar cider-jack-in-nrepl-middlewares nil
508534
"List of Clojure variable names.
@@ -584,10 +610,10 @@ of EXCLUSIONS can be provided as well. The returned
584610
string is quoted for passing as argument to an inferior shell."
585611
(shell-quote-argument (format "[%s %S%s]" (car list) (cadr list) (cider--lein-artifact-exclusions exclusions))))
586612

587-
(defun cider-lein-jack-in-dependencies (global-opts params dependencies dependencies-exclusions lein-plugins)
613+
(defun cider-lein-jack-in-dependencies (global-opts params dependencies dependencies-exclusions lein-plugins &optional lein-middlewares)
588614
"Create lein jack-in dependencies.
589615
Does so by concatenating GLOBAL-OPTS, DEPENDENCIES, with DEPENDENCIES-EXCLUSIONS
590-
removed, LEIN-PLUGINS, and finally PARAMS."
616+
removed, LEIN-PLUGINS, LEIN-MIDDLEWARES and finally PARAMS."
591617
(concat
592618
global-opts
593619
(unless (seq-empty-p global-opts) " ")
@@ -600,7 +626,11 @@ removed, LEIN-PLUGINS, and finally PARAMS."
600626
(seq-map (lambda (plugin)
601627
(concat "update-in :plugins conj "
602628
(cider--list-as-lein-artifact plugin)))
603-
lein-plugins))
629+
lein-plugins)
630+
(seq-map (lambda (middleware)
631+
(concat "update-in :middlewares conj "
632+
middleware))
633+
lein-middlewares))
604634
" -- ")
605635
" -- "
606636
params))
@@ -614,6 +644,8 @@ one used."
614644
(let* ((deps-string (string-join
615645
(seq-map (lambda (dep)
616646
(format "%s {:mvn/version \"%s\"}" (car dep) (cadr dep)))
647+
;; NOTE: injecting Lein plugins for deps.edn projects
648+
;; seems a bit dubious, worth revisiting at some point.
617649
(append dependencies cider-jack-in-lein-plugins))
618650
" "))
619651
(middleware (mapconcat
@@ -675,13 +707,17 @@ dependencies."
675707
(cider-add-clojure-dependencies-maybe
676708
cider-jack-in-dependencies)
677709
cider-jack-in-dependencies-exclusions
678-
(cider-jack-in-normalized-lein-plugins)))
710+
(cider-jack-in-normalized-lein-plugins project-type)
711+
(if cider-enrich-classpath
712+
(append cider-jack-in-lein-middlewares
713+
'("cider.enrich-classpath/middleware"))
714+
cider-jack-in-lein-middlewares)))
679715
('boot (cider-boot-jack-in-dependencies
680716
global-opts
681717
params
682718
(cider-add-clojure-dependencies-maybe
683719
cider-jack-in-dependencies)
684-
(cider-jack-in-normalized-lein-plugins)
720+
(cider-jack-in-normalized-lein-plugins project-type)
685721
(cider-jack-in-normalized-nrepl-middlewares)))
686722
('clojure-cli (cider-clojure-cli-jack-in-dependencies
687723
global-opts

doc/modules/ROOT/pages/basics/middleware_setup.adoc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,16 @@ Use the convenient plugin for defaults, either in your project's
3131

3232
[source,clojure]
3333
----
34-
:plugins [[cider/cider-nrepl "x.y.z"]]
34+
:plugins [[cider/cider-nrepl "x.y.z"]
35+
[mx.cider/enrich-classpath "x.y.z"]]
3536
----
3637

3738
A minimal `profiles.clj` for CIDER would be:
3839

3940
[source,clojure]
4041
----
41-
{:repl {:plugins [[cider/cider-nrepl "0.25.2"]]}}
42+
{:repl {:plugins [[cider/cider-nrepl "0.27.2"]
43+
[mx.cider/enrich-classpath "1.4.1"]]}}
4244
----
4345

4446
WARNING: Be careful not to place this in the `:user` profile, as this way CIDER's

test/cider-tests.el

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,9 @@
122122
(shell-quote-argument "[nrepl/nrepl \"0.5.3\"]")
123123
" -- update-in :plugins conj "
124124
(shell-quote-argument "[cider/cider-nrepl \"0.10.0-SNAPSHOT\"]")
125+
" -- update-in :plugins conj "
126+
(shell-quote-argument "[mx.cider/enrich-classpath \"1.4.1\"]")
127+
" -- update-in :middlewares conj cider.enrich-classpath/middleware"
125128
" -- repl :headless")))
126129

127130
(it "can inject dependencies in a lein project with an exclusion"
@@ -132,6 +135,9 @@
132135
(shell-quote-argument "[nrepl/nrepl \"0.5.3\" :exclusions [org.clojure/clojure]]")
133136
" -- update-in :plugins conj "
134137
(shell-quote-argument "[cider/cider-nrepl \"0.10.0-SNAPSHOT\"]")
138+
" -- update-in :plugins conj "
139+
(shell-quote-argument "[mx.cider/enrich-classpath \"1.4.1\"]")
140+
" -- update-in :middlewares conj cider.enrich-classpath/middleware"
135141
" -- repl :headless")))
136142

137143
(it "can inject dependencies in a lein project with multiple exclusions"
@@ -141,6 +147,9 @@
141147
(shell-quote-argument "[nrepl/nrepl \"0.5.3\" :exclusions [org.clojure/clojure foo.bar/baz]]")
142148
" -- update-in :plugins conj "
143149
(shell-quote-argument "[cider/cider-nrepl \"0.10.0-SNAPSHOT\"]")
150+
" -- update-in :plugins conj "
151+
(shell-quote-argument "[mx.cider/enrich-classpath \"1.4.1\"]")
152+
" -- update-in :middlewares conj cider.enrich-classpath/middleware"
144153
" -- repl :headless")))
145154

146155
(it "can inject dependencies in a boot project"
@@ -173,6 +182,9 @@
173182
(shell-quote-argument "[refactor-nrepl \"2.0.0\"]")
174183
" -- update-in :plugins conj "
175184
(shell-quote-argument "[cider/cider-nrepl \"0.11.0\"]")
185+
" -- update-in :plugins conj "
186+
(shell-quote-argument "[mx.cider/enrich-classpath \"1.4.1\"]")
187+
" -- update-in :middlewares conj cider.enrich-classpath/middleware"
176188
" -- repl :headless")))
177189

178190
(it "can inject dependencies in a boot project"
@@ -203,6 +215,9 @@
203215
(shell-quote-argument "[nrepl/nrepl \"0.5.3\"]")
204216
" -- update-in :plugins conj "
205217
(shell-quote-argument "[cider/cider-nrepl \"0.11.0\"]")
218+
" -- update-in :plugins conj "
219+
(shell-quote-argument "[mx.cider/enrich-classpath \"1.4.1\"]")
220+
" -- update-in :middlewares conj cider.enrich-classpath/middleware"
206221
" -- repl :headless")))
207222
(it "can concat in a boot project"
208223
(expect (cider-inject-jack-in-dependencies "-C -o" "repl -s wait" 'boot)
@@ -259,12 +274,14 @@
259274
(cider-jack-in-normalized-nrepl-middlewares)
260275
(expect 'middlewares-predicate :to-have-been-called-times 1)))
261276

262-
(describe "when the middleware and plugin lists have been normalized"
277+
(describe "when the middleware and plugin lists have been normalized (Lein)"
263278
(before-each
264279
(spy-on 'cider-jack-in-normalized-nrepl-middlewares
265280
:and-return-value '("refactor-nrepl.middleware/wrap-refactor" "cider.nrepl/cider-middleware"))
266281
(spy-on 'cider-jack-in-normalized-lein-plugins
267-
:and-return-value '(("refactor-nrepl" "2.0.0") ("cider/cider-nrepl" "0.11.0")))
282+
:and-return-value '(("refactor-nrepl" "2.0.0")
283+
("cider/cider-nrepl" "0.11.0")
284+
("mx.cider/enrich-classpath" "1.4.1")))
268285
(setq-local cider-jack-in-dependencies-exclusions '()))
269286
(it "uses them in a lein project"
270287
(expect (cider-inject-jack-in-dependencies "" "repl :headless" 'lein)
@@ -274,7 +291,19 @@
274291
(shell-quote-argument "[refactor-nrepl \"2.0.0\"]")
275292
" -- update-in :plugins conj "
276293
(shell-quote-argument "[cider/cider-nrepl \"0.11.0\"]")
277-
" -- repl :headless")))
294+
" -- update-in :plugins conj "
295+
(shell-quote-argument "[mx.cider/enrich-classpath \"1.4.1\"]")
296+
" -- update-in :middlewares conj cider.enrich-classpath/middleware"
297+
" -- repl :headless"))))
298+
299+
(describe "when the middleware and plugin lists have been normalized (Boot)"
300+
(before-each
301+
(spy-on 'cider-jack-in-normalized-nrepl-middlewares
302+
:and-return-value '("refactor-nrepl.middleware/wrap-refactor" "cider.nrepl/cider-middleware"))
303+
(spy-on 'cider-jack-in-normalized-lein-plugins
304+
:and-return-value '(("refactor-nrepl" "2.0.0")
305+
("cider/cider-nrepl" "0.11.0")))
306+
(setq-local cider-jack-in-dependencies-exclusions '()))
278307
(it "uses them in a boot project"
279308
(expect (cider-inject-jack-in-dependencies "" "repl -s wait" 'boot)
280309
:to-equal (concat "-i \"(require 'cider.tasks)\""

0 commit comments

Comments
 (0)