Skip to content

Commit bf918a5

Browse files
committed
Merge pull request #496 from jonpither/known-endpoints
Cider command uses known endpoints
2 parents 41b4608 + b4692cb commit bf918a5

File tree

4 files changed

+63
-3
lines changed

4 files changed

+63
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
### New features
66

7+
* Cider command uses `cider-known-endpoints`.
78
* [#490](https://github.com/clojure-emacs/cider/pull/490) Dedicated
89
support for `company-mode` in `cider-complete-at-point`.
910
* [#460](https://github.com/clojure-emacs/cider/issues/460) Support for

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,15 @@ underlying project directories:
268268
(setq cider-switch-to-repl-command 'cider-switch-to-current-repl-buffer)
269269
```
270270

271+
* You can configure known endpoints used by the cider command. This is useful if you
272+
have a list of common host/ports you want to establish remote nREPL connections to.
273+
Using an optional label is helpful for identifying each host.
274+
If you want to bypass IDO selection of an endpoint, use <kbd>C-j</kbd>.
275+
276+
```el
277+
(setq cider-known-endpoints '(("host-a" "10.10.10.1" "7888") ("host-b" "7888")))
278+
```
279+
271280
### REPL History
272281

273282
* To make the REPL history wrap around when its end is reached:

cider.el

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,14 @@
7070
Normally it won't be used, unless `pkg-info' fails to extract the
7171
version from the CIDER package or library.")
7272

73+
(defcustom cider-known-endpoints nil
74+
"Specify a list of custom endpoints where each endpoint is a list.
75+
For example: '((\"label\" \"host\" \"port\")).
76+
The label is optional so that '(\"host\" \"port\") will suffice.
77+
This variable is used by the CIDER command."
78+
:type 'list
79+
:group 'cider)
80+
7381
;;;###autoload
7482
(defun cider-version ()
7583
"Display CIDER's version."
@@ -104,12 +112,35 @@ start the server."
104112
(setq nrepl-project-dir project-dir))
105113
(message "Starting nREPL server...")))))
106114

115+
(defun cider-known-endpoint-candidates ()
116+
"Known endpoint candidates for establishing an nREPL connection.
117+
A default will be included consisting of `nrepl-default-host' and
118+
`nrepl-default-port'."
119+
(-distinct
120+
(mapcar (lambda (endpoint)
121+
(mapconcat 'identity endpoint " "))
122+
(cons (list (nrepl-current-host) (nrepl-default-port))
123+
cider-known-endpoints))))
124+
125+
(defun cider-select-known-endpoint ()
126+
"Select an endpoint from known endpoints.
127+
The returned endpoint has the label removed."
128+
(let ((selected-endpoint (split-string
129+
(ido-completing-read
130+
"Host: " (cider-known-endpoint-candidates)))))
131+
(if (= 3 (length selected-endpoint))
132+
(cdr selected-endpoint)
133+
selected-endpoint)))
134+
107135
;;;###autoload
108136
(defun cider (host port)
109137
"Connect to an nREPL server identified by HOST and PORT."
110-
(interactive (list (read-string "Host: " (nrepl-current-host) nil (nrepl-current-host))
111-
(string-to-number (let ((port (nrepl-default-port)))
112-
(read-string "Port: " port nil port)))))
138+
(interactive (let ((known-endpoint (when cider-known-endpoints
139+
(cider-select-known-endpoint))))
140+
(list (or (car known-endpoint)
141+
(read-string "Host: " (nrepl-current-host) nil (nrepl-current-host)))
142+
(string-to-number (let ((port (or (cadr known-endpoint) (nrepl-default-port))))
143+
(read-string "Port: " port nil port))))))
113144
(setq cider-current-clojure-buffer (current-buffer))
114145
(when (nrepl-check-for-repl-buffer `(,host ,port) nil)
115146
(nrepl-connect host port)))

test/cider-tests.el

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,3 +412,22 @@
412412

413413
(dolist (buf (list b1 b2 b3 b1-repl b2-repl b3-repl))
414414
(kill-buffer buf)))))
415+
416+
(ert-deftest test-cider-known-endpoint-candidates ()
417+
(let ((cider-known-endpoints '(("label" "host" "port"))))
418+
(noflet ((nrepl-current-host () "current-host")
419+
(nrepl-default-port () "current-port"))
420+
(should (equal '("current-host current-port" "label host port")
421+
(cider-known-endpoint-candidates))))))
422+
423+
(ert-deftest test-cider-known-endpoint-candidates-remove-duplicates ()
424+
(let ((cider-known-endpoints '(("label" "host" "port") ("label" "host" "port"))))
425+
(noflet ((nrepl-current-host () "current-host")
426+
(nrepl-default-port () "current-port"))
427+
(should (equal '("current-host current-port" "label host port")
428+
(cider-known-endpoint-candidates))))))
429+
430+
(ert-deftest test-cider-select-known-endpoint-remove-label ()
431+
(noflet ((cider-known-endpoint-candidates () '())
432+
(ido-completing-read (dontcare dontcare) "label host port"))
433+
(should (equal '("host" "port") (cider-select-known-endpoint)))))

0 commit comments

Comments
 (0)