Skip to content

Commit 294af3d

Browse files
committed
WIP - log socket repl to file
1 parent 9ea3a09 commit 294af3d

File tree

4 files changed

+40
-22
lines changed

4 files changed

+40
-22
lines changed

socket-repl-plugin/TODO

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,13 @@ TODO
33
[X] Make buffer-get-text-async work, just test it at the repl
44
[X] Get "eval-code" working
55
[_] Figure out how to accumulate / display results in vim
6-
append it all to a file
7-
preferably a temp file (could be a lot of data)
8-
don't keep it in memory
9-
`:ShowRepl`
6+
[X] append it all to a file
7+
[X] preferably a temp file (could be a lot of data)
8+
[_] `:ShowRepl`
109
no vimscript, invoke a plugin function
1110
show the contents of the temp file in the current buffer
1211
make it autoread
13-
[_] Name vim functions correctly
12+
[X] Name vim functions correctly
1413
[_] Pass host, port in connect
1514
[_] Shut down the plugin if no input received for one minute
1615

socket-repl-plugin/plugin/socketrepl.vim

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,20 @@ function! Connect()
1818
let res = rpcrequest(1, 'connect', [])
1919
return res
2020
endfunction
21+
command! Connect call Connect()
2122

2223
function! EvalBuffer()
2324
"call StartIfNotRunning()
2425
let res = rpcrequest(1, 'eval-buffer', [])
2526
return res
2627
endfunction
28+
command! EvalBuffer call EvalBuffer()
2729

2830
function! EvalCode()
2931
"call StartIfNotRunning()
3032
let res = rpcrequest(g:channel, 'eval-code', [])
3133
return res
3234
endfunction
35+
command! EvalCode call EvalCode()
3336

3437
echo 'socket repl plugin loaded!'

socket-repl-plugin/src/socket_repl/socket_repl_plugin.clj

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,14 @@
77
[neovim-client.nvim :as nvim])
88
(:import
99
(java.net Socket)
10-
(java.io PrintStream)))
10+
(java.io PrintStream File)))
1111

1212
(def current-connection (atom nil))
1313

14+
(defn output-file
15+
[]
16+
(File/createTempFile "socket-repl" ".txt"))
17+
1418
(defn connection
1519
"Create a connection to a socket repl."
1620
[host port]
@@ -28,6 +32,22 @@
2832
(.println out code-string)
2933
(.flush out))
3034

35+
(defn write-code!
36+
"Like `write-code`, but uses the current socket repl connection."
37+
[code-string]
38+
(write-code @current-connection code-string))
39+
40+
(defn write-output
41+
"Write a string to the output file."
42+
[{:keys [:file]} string]
43+
(.print file string)
44+
(.flush file))
45+
46+
(defn write-output!
47+
"Like `write-output`, but uses the current socket repl connection."
48+
[string]
49+
(write-output @current-connection string))
50+
3151
(defn connect!
3252
"Connect to a socket repl. Adds the connection to the `current-connection`
3353
atom. Creates `go-loop`s to delegate input from the socket to `handler` one
@@ -37,13 +57,15 @@
3757
[host port handler]
3858
(let [conn (connection host port)
3959
chan (async/chan 10)]
40-
(reset! current-connection (assoc conn
41-
:handler handler
42-
:chan chan))
60+
(reset! current-connection
61+
(assoc conn
62+
:handler handler
63+
:chan chan
64+
:file (PrintStream. (output-file))))
4365

4466
;; input producer
4567
(go-loop []
46-
(when-let [line (.readLine (:in conn))]
68+
(when-let [line (str (.readLine (:in conn)) "\n")]
4769
(>! chan line)
4870
(recur)))
4971

@@ -54,11 +76,6 @@
5476
(recur))))
5577
"success")
5678

57-
(defn write-code!
58-
"Like `write-code`, but uses the current socket repl connection."
59-
[code-string]
60-
(write-code @current-connection code-string))
61-
6279
(defn -main
6380
[& args]
6481
;; TODO: remove params for STDIO
@@ -71,8 +88,7 @@
7188
(connect! "localhost" "5555"
7289
(fn [x]
7390
(nvim/run-command-async!
74-
;; TODO: Actually, append to a buffer?
75-
(format ":echo '%s'" (pr-str x))
91+
(write-output! x)
7692
(fn [_] nil))))))
7793

7894
(nvim/register-method!
@@ -85,10 +101,8 @@
85101
"eval-buffer"
86102
(fn [msg]
87103
(nvim/get-current-buffer-text-async
88-
(fn [[x & _]]
89-
;; TODO: send code being executed
90-
;; back to vim, to show what actually
91-
;; happened in the repl buffer
104+
(fn [x]
105+
(write-output! (str x "\n"))
92106
(write-code! x)))))
93107

94108
;; TODO: Rather than an arbitrary timeout, the plugin should shut down

src/neovim_client/nvim.clj

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@
8282
(buffer-get-line-async buffer n #(buffer-set-line-async! buffer n
8383
(update-fn %)))))
8484

85+
;; TODO: Do this with `get-buffer-line-slice`
8586
(defn buffer-get-text
8687
"Get the buffer's text as a single string."
8788
[buffer]
@@ -93,7 +94,8 @@
9394
"Convenience function to get the current buffer's text asynchronously."
9495
[f]
9596
(get-current-buffer-async
96-
(fn [buffer] (buffer-get-lines-async buffer 0 -1 f))))
97+
(fn [buffer] (buffer-get-lines-async
98+
buffer 0 -1 (fn [lines] (f (str/join "\n" lines)))))))
9799

98100
;; TODO - make it wipe out text in the buffer after the last line of new text.
99101
(defn buffer-set-text!

0 commit comments

Comments
 (0)