Skip to content

Commit 41e5c32

Browse files
committed
Use the new PREPL in 1.10
To make the results returned from the REPL parsable this commit switches to a dependency on Clojure 1.10, where the PREPL (Program REPL) was introduced. This commit also switches away from explicitly using a PrintStream in the repl-log namespace; instead a clojure.java.io/writer is used. This feels more Clojur-y. `tap>` will be used in conjunction with a custom tap listener to implement communication with the REPL which should be hidden from the user. That is a future commit.
1 parent dc266a9 commit 41e5c32

File tree

4 files changed

+43
-12
lines changed

4 files changed

+43
-12
lines changed

project.clj

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
:url "http://example.com/FIXME"
44
:license {:name "Eclipse Public License"
55
:url "http://www.eclipse.org/legal/epl-v10.html"}
6-
:dependencies [[org.clojure/clojure "1.9.0"]
6+
:dependencies [[org.clojure/clojure "1.10.0-alpha4"]
77
[org.clojure/tools.logging "0.3.1"]
88
[org.clojure/tools.namespace "0.3.0-alpha4"]
99
[org.clojure/tools.nrepl "0.2.13"]
@@ -15,6 +15,7 @@
1515
[neovim-client "0.1.2"]]
1616
:main ^:skip-aot socket-repl.system
1717
:target-path "target/%s"
18-
:profiles {:uberjar {:aot [clojure.tools.logging.impl
18+
:profiles {:dev {:jvm-opts ["-Dclojure.server.repl={:port 8888 :accept clojure.core.server/io-prepl}"]}
19+
:uberjar {:aot [clojure.tools.logging.impl
1920
socket-repl.socket-repl-plugin
2021
socket-repl.system]}})

src/socket_repl/repl_log.clj

+22-8
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
(ns socket-repl.repl-log
22
"Writes (presumably socket output) to the repl log."
33
(:require
4+
[clojure.edn :as edn]
45
[clojure.core.async :as async]
6+
[clojure.java.io :as io]
7+
[clojure.string :as string]
8+
[clojure.tools.logging :as log]
59
[socket-repl.nrepl :as nrepl]
610
[socket-repl.socket-repl :as socket-repl]
711
[socket-repl.util :refer [log-start log-stop]])
@@ -34,27 +38,37 @@
3438
(nrepl/subscribe-output nrepl input-channel)
3539

3640
;; Write input to file.
37-
(let [print-stream (PrintStream. file)]
41+
(let [writer (io/writer file)]
3842
(async/thread
3943
(loop []
4044
(when-let [input (async/<!! input-channel)]
41-
(.println print-stream (#'format-input input))
42-
(.flush print-stream)
45+
(binding [*out* writer]
46+
(println input))
4347
(recur))))
44-
(assoc repl-log :print-stream print-stream))))
48+
(assoc repl-log :writer writer))))
4549

4650
(defn stop
47-
[{:keys [print-stream input-channel] :as repl-log}]
51+
[{:keys [writer input-channel] :as repl-log}]
4852
(log-stop
4953
"repl-log"
50-
(.close print-stream)
54+
(.close writer)
5155
(async/close! input-channel)
52-
(dissoc repl-log :print-stream :input-channel)))
56+
(dissoc repl-log :writer :input-channel)))
57+
58+
(defn format-stuff
59+
[v]
60+
(log/info v)
61+
(case (:tag v)
62+
(:out) (string/trim-newline (:val v))
63+
(:ret) (str (:form v) " => " (:val v))
64+
v))
5365

5466
(defn new
5567
[socket-repl nrepl]
5668
{:socket-repl socket-repl
5769
:nrepl nrepl
5870
:file (File/createTempFile "socket-repl" ".txt")
5971
:print-stream nil
60-
:input-channel (async/chan 1024)})
72+
:input-channel (async/chan 1024 (comp
73+
(filter #(not= (:tag %) :tap))
74+
(map format-stuff)))})

src/socket_repl/socket_repl.clj

+18-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
(ns socket-repl.socket-repl
22
"Provides a channel interface to socket repl input and output."
33
(:require
4+
[clojure.edn :as edn]
45
[clojure.java.io :as io]
56
[clojure.core.async :as async]
7+
[clojure.tools.logging :as log]
68
[socket-repl.util :refer [log-start log-stop]])
79
(:import
810
(java.net Socket)
@@ -74,10 +76,25 @@
7476
(async/close! input-channel)
7577
socket-repl))
7678

79+
(defn deep-edn-read-string
80+
[edn]
81+
(if (string? edn)
82+
(let [data (edn/read-string edn)]
83+
(cond
84+
(map? data) (into {}
85+
(map (juxt (comp deep-edn-read-string first)
86+
(comp deep-edn-read-string second))
87+
data))
88+
(vector? data) (into [] (map deep-edn-read-string data))
89+
(list? data) (apply list (map deep-edn-read-string data))
90+
(set? data) (into #{} (map deep-edn-read-string data))
91+
:else data))
92+
edn))
93+
7794
(defn new
7895
[]
7996
{:input-channel (async/chan 1024)
80-
:output-channel (async/chan 1024)
97+
:output-channel (async/chan 1024 (map edn/read-string))
8198
:connection (atom {:host nil
8299
:port nil
83100
:socket nil

src/socket_repl/socket_repl_plugin.clj

-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@
7070
"plugin"
7171
(let [mult (async/mult code-channel)]
7272
(async/tap mult (socket-repl/input-channel socket-repl))
73-
(async/tap mult (repl-log/input-channel repl-log))
7473
(async/tap mult (nrepl/input-channel nrepl)))
7574

7675
;; Setup plugin functions.

0 commit comments

Comments
 (0)