Skip to content

Commit 5c4c914

Browse files
Allow user provided commands for cider-jack-in (#3286)
1 parent f695986 commit 5c4c914

File tree

3 files changed

+103
-38
lines changed

3 files changed

+103
-38
lines changed

cider.el

Lines changed: 53 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,19 @@ specifying the artifact ID, and the second element the version number."
510510
(string :tag "Artifact ID")
511511
(string :tag "Version"))))
512512

513+
(defvar-local cider-jack-in-cmd nil
514+
"The custom command used to start a nrepl server.
515+
This is used by `cider-jack-in`.
516+
517+
If this variable is set, its value will be
518+
used as the command to start the nrepl server
519+
instead of the default command inferred from
520+
the project type.
521+
522+
This allows for fine-grained control over the jack-in process.
523+
The value should be a string representing the command to start
524+
the nrepl server, such as \"nbb nrepl-server\".")
525+
513526
(defvar cider-jack-in-lein-plugins nil
514527
"List of Leiningen plugins to be injected at jack-in.
515528
Each element is a list of artifact name and version, followed optionally by
@@ -1494,43 +1507,46 @@ PARAMS is a plist with the following keys (non-exhaustive list)
14941507
14951508
:project-type optional, the project type to create the command for; see
14961509
`cider-jack-in-command' for the list of valid types)."
1497-
(let* ((params (cider--update-do-prompt params))
1498-
(project-dir (plist-get params :project-dir))
1499-
(params-project-type (plist-get params :project-type))
1500-
(project-type (or params-project-type
1501-
(cider-project-type project-dir)))
1502-
(command (cider-jack-in-command project-type))
1503-
(command-resolved (cider-jack-in-resolve-command project-type))
1504-
(command-global-opts (cider-jack-in-global-options project-type))
1505-
(command-params (cider-jack-in-params project-type)))
1506-
(if command-resolved
1507-
(with-current-buffer (or (plist-get params :--context-buffer)
1508-
(current-buffer))
1509-
(let* ((command-params (if (plist-get params :do-prompt)
1510-
(read-string "nREPL server command: "
1511-
command-params
1512-
'cider--jack-in-nrepl-params-history)
1513-
command-params))
1514-
(cmd-params (if cider-inject-dependencies-at-jack-in
1515-
(cider-inject-jack-in-dependencies command-global-opts command-params project-type)
1516-
command-params)))
1517-
(if (or project-dir cider-allow-jack-in-without-project)
1518-
(when (or project-dir
1519-
(eq cider-allow-jack-in-without-project t)
1520-
(and (null project-dir)
1521-
(eq cider-allow-jack-in-without-project 'warn)
1522-
(or params-project-type
1523-
(y-or-n-p "Are you sure you want to run `cider-jack-in' without a Clojure project? "))))
1524-
(let ((cmd (format "%s %s" command-resolved (if (or (string-equal command "powershell")
1525-
(string-equal command "pwsh"))
1526-
(cider--powershell-encode-command cmd-params)
1527-
cmd-params))))
1528-
(plist-put params :jack-in-cmd (if (or cider-edit-jack-in-command
1529-
(plist-get params :edit-jack-in-command))
1530-
(read-string "jack-in command: " cmd 'cider--jack-in-cmd-history)
1531-
cmd))))
1532-
(user-error "`cider-jack-in' is not allowed without a Clojure project"))))
1533-
(user-error "The %s executable isn't on your `exec-path'" command))))
1510+
(cond
1511+
((plist-get params :jack-in-cmd) params)
1512+
(cider-jack-in-cmd (plist-put params :jack-in-cmd cider-jack-in-cmd))
1513+
(t (let* ((params (cider--update-do-prompt params))
1514+
(project-dir (plist-get params :project-dir))
1515+
(params-project-type (plist-get params :project-type))
1516+
(project-type (or params-project-type
1517+
(cider-project-type project-dir)))
1518+
(command (cider-jack-in-command project-type))
1519+
(command-resolved (cider-jack-in-resolve-command project-type))
1520+
(command-global-opts (cider-jack-in-global-options project-type))
1521+
(command-params (cider-jack-in-params project-type)))
1522+
(if command-resolved
1523+
(with-current-buffer (or (plist-get params :--context-buffer)
1524+
(current-buffer))
1525+
(let* ((command-params (if (plist-get params :do-prompt)
1526+
(read-string "nREPL server command: "
1527+
command-params
1528+
'cider--jack-in-nrepl-params-history)
1529+
command-params))
1530+
(cmd-params (if cider-inject-dependencies-at-jack-in
1531+
(cider-inject-jack-in-dependencies command-global-opts command-params project-type)
1532+
command-params)))
1533+
(if (or project-dir cider-allow-jack-in-without-project)
1534+
(when (or project-dir
1535+
(eq cider-allow-jack-in-without-project t)
1536+
(and (null project-dir)
1537+
(eq cider-allow-jack-in-without-project 'warn)
1538+
(or params-project-type
1539+
(y-or-n-p "Are you sure you want to run `cider-jack-in' without a Clojure project? "))))
1540+
(let ((cmd (format "%s %s" command-resolved (if (or (string-equal command "powershell")
1541+
(string-equal command "pwsh"))
1542+
(cider--powershell-encode-command cmd-params)
1543+
cmd-params))))
1544+
(plist-put params :jack-in-cmd (if (or cider-edit-jack-in-command
1545+
(plist-get params :edit-jack-in-command))
1546+
(read-string "jack-in command: " cmd 'cider--jack-in-cmd-history)
1547+
cmd))))
1548+
(user-error "`cider-jack-in' is not allowed without a Clojure project"))))
1549+
(user-error "The %s executable isn't on your `exec-path'" command))))))
15341550

15351551
(defun cider--update-host-port (params)
15361552
"Update :host and :port; or :socket-file in PARAMS."

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,40 @@ wait`)
241241
* `cider-shadow-cljs-parameters` - the task to start a REPL server (`server` by default)
242242
* `cider-shadow-cljs-global-options`
243243

244+
=== Override the Jack-In Command
245+
246+
Which Jack-In Command is used is based on the project type. You can override the Jack-In Command either project-wide or as an argument in Lisp.
247+
This allows for fine-grained control over how cider starts the nrepl-server.
248+
249+
The precedence order for determining the Jack-In Command is:
250+
1) :jack-in-cmd if provided as a parameter,
251+
2) `cider-jack-in-command` if set as a directory local variable, and
252+
3) inferred from the project type (the default).
253+
254+
==== Setting a project-wide command
255+
256+
You can set a local variable `cider-jack-in-command` to override the jack-in command.
257+
258+
[source,emacs-lisp]
259+
----
260+
((nil
261+
(cider-jack-in-cmd . "nbb nrepl-server")))
262+
----
263+
264+
==== Passing the Command Programmatically as a Parameter
265+
266+
You can provide an override Jack-In command as an argument to `cider-jack-in`.
267+
Here is an example Nbb Jack-In command, providing a custom `:jack-in-cmd`.
268+
269+
[source,emacs-lisp]
270+
----
271+
(defun cider-jack-in-nbb-2 ()
272+
"Start a Cider nREPL server with the 'nbb nrepl-server' command."
273+
(interactive)
274+
(cider-jack-in-clj '(:jack-in-cmd "nbb nrepl-server")))
275+
----
276+
277+
244278
== Connect to a Running nREPL Server
245279

246280
If you have an nREPL server already running, CIDER can connect to

test/cider-tests.el

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -523,7 +523,22 @@
523523
(spy-on 'cider-project-type :and-return-value 'clojure-cli)
524524
(spy-on 'cider-jack-in-resolve-command :and-return-value "clojure")
525525
(expect (plist-get (cider--update-jack-in-cmd nil) :jack-in-cmd)
526-
:to-equal expected)))))
526+
:to-equal expected))))
527+
(describe "Override jack-in command"
528+
(it "Uses the param, if provided"
529+
(let* ((params '(:jack-in-cmd "Snowcrash"))
530+
(params (cider--update-jack-in-cmd params)))
531+
(expect params :to-equal '(:jack-in-cmd "Snowcrash"))))
532+
(it "Uses the `cider-jack-in-cmd', if provided"
533+
(let* ((params '())
534+
(cider-jack-in-cmd "Seveneves")
535+
(params (cider--update-jack-in-cmd params)))
536+
(expect params :to-equal '(:jack-in-cmd "Seveneves"))))
537+
(it "Uses params over `cider-jack-in-cmd', if provided"
538+
(let* ((params '(:jack-in-cmd "Snowcrash"))
539+
(cider-jack-in-cmd "Seveneves")
540+
(params (cider--update-jack-in-cmd params)))
541+
(expect params :to-equal '(:jack-in-cmd "Snowcrash"))))))
527542

528543
(defmacro with-temp-shadow-config (contents &rest body)
529544
"Run BODY with a mocked shadow-cljs.edn project file with the CONTENTS."

0 commit comments

Comments
 (0)