Skip to content

Commit 4f7daaa

Browse files
committed
Move universal jack-in and project-type detection to cider-jack-in.el
cider-jack-in-universal is the obvious peer to cider-jack-in-clj/cljs, all of which already live in cider-jack-in.el; pulling these final bits in completes the jack-in subsystem. Moved: - cider--identify-buildtools-present and cider-project-type - project type detection that walks cider-jack-in-tools (already in this file) - cider--effective-jack-in-default - auto-detect helper for the default tool when cider-jack-in-default is nil - cider--universal-jack-in-tools, cider--universal-jack-in-opts, and cider-jack-in-universal - the dispatcher driven by :universal-prefix-arg cider-jack-in.el forward-declares cider-preferred-build-tool and cider-jack-in-default (still defcustoms in cider.el). The ;;;###autoload cookie on cider-jack-in-universal is preserved. cider.el drops from 1181 to 1070 lines.
1 parent fc0c0c0 commit 4f7daaa

2 files changed

Lines changed: 116 additions & 111 deletions

File tree

lisp/cider-jack-in.el

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@
5454
(defvar cider-clojure-cli-aliases)
5555
(defvar cider-clojure-cli-global-aliases)
5656
(defvar cider-enable-nrepl-jvmti-agent)
57+
(defvar cider-preferred-build-tool)
58+
(defvar cider-jack-in-default)
5759
(declare-function cider--update-params "cider")
5860
(declare-function cider-connect-sibling-clj "cider")
5961
(declare-function cider-connect-sibling-cljs "cider")
@@ -733,6 +735,120 @@ only when the ClojureScript dependencies are met."
733735
(cider-connect-sibling-cljs params clj-repl))
734736
(cider-connect-sibling-cljs params clj-repl))))))))
735737

738+
739+
;;; Project type detection and universal jack-in
740+
741+
(defun cider--identify-buildtools-present (&optional project-dir)
742+
"Identify build systems present by their build files in PROJECT-DIR.
743+
PROJECT-DIR defaults to the current project. The set of recognized build
744+
files is derived from the :project-files entries in `cider-jack-in-tools'."
745+
(let ((default-directory (or project-dir (clojure-project-dir (cider-current-dir)))))
746+
(delq nil
747+
(mapcar (lambda (entry)
748+
(when (seq-some #'file-exists-p
749+
(plist-get (cdr entry) :project-files))
750+
(car entry)))
751+
cider-jack-in-tools))))
752+
753+
(defun cider-project-type (&optional project-dir)
754+
"Determine the type of the project in PROJECT-DIR.
755+
When multiple project file markers are present, check for a preferred build
756+
tool in `cider-preferred-build-tool', otherwise prompt the user to choose.
757+
PROJECT-DIR defaults to the current project."
758+
(let* ((choices (cider--identify-buildtools-present project-dir))
759+
(multiple-project-choices (> (length choices) 1))
760+
;; this needs to be a string to be used in `completing-read'
761+
(default (symbol-name (car choices)))
762+
;; `cider-preferred-build-tool' used to be a string prior to CIDER
763+
;; 0.18, therefore the need for `cider-maybe-intern'
764+
(preferred-build-tool (cider-maybe-intern cider-preferred-build-tool)))
765+
(cond ((and multiple-project-choices
766+
(member preferred-build-tool choices))
767+
preferred-build-tool)
768+
(multiple-project-choices
769+
(intern
770+
(completing-read
771+
(format "Which command should be used (default %s): " default)
772+
choices nil t nil nil default)))
773+
(choices
774+
(car choices))
775+
;; If we're outside a project, fall back to the configured (or
776+
;; auto-detected) default tool. `cider-jack-in-default' used to
777+
;; be a string prior to CIDER 0.18, hence `cider-maybe-intern'.
778+
(t (cider--effective-jack-in-default)))))
779+
780+
(defun cider--effective-jack-in-default ()
781+
"Return `cider-jack-in-default', auto-detecting when nil.
782+
Auto-detect picks `clojure-cli' if \"clojure\" is on PATH at call time,
783+
otherwise `lein'."
784+
(or (cider-maybe-intern cider-jack-in-default)
785+
(if (and (not (file-remote-p default-directory))
786+
(executable-find "clojure"))
787+
'clojure-cli
788+
'lein)))
789+
790+
(defun cider--universal-jack-in-tools ()
791+
"Return the subset of `cider-jack-in-tools' usable by `cider-jack-in-universal'.
792+
Each entry is a tool that has a :universal-prefix-arg."
793+
(seq-filter (lambda (entry) (plist-get (cdr entry) :universal-prefix-arg))
794+
cider-jack-in-tools))
795+
796+
(defun cider--universal-jack-in-opts (project-type)
797+
"Build the params plist for `cider-jack-in-universal' for PROJECT-TYPE.
798+
The returned plist forces project dir editing and carries the cljs REPL
799+
type (when applicable) so the right entry point can dispatch."
800+
(let* ((spec (cider--jack-in-tool project-type))
801+
(opts (list :project-type project-type :edit-project-dir t)))
802+
(when-let* ((cljs-type (plist-get spec :cljs-repl-type)))
803+
(setq opts (plist-put opts :cljs-repl-type cljs-type)))
804+
opts))
805+
806+
;;;###autoload
807+
(defun cider-jack-in-universal (arg)
808+
"Start and connect to an nREPL server for the current project or ARG project id.
809+
810+
If a project is found in current dir, call `cider-jack-in' passing ARG as
811+
first parameter, of which see. Otherwise, ask user which project type to
812+
start an nREPL server and connect to without a project.
813+
814+
But if invoked with a numeric prefix ARG, then start an nREPL server for
815+
the project type denoted by ARG number and connect to it, even if there is
816+
no project for it in the current dir.
817+
818+
The supported project tools are those in `cider-jack-in-tools' that have a
819+
:universal-prefix-arg key.
820+
821+
You can pass a numeric prefix argument n with `M-n` or `C-u n`.
822+
823+
For example, to jack in to leiningen which is assigned to prefix arg 2 type
824+
825+
M-2 \\[cider-jack-in-universal]."
826+
(interactive "P")
827+
(let ((cpt (clojure-project-dir (cider-current-dir))))
828+
(if (or (integerp arg) (null cpt))
829+
(let* ((tools (cider--universal-jack-in-tools))
830+
(project-type
831+
(cond
832+
((null arg)
833+
(intern (completing-read
834+
"No project found in current dir, select project type to jack in: "
835+
(mapcar #'car tools) nil t)))
836+
(t
837+
(or (car (seq-find (lambda (entry)
838+
(eql arg (plist-get (cdr entry) :universal-prefix-arg)))
839+
tools))
840+
(error ":cider-jack-in-universal :unsupported-prefix-argument %S :no-such-project"
841+
arg)))))
842+
(opts (cider--universal-jack-in-opts project-type))
843+
(jack-in-type (or (plist-get (cider--jack-in-tool project-type) :jack-in-type)
844+
'clj)))
845+
(pcase jack-in-type
846+
('clj (cider-jack-in-clj opts))
847+
('cljs (cider-jack-in-cljs opts))
848+
(_ (error ":cider-jack-in-universal :jack-in-type-unsupported %S" jack-in-type))))
849+
850+
(cider-jack-in-clj arg))))
851+
736852
(provide 'cider-jack-in)
737853

738854
;;; cider-jack-in.el ends here

lisp/cider.el

Lines changed: 0 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -1024,117 +1024,6 @@ see also `cider-clear-running-nrepl-paths-cache'."
10241024
(cons now paths))
10251025
paths))))
10261026

1027-
(defun cider--identify-buildtools-present (&optional project-dir)
1028-
"Identify build systems present by their build files in PROJECT-DIR.
1029-
PROJECT-DIR defaults to the current project. The set of recognized build
1030-
files is derived from the :project-files entries in `cider-jack-in-tools'."
1031-
(let ((default-directory (or project-dir (clojure-project-dir (cider-current-dir)))))
1032-
(delq nil
1033-
(mapcar (lambda (entry)
1034-
(when (seq-some #'file-exists-p
1035-
(plist-get (cdr entry) :project-files))
1036-
(car entry)))
1037-
cider-jack-in-tools))))
1038-
1039-
(defun cider-project-type (&optional project-dir)
1040-
"Determine the type of the project in PROJECT-DIR.
1041-
When multiple project file markers are present, check for a preferred build
1042-
tool in `cider-preferred-build-tool', otherwise prompt the user to choose.
1043-
PROJECT-DIR defaults to the current project."
1044-
(let* ((choices (cider--identify-buildtools-present project-dir))
1045-
(multiple-project-choices (> (length choices) 1))
1046-
;; this needs to be a string to be used in `completing-read'
1047-
(default (symbol-name (car choices)))
1048-
;; `cider-preferred-build-tool' used to be a string prior to CIDER
1049-
;; 0.18, therefore the need for `cider-maybe-intern'
1050-
(preferred-build-tool (cider-maybe-intern cider-preferred-build-tool)))
1051-
(cond ((and multiple-project-choices
1052-
(member preferred-build-tool choices))
1053-
preferred-build-tool)
1054-
(multiple-project-choices
1055-
(intern
1056-
(completing-read
1057-
(format "Which command should be used (default %s): " default)
1058-
choices nil t nil nil default)))
1059-
(choices
1060-
(car choices))
1061-
;; If we're outside a project, fall back to the configured (or
1062-
;; auto-detected) default tool. `cider-jack-in-default' used to
1063-
;; be a string prior to CIDER 0.18, hence `cider-maybe-intern'.
1064-
(t (cider--effective-jack-in-default)))))
1065-
1066-
(defun cider--effective-jack-in-default ()
1067-
"Return `cider-jack-in-default', auto-detecting when nil.
1068-
Auto-detect picks `clojure-cli' if \"clojure\" is on PATH at call time,
1069-
otherwise `lein'."
1070-
(or (cider-maybe-intern cider-jack-in-default)
1071-
(if (and (not (file-remote-p default-directory))
1072-
(executable-find "clojure"))
1073-
'clojure-cli
1074-
'lein)))
1075-
1076-
(defun cider--universal-jack-in-tools ()
1077-
"Return the subset of `cider-jack-in-tools' usable by `cider-jack-in-universal'.
1078-
Each entry is a tool that has a :universal-prefix-arg."
1079-
(seq-filter (lambda (entry) (plist-get (cdr entry) :universal-prefix-arg))
1080-
cider-jack-in-tools))
1081-
1082-
(defun cider--universal-jack-in-opts (project-type)
1083-
"Build the params plist for `cider-jack-in-universal' for PROJECT-TYPE.
1084-
The returned plist forces project dir editing and carries the cljs REPL
1085-
type (when applicable) so the right entry point can dispatch."
1086-
(let* ((spec (cider--jack-in-tool project-type))
1087-
(opts (list :project-type project-type :edit-project-dir t)))
1088-
(when-let* ((cljs-type (plist-get spec :cljs-repl-type)))
1089-
(setq opts (plist-put opts :cljs-repl-type cljs-type)))
1090-
opts))
1091-
1092-
;;;###autoload
1093-
(defun cider-jack-in-universal (arg)
1094-
"Start and connect to an nREPL server for the current project or ARG project id.
1095-
1096-
If a project is found in current dir, call `cider-jack-in' passing ARG as
1097-
first parameter, of which see. Otherwise, ask user which project type to
1098-
start an nREPL server and connect to without a project.
1099-
1100-
But if invoked with a numeric prefix ARG, then start an nREPL server for
1101-
the project type denoted by ARG number and connect to it, even if there is
1102-
no project for it in the current dir.
1103-
1104-
The supported project tools are those in `cider-jack-in-tools' that have a
1105-
:universal-prefix-arg key.
1106-
1107-
You can pass a numeric prefix argument n with `M-n` or `C-u n`.
1108-
1109-
For example, to jack in to leiningen which is assigned to prefix arg 2 type
1110-
1111-
M-2 \\[cider-jack-in-universal]."
1112-
(interactive "P")
1113-
(let ((cpt (clojure-project-dir (cider-current-dir))))
1114-
(if (or (integerp arg) (null cpt))
1115-
(let* ((tools (cider--universal-jack-in-tools))
1116-
(project-type
1117-
(cond
1118-
((null arg)
1119-
(intern (completing-read
1120-
"No project found in current dir, select project type to jack in: "
1121-
(mapcar #'car tools) nil t)))
1122-
(t
1123-
(or (car (seq-find (lambda (entry)
1124-
(eql arg (plist-get (cdr entry) :universal-prefix-arg)))
1125-
tools))
1126-
(error ":cider-jack-in-universal :unsupported-prefix-argument %S :no-such-project"
1127-
arg)))))
1128-
(opts (cider--universal-jack-in-opts project-type))
1129-
(jack-in-type (or (plist-get (cider--jack-in-tool project-type) :jack-in-type)
1130-
'clj)))
1131-
(pcase jack-in-type
1132-
('clj (cider-jack-in-clj opts))
1133-
('cljs (cider-jack-in-cljs opts))
1134-
(_ (error ":cider-jack-in-universal :jack-in-type-unsupported %S" jack-in-type))))
1135-
1136-
(cider-jack-in-clj arg))))
1137-
11381027

11391028
(defcustom cider-connection-message-fn #'cider-random-words-of-inspiration
11401029
"The function to use to generate the message displayed on connect.

0 commit comments

Comments
 (0)