Skip to content

Commit d439f1e

Browse files
committed
Add debug logging to component start/stop
1 parent 99a1501 commit d439f1e

File tree

8 files changed

+179
-142
lines changed

8 files changed

+179
-142
lines changed

TODO

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -41,34 +41,24 @@ TODO alpha
4141
Connection localhost:3333
4242
Then trying an ,eb doesn't fail gracefully
4343
->Exception in logs, current-connection still nil?
44-
[_] Script debugging
44+
[X] Script debugging
4545
[X] nvim listening to socket, sourced debug script
4646
[X] plugin process running a repl
4747
[X] automatically connect to nvim, with socketrepl main
4848
[X] another clojure process w/ socket repl on 5555
49-
[_] docs
49+
[X] docs
5050
You still have to :Connect from nvim
5151
[X] Fix `go` usage, no i/o
52-
[_] Global state
52+
[X] Global state
5353
[X] `user` should also have a `reset` to stop -> start
5454
Working towards getting rid of `current-connection`
5555
[X] Make a repl log component
5656
[X] Refactor names of things in the `socket-repl-cnn` atom
57-
[_] test out closing at repl
5857
[_] Can we use a multimethod rather than register-method?
5958
possibly wrap w/ async/thread
6059

61-
62-
6360
[_] Enhance debugging
64-
How to reset state?
65-
repl, rerun `connect!`, maybe just `go`
66-
nvim channel 1 will be fubar, how to replace?
67-
debug script when sourced increments channel
68-
69-
TODO next
70-
[_] Rather than explicit repl output logging, can we intercept
71-
channel
72-
stream implementation
73-
[X] Shut down the plugin if no input received for one minute
74-
[X] evaluate form under cursor
61+
[_] Comment new debug script function
62+
[_] Document / video process for debugging, including refresh, restart
63+
and reconnect
64+
[_] debug restart function should close repl log

plugin/socketrepl.vim.debug

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
let g:is_running = 0
22
let g:channel = -1
33

4+
"When the plugin is restarted at the repl, it reconnects, using the next
5+
"available channel.
6+
1function! PluginRestart()
7+
let g:is_running = 0
8+
endfunction
9+
410
function! StartIfNotRunning()
511
if g:is_running == 0
612
let g:is_running = 1
7-
let g:channel = 1
13+
let g:channel = g:channel + 1
814
endif
915
endfunction
1016

-1.72 KB
Binary file not shown.

src/socket_repl/repl_log.clj

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
"Writes (presumably socket output) to the repl log."
33
(:require
44
[clojure.core.async :as async]
5-
[socket-repl.socket-repl :as socket-repl])
5+
[socket-repl.socket-repl :as socket-repl]
6+
[socket-repl.util :refer [log-start log-stop]])
67
(:import
78
(java.io PrintStream File)))
89

@@ -19,24 +20,28 @@
1920
(defn start
2021
[{:keys [file input-channel socket-repl] :as repl-log}]
2122

22-
;; Subscribe to socket-repl output.
23-
(socket-repl/subscribe-output socket-repl input-channel)
23+
(log-start
24+
"repl-log"
25+
;; Subscribe to socket-repl output.
26+
(socket-repl/subscribe-output socket-repl input-channel)
2427

25-
;; Write input to file.
26-
(let [print-stream (PrintStream. file)]
27-
(async/thread
28-
(loop []
29-
(when-let [input (async/<!! input-channel)]
30-
(.println print-stream input)
31-
(.flush print-stream)
32-
(recur))))
33-
(assoc repl-log :print-stream print-stream)))
28+
;; Write input to file.
29+
(let [print-stream (PrintStream. file)]
30+
(async/thread
31+
(loop []
32+
(when-let [input (async/<!! input-channel)]
33+
(.println print-stream (str input "FOO"))
34+
(.flush print-stream)
35+
(recur))))
36+
(assoc repl-log :print-stream print-stream))))
3437

3538
(defn stop
3639
[{:keys [print-stream input-channel] :as repl-log}]
37-
(.close print-stream)
38-
(async/close! input-channel)
39-
(dissoc repl-log :print-stream :input-channel))
40+
(log-stop
41+
"repl-log"
42+
(.close print-stream)
43+
(async/close! input-channel)
44+
(dissoc repl-log :print-stream :input-channel)))
4045

4146
(defn new
4247
[socket-repl]

src/socket_repl/socket_repl.clj

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
"Provides a channel interface to socket repl input and output."
33
(:require
44
[clojure.java.io :as io]
5-
[clojure.core.async :as async])
5+
[clojure.core.async :as async]
6+
[socket-repl.util :refer [log-start log-stop]])
67
(:import
78
(java.net Socket)
89
(java.io PrintStream)))
@@ -26,6 +27,7 @@
2627
reader (io/reader socket)]
2728
(reset! connection {:host host
2829
:port port
30+
:socket socket
2931
:print-stream (-> socket io/output-stream PrintStream.)
3032
:reader reader})
3133
(future
@@ -48,28 +50,36 @@
4850

4951
(defn start
5052
[{:keys [input-channel] :as socket-repl}]
51-
(async/thread
52-
(loop []
53-
(when-let [input (async/<!! input-channel)]
54-
(when (connected? socket-repl)
55-
(write-code socket-repl input))
56-
(recur))))
57-
socket-repl)
53+
(log-start
54+
"socket-repl"
55+
(async/thread
56+
(loop []
57+
(when-let [input (async/<!! input-channel)]
58+
(when (connected? socket-repl)
59+
(write-code socket-repl input))
60+
(recur))))
61+
socket-repl))
5862

5963
(defn stop
6064
[{:keys [connection output-channel input-channel] :as socket-repl}]
61-
(let [{:keys [reader print-stream]} @connection]
62-
(.close reader)
63-
(.close print-stream))
64-
(async/close! output-channel)
65-
(async/close! input-channel)
66-
socket-repl)
65+
(log-stop
66+
"socket-repl"
67+
(let [{:keys [reader print-stream socket]} @connection]
68+
;(when reader (.close reader))
69+
;(when print-stream (.close print-stream))
70+
(when socket
71+
(.shutdownInput socket)
72+
(.shutdownOutput socket)))
73+
(async/close! output-channel)
74+
(async/close! input-channel)
75+
socket-repl))
6776

6877
(defn new
6978
[]
7079
{:input-channel (async/chan 1024)
7180
:output-channel (async/chan 1024)
7281
:connection (atom {:host nil
7382
:port nil
83+
:socket nil
7484
:reader nil
7585
:print-stream nil})})

src/socket_repl/socket_repl_plugin.clj

Lines changed: 100 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
[neovim-client.message :as message]
1010
[neovim-client.nvim :as nvim]
1111
[socket-repl.repl-log :as repl-log]
12-
[socket-repl.socket-repl :as socket-repl]))
12+
[socket-repl.socket-repl :as socket-repl]
13+
[socket-repl.util :refer [log-start log-stop]]))
1314

1415
(defn position
1516
"Find the position in a code string given line and column."
@@ -79,106 +80,110 @@
7980
[{:keys [debug nvim repl-log socket-repl code-channel] :as plugin}]
8081

8182
;; Wire sub-component io.
82-
(let [mult (async/mult code-channel)]
83-
(async/tap mult (socket-repl/input-channel socket-repl))
84-
(async/tap mult (repl-log/input-channel repl-log)))
85-
86-
;; Setup plugin functions.
87-
(nvim/register-method!
88-
nvim
89-
"connect"
90-
(fn [msg]
91-
(let [[host port] (-> msg
92-
message/params
93-
first
94-
(string/split #":"))]
95-
(try
96-
(socket-repl/connect socket-repl host port)
97-
(catch Throwable t
98-
(log/error t "Error connecting to socket repl")
99-
(async/thread (nvim/vim-command
100-
nvim
101-
":echo 'Unable to connect to socket repl.'"))))
102-
:done)))
103-
104-
(nvim/register-method!
105-
nvim
106-
"eval-code"
107-
(run-command
108-
plugin
83+
(log-start
84+
"plugin"
85+
(let [mult (async/mult code-channel)]
86+
(async/tap mult (socket-repl/input-channel socket-repl))
87+
(async/tap mult (repl-log/input-channel repl-log)))
88+
89+
;; Setup plugin functions.
90+
(nvim/register-method!
91+
nvim
92+
"connect"
10993
(fn [msg]
110-
(let [coords (nvim/get-cursor-location nvim)
111-
buffer-text (nvim/get-current-buffer-text nvim)]
94+
(let [[host port] (-> msg
95+
message/params
96+
first
97+
(string/split #":"))]
11298
(try
113-
(async/>!! code-channel (get-form-at buffer-text coords))
99+
(socket-repl/connect socket-repl host port)
114100
(catch Throwable t
115-
(log/error t "Error evaluating a form")
116-
(write-error repl-log t)))))))
117-
118-
(nvim/register-method!
119-
nvim
120-
"eval-buffer"
121-
(run-command
122-
plugin
123-
(fn [msg]
124-
(let [buffer (nvim/vim-get-current-buffer nvim)
125-
filename (nvim/buffer-get-name nvim buffer)]
126-
(if (.exists (io/as-file filename))
127-
(do
128-
;; Not sure if saving the file is really always what we want,
129-
;; but if we don't, stale data will be loaded.
130-
(nvim/vim-command nvim ":w")
131-
(async/>!! code-channel (format "(load-file \"%s\")" filename)))
132-
(let [code (string/join "\n" (nvim/buffer-get-line-slice
133-
nvim buffer 0 -1))]
134-
(async/>!! code-channel (format "(eval '(do %s))" code))))))))
135-
136-
(nvim/register-method!
137-
nvim
138-
"doc"
139-
(run-command
140-
plugin
141-
(fn [msg]
142-
(nvim/get-current-word-async
143-
nvim
144-
(fn [word]
145-
(let [code (format "(clojure.repl/doc %s)" word)]
146-
(async/>!! code-channel code)))))))
147-
148-
(nvim/register-method!
149-
nvim
150-
"show-log"
151-
(run-command
152-
plugin
153-
(fn [msg]
154-
(let [file (-> repl-log repl-log/file .getAbsolutePath)]
155-
(let [original-window (nvim/vim-get-current-window nvim)
156-
buffer-cmd (first (message/params msg))
157-
rlog-buffer (get-rlog-buffer-name nvim)
158-
rlog-buffer-visible? (when rlog-buffer
159-
(async/<!! (nvim/buffer-visible?-async
160-
nvim rlog-buffer)))]
161-
(when-not rlog-buffer-visible?
162-
(nvim/vim-command
163-
nvim
164-
(format "%s | nnoremap <buffer> q :q<cr> | :let b:rlog=1 | :call termopen('tail -f %s') | :set ft=clojurerepl"
165-
buffer-cmd file))
166-
(nvim/vim-set-current-window nvim original-window)))))))
167-
168-
(nvim/register-method!
169-
nvim
170-
"dismiss-log"
171-
(run-command
172-
plugin
173-
(fn [msg]
174-
(nvim/vim-command
175-
nvim (format "bd! %s" (get-rlog-buffer-number nvim))))))
176-
plugin)
101+
(log/error t "Error connecting to socket repl")
102+
(async/thread (nvim/vim-command
103+
nvim
104+
":echo 'Unable to connect to socket repl.'"))))
105+
:done)))
106+
107+
(nvim/register-method!
108+
nvim
109+
"eval-code"
110+
(run-command
111+
plugin
112+
(fn [msg]
113+
(let [coords (nvim/get-cursor-location nvim)
114+
buffer-text (nvim/get-current-buffer-text nvim)]
115+
(try
116+
(async/>!! code-channel (get-form-at buffer-text coords))
117+
(catch Throwable t
118+
(log/error t "Error evaluating a form")
119+
(write-error repl-log t)))))))
120+
121+
(nvim/register-method!
122+
nvim
123+
"eval-buffer"
124+
(run-command
125+
plugin
126+
(fn [msg]
127+
(let [buffer (nvim/vim-get-current-buffer nvim)
128+
filename (nvim/buffer-get-name nvim buffer)]
129+
(if (.exists (io/as-file filename))
130+
(do
131+
;; Not sure if saving the file is really always what we want,
132+
;; but if we don't, stale data will be loaded.
133+
(nvim/vim-command nvim ":w")
134+
(async/>!! code-channel (format "(load-file \"%s\")" filename)))
135+
(let [code (string/join "\n" (nvim/buffer-get-line-slice
136+
nvim buffer 0 -1))]
137+
(async/>!! code-channel (format "(eval '(do %s))" code))))))))
138+
139+
(nvim/register-method!
140+
nvim
141+
"doc"
142+
(run-command
143+
plugin
144+
(fn [msg]
145+
(nvim/get-current-word-async
146+
nvim
147+
(fn [word]
148+
(let [code (format "(clojure.repl/doc %s)" word)]
149+
(async/>!! code-channel code)))))))
150+
151+
(nvim/register-method!
152+
nvim
153+
"show-log"
154+
(run-command
155+
plugin
156+
(fn [msg]
157+
(let [file (-> repl-log repl-log/file .getAbsolutePath)]
158+
(let [original-window (nvim/vim-get-current-window nvim)
159+
buffer-cmd (first (message/params msg))
160+
rlog-buffer (get-rlog-buffer-name nvim)
161+
rlog-buffer-visible? (when rlog-buffer
162+
(async/<!! (nvim/buffer-visible?-async
163+
nvim rlog-buffer)))]
164+
(when-not rlog-buffer-visible?
165+
(nvim/vim-command
166+
nvim
167+
(format "%s | nnoremap <buffer> q :q<cr> | :let b:rlog=1 | :call termopen('tail -f %s') | :set ft=clojurerepl"
168+
buffer-cmd file))
169+
(nvim/vim-set-current-window nvim original-window)))))))
170+
171+
(nvim/register-method!
172+
nvim
173+
"dismiss-log"
174+
(run-command
175+
plugin
176+
(fn [msg]
177+
(nvim/vim-command
178+
nvim (format "bd! %s" (get-rlog-buffer-number nvim))))))
179+
plugin))
177180

178181
(defn stop
179182
[plugin]
180-
(async/close! (:code-channel plugin))
181-
plugin)
183+
(log-stop
184+
"plugin"
185+
(async/close! (:code-channel plugin))
186+
plugin))
182187

183188
(defn new
184189
[debug nvim repl-log socket-repl]

0 commit comments

Comments
 (0)