Skip to content

Commit d97dfa0

Browse files
authored
Detect host and port number from babashka nREPL conn msg (#3087)
1 parent 75755b4 commit d97dfa0

File tree

2 files changed

+46
-4
lines changed

2 files changed

+46
-4
lines changed

nrepl-client.el

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1043,6 +1043,20 @@ been determined."
10431043
(propertize cmd 'face 'font-lock-keyword-face))
10441044
serv-proc)))
10451045

1046+
(defconst nrepl-listening-address-regexp
1047+
(rx (or
1048+
;; standard
1049+
(and "nREPL server started on port " (group-n 1 (+ (any "0-9"))))
1050+
;; babashka
1051+
(and "Started nREPL server at "
1052+
(group-n 2 (+? any)) ":" (group-n 1 (+ (any "0-9"))))))
1053+
"A regexp to search an nREPL's stdout for the address it is listening on.
1054+
1055+
If it matches, the address components can be extracted using the following
1056+
match groups:
1057+
1 for the port, and
1058+
2 for the host (babashka only).")
1059+
10461060
(defun nrepl-server-filter (process output)
10471061
"Process nREPL server output from PROCESS contained in OUTPUT."
10481062
;; In Windows this can be false:
@@ -1062,10 +1076,12 @@ been determined."
10621076
(set-window-point win (point)))))
10631077
;; detect the port the server is listening on from its output
10641078
(when (and (null nrepl-endpoint)
1065-
(string-match "nREPL server started on port \\([0-9]+\\)" output))
1066-
(let ((port (string-to-number (match-string 1 output))))
1067-
(setq nrepl-endpoint (list :host (or (file-remote-p default-directory 'host)
1068-
"localhost")
1079+
(string-match nrepl-listening-address-regexp output))
1080+
(let ((host (or (match-string 2 output)
1081+
(file-remote-p default-directory 'host)
1082+
"localhost"))
1083+
(port (string-to-number (match-string 1 output))))
1084+
(setq nrepl-endpoint (list :host host
10691085
:port port))
10701086
(message "[nREPL] server started on %s" port)
10711087
(when nrepl-on-port-callback

test/nrepl-client-tests.el

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,32 @@
111111
(expect (nrepl-make-buffer-name "*buff-name %r:%S*" params)
112112
:to-equal "*buff-name cljs*")))))
113113

114+
(describe "nrepl-parse-port"
115+
(it "standard"
116+
(let ((msg "nREPL server started on port 58882 on host kubernetes.docker.internal - nrepl://kubernetes.docker.internal:58882"))
117+
(expect (string-match nrepl-listening-address-regexp msg)
118+
:not :to-be nil)
119+
(expect (match-string 1 msg)
120+
:to-equal "58882")
121+
(expect (match-string 2 msg)
122+
:to-be nil)))
123+
(it "babashka"
124+
(let ((msg "Started nREPL server at 127.0.0.1:1667"))
125+
(expect (string-match nrepl-listening-address-regexp msg)
126+
:not :to-be nil)
127+
(expect (match-string 1 msg)
128+
:to-equal "1667")
129+
(expect (match-string 2 msg)
130+
:to-equal "127.0.0.1")))
131+
(it "shadow"
132+
(let ((msg "shadow-cljs - nREPL server started on port 50999"))
133+
(expect (string-match nrepl-listening-address-regexp msg)
134+
:not :to-be nil)
135+
(expect (match-string 1 msg)
136+
:to-equal "50999")
137+
(expect (match-string 2 msg)
138+
:to-be nil))))
139+
114140
(describe "nrepl-client-lifecycle"
115141
(it "start and stop nrepl client process"
116142

0 commit comments

Comments
 (0)