Skip to content

Commit d367540

Browse files
committed
Add internal communication channel
Previous commit mentions the `tap>` function for doing private communication between the plugin and REPL; I decided against that, and instead added another instance of the `socket-repl` "component" to which the repl-log component does not subscribe.
1 parent 41e5c32 commit d367540

File tree

3 files changed

+54
-16
lines changed

3 files changed

+54
-16
lines changed

src/socket_repl/repl_log.clj

-1
Original file line numberDiff line numberDiff line change
@@ -70,5 +70,4 @@
7070
:file (File/createTempFile "socket-repl" ".txt")
7171
:print-stream nil
7272
:input-channel (async/chan 1024 (comp
73-
(filter #(not= (:tag %) :tap))
7473
(map format-stuff)))})

src/socket_repl/socket_repl_plugin.clj

+50-13
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
(:code-channel plugin))
6464

6565
(defn start
66-
[{:keys [nvim nrepl repl-log socket-repl code-channel] :as plugin}]
66+
[{:keys [nvim nrepl repl-log socket-repl internal-socket-repl code-channel] :as plugin}]
6767

6868
;; Wire sub-component io.
6969
(log-start
@@ -83,6 +83,19 @@
8383
(string/split #":"))]
8484
(try
8585
(socket-repl/connect socket-repl host port)
86+
(socket-repl/connect internal-socket-repl host port)
87+
(async/>!! (socket-repl/input-channel internal-socket-repl)
88+
'(do
89+
(ns srepl.injection
90+
(:refer-clojure :rename {eval core-eval}))
91+
(defn eval
92+
([form] (eval form nil))
93+
([form options]
94+
(let [result (core-eval form)]
95+
(merge
96+
#:socket-repl{:result (pr-str result)
97+
:form (pr-str form)}
98+
options))))))
8699
(catch Throwable t
87100
(log/error t "Error connecting to socket repl")
88101
(async/thread (api/command
@@ -115,11 +128,12 @@
115128
plugin
116129
(fn [msg]
117130
(try
118-
(async/>!! code-channel (parser/read-next
119-
(-> msg
120-
message/params
121-
ffirst)
122-
0 0))
131+
(async/>!! (socket-repl/input-channel socket-repl)
132+
(parser/read-next
133+
(-> msg
134+
message/params
135+
ffirst)
136+
0 0))
123137
(catch Throwable t
124138
(log/error t "Error evaluating form")
125139
(write-error repl-log t))))))
@@ -133,7 +147,8 @@
133147
(let [[row col] (api-ext/get-cursor-location nvim)
134148
buffer-text (api-ext/get-current-buffer-text nvim)]
135149
(try
136-
(async/>!! code-channel (parser/read-next buffer-text row (inc col)))
150+
(async/>!! (socket-repl/input-channel socket-repl)
151+
(parser/read-next buffer-text row (inc col)))
137152
(catch Throwable t
138153
(log/error t "Error evaluating a form")
139154
(write-error repl-log t)))))))
@@ -147,7 +162,8 @@
147162
(let [buffer (api/get-current-buf nvim)]
148163
(let [code (string/join "\n" (api.buffer-ext/get-lines
149164
nvim buffer 0 -1))]
150-
(async/>!! code-channel (format "(eval '(do %s))" code)))))))
165+
(async/>!! (socket-repl/input-channel socket-repl)
166+
(format "(eval '(do %s))" code)))))))
151167

152168
(nvim/register-method!
153169
nvim
@@ -158,7 +174,7 @@
158174
(let [code (format "(clojure.repl/doc %s)" (-> msg
159175
message/params
160176
ffirst))]
161-
(async/>!! code-channel code)))))
177+
(async/>!! (socket-repl/input-channel socket-repl) code)))))
162178

163179
(nvim/register-method!
164180
nvim
@@ -170,7 +186,7 @@
170186
nvim
171187
(fn [word]
172188
(let [code (format "(clojure.repl/doc %s)" word)]
173-
(async/>!! code-channel code)))))))
189+
(async/>!! (socket-repl/input-channel socket-repl) code)))))))
174190

175191
(nvim/register-method!
176192
nvim
@@ -181,7 +197,7 @@
181197
(let [code (format "(clojure.repl/source %s)" (-> msg
182198
message/params
183199
ffirst))]
184-
(async/>!! code-channel code)))))
200+
(async/>!! (socket-repl/input-channel socket-repl) code)))))
185201

186202
(nvim/register-method!
187203
nvim
@@ -193,7 +209,27 @@
193209
nvim
194210
(fn [word]
195211
(let [code (format "(clojure.repl/source %s)" word)]
196-
(async/>!! code-channel code)))))))
212+
(async/>!! (socket-repl/input-channel socket-repl) code)))))))
213+
214+
(nvim/register-method!
215+
nvim
216+
"cp"
217+
(run-command
218+
plugin
219+
(fn [msg]
220+
(let [code-form "(map #(.getAbsolutePath %) (clojure.java.classpath/classpath))"]
221+
(log/info msg)
222+
(async/>!! (socket-repl/input-channel internal-socket-repl)
223+
code-form)
224+
(log/info "I'm in!")
225+
(async/thread
226+
(let [res-chan (async/chan 1 (filter #(= (:form %) code-form)))]
227+
(socket-repl/subscribe-output internal-socket-repl res-chan)
228+
(let [res (async/<!! res-chan)]
229+
(log/info (:ns res))
230+
(log/info (:ms res))
231+
(log/info (:val res))
232+
(.close res-chan))))))))
197233

198234
(nvim/register-method!
199235
nvim
@@ -244,9 +280,10 @@
244280
plugin))
245281

246282
(defn new
247-
[nvim nrepl repl-log socket-repl]
283+
[nvim nrepl repl-log socket-repl internal-socket-repl]
248284
{:nvim nvim
249285
:nrepl nrepl
250286
:repl-log repl-log
251287
:socket-repl socket-repl
288+
:internal-socket-repl internal-socket-repl
252289
:code-channel (async/chan 1024)})

src/socket_repl/system.clj

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
(ns socket-repl.system
2-
"The system created by wiring various components together."
2+
"The system created by wiring various components together."
33
(:require
44
[clojure.core.async :as async]
55
[clojure.tools.logging :as log]
@@ -13,12 +13,14 @@
1313
(defn new-system*
1414
[nvim]
1515
(let [socket-repl (socket-repl/start (socket-repl/new))
16+
internal-socket-repl (socket-repl/start (socket-repl/new))
1617
nrepl (nrepl/start (nrepl/new))
1718
repl-log (repl-log/start (repl-log/new socket-repl nrepl))
18-
plugin (plugin/start (plugin/new nvim nrepl repl-log socket-repl))]
19+
plugin (plugin/start (plugin/new nvim nrepl repl-log socket-repl internal-socket-repl))]
1920
{:nvim nvim
2021
:repl-log repl-log
2122
:socket-repl socket-repl
23+
:internal-socket-repl internal-socket-repl
2224
:nrepl nrepl
2325
:plugin plugin}))
2426

0 commit comments

Comments
 (0)