@@ -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.
183186Each element in HOSTS is one of: (host), (host port) or (label host port).
184187Return 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.
213218use `cider-ps-running-nrepls-command' and `cider-ps-running-nrepl-path-regexp-list' ."
214219 (let (paths)
0 commit comments