Skip to content

Commit da1540a

Browse files
committed
[Fix #828] Fix completion issues and don't forget project directory in cider-connect
- Add default values to fix `ido-ubiquitous` and standard `completing-read` - Look into current project directory for `nrepl-port` files - Generalize and rename `nrepl-default-port` -> `nrepl-extract-port` - Make sure that ports are always found in the right host (when connecting from remote files to localhost, or connection to different remotes than the current file's host)
1 parent 165b75b commit da1540a

File tree

3 files changed

+52
-49
lines changed

3 files changed

+52
-49
lines changed

cider-util.el

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -157,14 +157,15 @@ to `fill-column'."
157157
"Join all STRINGS using SEPARATOR."
158158
(mapconcat 'identity strings separator))
159159

160-
(defun cider-join-with-val-prop (candidates &optional sep)
161-
"Each element EL in CANDIDATES join with SEP and set :val property to EL.
162-
Useful for `completing-read' when candidates are complex objects."
160+
(defun cider-join-into-alist (candidates &optional separator)
161+
"Make an alist from CANDIDATES.
162+
The keys are the elements joined with SEPARATOR and values are the original
163+
elements. Useful for `completing-read' when candidates are complex
164+
objects."
163165
(mapcar (lambda (el)
164-
(propertize (if (listp el)
165-
(cider-string-join el (or sep ":"))
166-
(format "%s" el))
167-
:val el))
166+
(if (listp el)
167+
(cons (cider-string-join el (or separator ":")) el)
168+
(cons el el)))
168169
candidates))
169170

170171
(provide 'cider-util)

cider.el

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ Create REPL buffer and start an nREPL client connection."
146146
"Interactively select the host and port to connect to."
147147
(let* ((ssh-hosts (cider--ssh-hosts))
148148
(hosts (-distinct (append (when cider-host-history
149-
(list (car cider-host-history )))
149+
(list (list (car cider-host-history))))
150150
(list (list (nrepl-current-host)))
151151
cider-known-endpoints
152152
ssh-hosts
@@ -159,17 +159,20 @@ Create REPL buffer and start an nREPL client connection."
159159
(not (assoc-string host ssh-hosts))))
160160
;; Each lein-port is a list of the form (dir port)
161161
(lein-ports (if local-p
162-
(let ((default-directory (if (file-remote-p default-directory)
163-
"~/"
164-
default-directory)))
165-
(cider-locate-running-nrepl-ports))
166-
(let ((vec (vector "ssh" nil host "" nil)))
162+
;; might connect to localhost from a remote file
163+
(let* ((change-dir-p (file-remote-p default-directory))
164+
(default-directory (if change-dir-p "~/" default-directory)))
165+
(cider-locate-running-nrepl-ports (unless change-dir-p default-directory)))
166+
(let ((vec (vector "ssh" nil host "" nil))
167+
;; might connect to a different remote
168+
(dir (when (file-remote-p default-directory)
169+
(with-parsed-tramp-file-name default-directory cur
170+
(when (string= cur-host host) default-directory)))))
167171
(tramp-maybe-open-connection vec)
168172
(with-current-buffer (tramp-get-connection-buffer vec)
169-
(cider-locate-running-nrepl-ports)))))
173+
(cider-locate-running-nrepl-ports dir)))))
170174
(ports (append (cdr sel-host) lein-ports))
171175
(port (cider--completing-read-port host ports)))
172-
(setq cider-host-history (cons sel-host (delete sel-host cider-host-history)))
173176
(list host port)))
174177

175178
(defun cider--ssh-hosts ()
@@ -182,33 +185,35 @@ Create REPL buffer and start an nREPL client connection."
182185
"Interactively select host from HOSTS.
183186
Each element in HOSTS is one of: (host), (host port) or (label host port).
184187
Return a list of the form (HOST PORT), where PORT can be nil."
185-
(let* ((sel-host (completing-read "Host: " (cider-join-with-val-prop hosts)))
186-
(host (or (get-text-property 1 :val sel-host) (list sel-host))))
188+
(let* ((hosts (cider-join-into-alist hosts))
189+
(sel-host (completing-read "Host: " hosts nil nil nil
190+
'cider-host-history (caar hosts)))
191+
(host (or (cdr (assoc sel-host hosts)) (list sel-host))))
187192
;; remove the label
188193
(if (= 3 (length host)) (cdr host) host)))
189194

190195
(defun cider--completing-read-port (host ports)
191196
"Interactively select port for HOST from PORTS."
192-
(let* ((sel-port (completing-read (format "Port for %s: " host)
193-
(cider-join-with-val-prop ports)))
194-
(port (or (get-text-property 1 :val sel-port) sel-port))
197+
(let* ((ports (cider-join-into-alist ports))
198+
(sel-port (completing-read (format "Port for %s: " host) ports
199+
nil nil nil nil (caar ports)))
200+
(port (or (cdr (assoc sel-port ports)) sel-port))
195201
(port (if (listp port) (second port) port)))
196202
(if (stringp port) (string-to-number port) port)))
197203

198-
(defun cider-locate-running-nrepl-ports ()
204+
(defun cider-locate-running-nrepl-ports (&optional dir)
199205
"Locate ports of running nREPL servers.
200-
Return a list of list of the form (project-dir port)."
201-
(let ((paths (cider--get-running-nrepl-paths)))
202-
(delq nil
203-
(mapcar (lambda (f)
204-
(-when-let (port-file (or (cider--file-path (concat f "/.nrepl-port"))
205-
(cider--file-path (concat f "/repl-port"))))
206-
(with-temp-buffer
207-
(insert-file-contents port-file)
208-
(list (file-name-nondirectory f) (buffer-string)))))
209-
paths))))
210-
211-
(defun cider--get-running-nrepl-paths ()
206+
When DIR is non-nil also look for nREPL port files in DIR. Return a list
207+
of list of the form (project-dir port)."
208+
(let* ((paths (cider--running-nrepl-paths))
209+
(proj-ports (mapcar (lambda (d)
210+
(-when-let (port (and d (nrepl-extract-port (cider--file-path d))))
211+
(list (file-name-nondirectory (directory-file-name d)) port)))
212+
(cons (nrepl-project-directory-for dir)
213+
paths))))
214+
(-distinct (delq nil proj-ports))))
215+
216+
(defun cider--running-nrepl-paths ()
212217
"Retrieve project paths of running nREPL servers.
213218
use `cider-ps-running-nrepls-command' and `cider-ps-running-nrepl-path-regexp-list'."
214219
(let (paths)

nrepl-client.el

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -251,10 +251,9 @@ Bind the value of the provided KEYS and execute BODY."
251251

252252
(defun nrepl-current-dir ()
253253
"Return the directory of the current buffer."
254-
(let ((file-name (buffer-file-name (current-buffer))))
255-
(or (when file-name
256-
(file-name-directory file-name))
257-
list-buffers-directory)))
254+
(or (-when-let (file-name (buffer-file-name (current-buffer)))
255+
(file-name-directory file-name))
256+
list-buffers-directory))
258257

259258
(defun nrepl-local-host-p (host)
260259
"Return t if HOST is local."
@@ -284,21 +283,19 @@ If so ask the user for confirmation."
284283
"An nREPL connection buffer already exists. Do you really want to create a new one? ")
285284
t))
286285

287-
(defun nrepl-default-port ()
288-
"Attempt to read port from .nrepl-port or target/repl-port.
289-
Falls back to `nrepl-port' if not found."
290-
(or (nrepl--port-from-file ".nrepl-port")
291-
(nrepl--port-from-file "target/repl-port")
292-
nrepl-port))
286+
(defun nrepl-extract-port (&optional dir)
287+
"Read port from .nrepl-port, nrepl-port or target/repl-port files in directory DIR."
288+
(-when-let (dir (or dir (nrepl-project-directory-for (nrepl-current-dir))))
289+
(or (nrepl--port-from-file (expand-file-name "repl-port" dir))
290+
(nrepl--port-from-file (expand-file-name ".nrepl-port" dir))
291+
(nrepl--port-from-file (expand-file-name "target/repl-port" dir)))))
293292

294293
(defun nrepl--port-from-file (file)
295294
"Attempts to read port from a file named by FILE."
296-
(let* ((dir (nrepl-project-directory-for (nrepl-current-dir)))
297-
(f (expand-file-name file dir)))
298-
(when (file-exists-p f)
299-
(with-temp-buffer
300-
(insert-file-contents f)
301-
(buffer-string)))))
295+
(when (file-exists-p file)
296+
(with-temp-buffer
297+
(insert-file-contents file)
298+
(buffer-string))))
302299

303300

304301
;;; nREPL dict

0 commit comments

Comments
 (0)