From 0e24a6e2881a614ce5532409f5de5877db0c1390 Mon Sep 17 00:00:00 2001 From: yuhan0 Date: Sun, 25 Jun 2023 20:55:35 +0800 Subject: [PATCH 1/5] feat: specify default connection params --- cider.el | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/cider.el b/cider.el index 11c81e64d..30db1761d 100644 --- a/cider.el +++ b/cider.el @@ -1540,14 +1540,21 @@ server buffer, in which case a new session for that server is created." (plist-put :session-name ses-name) (plist-put :repl-type 'cljs))))) +(defvar-local cider-connect-default-params nil + "Default plist of params to pass to `cider-connect'. +Recognized keys are :host, :port and :project-dir.") + ;;;###autoload (defun cider-connect-clj (&optional params) "Initialize a Clojure connection to an nREPL server. -PARAMS is a plist optionally containing :host, :port and :project-dir. On -prefix argument, prompt for all the parameters." +PARAMS is a plist optionally containing :host, :port and :project-dir. +If nil, use the default parameters in `cider-connect-default-params'. +When called interactively with a prefix argument, prompt for all the +parameters." (interactive "P") (cider-nrepl-connect - (thread-first params + ;; Make sure to copy the list, as the following steps will mutate it + (thread-first (or params (copy-sequence cider-connect-default-params)) (cider--update-project-dir) (cider--update-host-port) (cider--check-existing-session) From c8b9c2e30ddf565732db91393dd320738e359d69 Mon Sep 17 00:00:00 2001 From: yuhan0 Date: Wed, 8 May 2024 03:21:24 +0800 Subject: [PATCH 2/5] Add separate defcustom for cljs connection params --- cider.el | 58 ++++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 48 insertions(+), 10 deletions(-) diff --git a/cider.el b/cider.el index 30db1761d..5d53fa052 100644 --- a/cider.el +++ b/cider.el @@ -1540,21 +1540,54 @@ server buffer, in which case a new session for that server is created." (plist-put :session-name ses-name) (plist-put :repl-type 'cljs))))) -(defvar-local cider-connect-default-params nil - "Default plist of params to pass to `cider-connect'. -Recognized keys are :host, :port and :project-dir.") +(defcustom cider-connect-default-params nil + "Default plist of params for connecting to an external nREPL server. +Recognized keys are :host, :port and :project-dir. + +These are used as arguments to the commands `cider-connect-clj', +`cider-connect-cljs' and `cider-connect-clj&cljs', in order to bypass +the corresponding user prompts. + +This defcustom is intended for use with .dir-locals.el on a per-project basis. +See `cider-connect-default-cljs-params' in order to specify a separate set of params +for cljs REPL connections. + +Note: it is recommended to set the variable `cider-default-cljs-repl' +instead of specifying the :cljs-repl-type key." + :type '(plist :key-type + (choice (const :host) + (const :port) + (const :project-dir))) + :group 'cider) + +(defcustom cider-connect-default-cljs-params nil + "Default plist of params for connecting to a ClojureScript REPL. +Recognized keys are :host, :port and :project-dir. + +If non-nil, overrides `cider-connect-default-params' for the commands +`cider-connect-cljs' and (the latter half of) `cider-connect-clj&cljs'. + +Note: it is recommended to set the variable `cider-default-cljs-repl' +instead of specifying the :cljs-repl-type key." + :type '(plist :key-type + (choice (const :host) + (const :port) + (const :project-dir))) + :group 'cider) ;;;###autoload (defun cider-connect-clj (&optional params) "Initialize a Clojure connection to an nREPL server. PARAMS is a plist optionally containing :host, :port and :project-dir. If nil, use the default parameters in `cider-connect-default-params'. -When called interactively with a prefix argument, prompt for all the -parameters." + +With the prefix argument, prompt for all the parameters regardless of +their supplied or default values." (interactive "P") (cider-nrepl-connect - ;; Make sure to copy the list, as the following steps will mutate it - (thread-first (or params (copy-sequence cider-connect-default-params)) + (thread-first (or params cider-connect-default-params) + (copy-sequence) ;; Note: the following steps mutate the list + (map-delete :cljs-repl-type) (cider--update-project-dir) (cider--update-host-port) (cider--check-existing-session) @@ -1567,12 +1600,17 @@ parameters." "Initialize a ClojureScript connection to an nREPL server. PARAMS is a plist optionally containing :host, :port, :project-dir and :cljs-repl-type (e.g. 'shadow, 'node, 'figwheel, etc). +If nil, use the default parameters in `cider-connect-default-params' or +`cider-connect-default-cljs-params'. -On prefix, prompt for all the -parameters regardless of their supplied or default values." +With the prefix argument, prompt for all the parameters regardless of +their supplied or default values." (interactive "P") (cider-nrepl-connect - (thread-first params + (thread-first (or params + cider-connect-default-cljs-params + cider-connect-default-params) + (copy-sequence) (cider--update-project-dir) (cider--update-host-port) (cider--check-existing-session) From 9601d8635bf7942ba8a6238bd4c657f29d479305 Mon Sep 17 00:00:00 2001 From: yuhan0 Date: Tue, 28 May 2024 22:09:54 +0800 Subject: [PATCH 3/5] Refactor cider-connect-clj&cljs --- cider.el | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/cider.el b/cider.el index 5d53fa052..319aeead5 100644 --- a/cider.el +++ b/cider.el @@ -1623,22 +1623,27 @@ their supplied or default values." (defun cider-connect-clj&cljs (params &optional soft-cljs-start) "Initialize a Clojure and ClojureScript connection to an nREPL server. PARAMS is a plist optionally containing :host, :port, :project-dir and -:cljs-repl-type (e.g. 'shadow, 'node, 'figwheel, etc). When SOFT-CLJS-START is -non-nil, don't start if ClojureScript requirements are not met." +:cljs-repl-type (e.g. 'shadow, 'node, 'figwheel, etc). +If nil, use the default parameters in `cider-connect-default-params' and +`cider-connect-default-cljs-params'. + +When SOFT-CLJS-START is non-nil, don't start if ClojureScript requirements are +not met. + +With the prefix argument, prompt for all the parameters regardless of +their supplied or default values." (interactive "P") - (let* ((params (thread-first params - (cider--update-project-dir) - (cider--update-host-port) - (cider--check-existing-session) - (cider--update-cljs-type))) - (clj-params (thread-first params - copy-sequence - (map-delete :cljs-repl-type))) - (clj-repl (cider-connect-clj clj-params))) + (let* ((clj-repl (cider-connect-clj params)) + (cljs-params + (thread-first (or params cider-connect-default-cljs-params) + (copy-sequence) + (cider--update-cljs-type) + ;; already asked, don't ask on sibling connect + (plist-put :do-prompt nil)))) (when (if soft-cljs-start - (cider--check-cljs (plist-get params :cljs-repl-type) 'no-error) + (cider--check-cljs (plist-get cljs-params :cljs-repl-type) 'no-error) t) - (cider-connect-sibling-cljs params clj-repl)))) + (cider-connect-sibling-cljs cljs-params clj-repl)))) (defvar cider-connection-init-commands '(cider-jack-in-clj From 6b53c83316d01f640968b38c6a2222ee1c58e768 Mon Sep 17 00:00:00 2001 From: yuhan0 Date: Fri, 21 Mar 2025 22:05:30 +0800 Subject: [PATCH 4/5] Add changelog and documentation --- CHANGELOG.md | 1 + .../ROOT/pages/basics/up_and_running.adoc | 16 ++++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index fb5419278..10d0c682e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ - [#3784](https://github.com/clojure-emacs/cider/issues/3784): Inspector: make point less erratic when navigating between inspector screens. - [#3790](https://github.com/clojure-emacs/cider/issues/3790): Stacktrace: show messages and data for all exception causes by default. - [#3789](https://github.com/clojure-emacs/cider/issues/3789): Refactor and simplify exception handling. +- [#3359](https://github.com/clojure-emacs/cider/pull/3359): Add customizable default connection params ## 1.17.1 (2025-02-25) diff --git a/doc/modules/ROOT/pages/basics/up_and_running.adoc b/doc/modules/ROOT/pages/basics/up_and_running.adoc index 0d9f3a362..7f17a4594 100644 --- a/doc/modules/ROOT/pages/basics/up_and_running.adoc +++ b/doc/modules/ROOT/pages/basics/up_and_running.adoc @@ -358,6 +358,22 @@ reads for the host and port prompts when you invoke ("host-b" "7888"))) ---- +If you wish to bypass the prompts entirely, you can configure the variables +`cider-connect-default-params` and `cider-connect-default-cljs-params` +on a per-project basis using https://www.gnu.org/software/emacs/manual/html_node/emacs/Directory-Variables.html[.dir-locals.el]. + +The following configuration allows you to type kbd:[M-x] `cider-connect-clj&cljs` kbd:[RET] and instantly connect to both JVM and shadow-cljs REPLs without answering any prompts – useful when the project always uses the same fixed endpoints for its nREPL servers. + +[source,lisp] +--- +((clojure-mode + . ((cider-connect-default-params . (:host "localhost" :port 1234)) + (cider-connect-default-cljs-params . (:host "localhost" :port 5678)) + (cider-default-cljs-repl . shadow)))) +--- + +Note that the universal argument kbd:[C-u] can be used before a command to override these settings and force the prompts to be displayed. + == Working with Remote Hosts While most of the time you'd be connecting to a locally running nREPL From bdda420891d6837c66a1e999d9d20c942fbfdc54 Mon Sep 17 00:00:00 2001 From: yuhan0 Date: Fri, 21 Mar 2025 22:52:20 +0800 Subject: [PATCH 5/5] Add test --- test/integration/integration-tests.el | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/test/integration/integration-tests.el b/test/integration/integration-tests.el index 71081a995..c929949e2 100644 --- a/test/integration/integration-tests.el +++ b/test/integration/integration-tests.el @@ -616,3 +616,27 @@ If CLI-COMMAND is nil, then use the default." ;; wait for the REPL to exit (cider-itu-poll-until (not (eq (process-status nrepl-proc) 'run)) 15) (expect (member (process-status nrepl-proc) '(exit signal))))))))))) + +(describe "cider-connect-default-params" + (it "bypasses cider-connect user prompts" + (let (host-prompt port-prompt) + (spy-on 'cider--completing-read-host + :and-call-fake (lambda (_) (setq host-prompt t))) + (spy-on 'cider--completing-read-host + :and-call-fake (lambda (_) (setq port-prompt t))) + (with-temp-buffer + (setq-local cider-connect-default-params '(:host "localhost")) + (ignore-errors (call-interactively 'cider-connect)) + (expect host-prompt :to-equal nil) + (expect port-prompt :to-equal t)) + (with-temp-buffer + (setq host-prompt nil port-prompt nil) + (setq-local cider-connect-default-params + '(:host "localhost" :port 65536)) + (condition-case e + (call-interactively 'cider-connect) + (error + (expect e :to-equal + '(error "[nREPL] Direct connection to localhost:65536 failed")))) + (expect host-prompt :to-equal nil) + (expect port-prompt :to-equal nil)))))