Skip to content

Commit bde9da3

Browse files
committed
Handle reset of component state from the repl.
1 parent 89e5b32 commit bde9da3

File tree

2 files changed

+37
-21
lines changed

2 files changed

+37
-21
lines changed

src/neovim_client/nvim.clj

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
11
(ns neovim-client.nvim
22
(:require [clojure.core.async :as async]
33
[clojure.string :as str]
4+
[clojure.tools.logging :as log]
45
[neovim-client.message :as message :refer [->request-msg]]
5-
[neovim-client.rpc :as rpc]))
6-
7-
;; ***** Public *****
8-
9-
(def new rpc/new)
10-
(def register-method! rpc/register-method!)
11-
(def stop rpc/stop)
6+
[neovim-client.rpc :as rpc])
7+
(:import (java.net Socket)))
128

139
(defmacro defvim
1410
[fn-name vim-command & args]
@@ -116,3 +112,26 @@
116112
#(window-get-buffer nvim %))
117113
(vim-get-windows nvim))]
118114
((set visible-buffers) buffer-name))))
115+
116+
(def register-method! rpc/register-method!)
117+
(def stop rpc/stop)
118+
119+
(defn new*
120+
[input output debug]
121+
(let [component (rpc/new input output)]
122+
;; Each time you connect to the same nvim instance using a tcp socket, nvim
123+
;; allocates a new channel.
124+
(when debug
125+
(let [api-info (vim-get-api-info component)]
126+
(vim-command component (format "let g:nvim_tcp_plugin_channel = %s"
127+
(first api-info)))))
128+
component))
129+
130+
(defn new
131+
"Connect to msgpack-rpc channel via standard io or TCP socket."
132+
([] (new* System/in System/out) false)
133+
([host port]
134+
(log/info "plugin host connecting to nvim at " host ":" port)
135+
(let [socket (java.net.Socket. host port)]
136+
(.setTcpNoDelay socket true)
137+
(new* (.getInputStream socket) (.getOutputStream socket) true))))

src/neovim_client/rpc.clj

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
[neovim-client.message :refer [id value msg-type method params
77
->response-msg]
88
:as msg])
9-
(:import [java.io DataInputStream DataOutputStream]
10-
[java.net Socket]))
9+
(:import (java.io DataInputStream DataOutputStream)))
1110

1211
(defn- method-not-found
1312
[msg]
@@ -20,7 +19,7 @@
2019
(let [chan (async/chan 1024)]
2120
(async/go-loop
2221
[]
23-
(when-let [msg (msgpack/unpack (DataInputStream. input-stream))]
22+
(when-let [msg (msgpack/unpack input-stream)]
2423
(log/info "stream -> msg -> in chan: " msg)
2524
(async/>! chan msg)
2625
(recur)))
@@ -40,7 +39,7 @@
4039
[]
4140
(when-let [msg (async/<! chan)]
4241
(log/info "stream <- msg <- out chan: " msg)
43-
(write-msg! (msgpack/pack msg) (DataOutputStream. output-stream))
42+
(write-msg! (msgpack/pack msg) output-stream)
4443
(recur)))
4544
chan))
4645

@@ -68,12 +67,18 @@
6867
[{:keys [input-stream output-stream out-chan in-chan]}]
6968
(async/close! out-chan)
7069
(async/close! in-chan)
70+
;; TODO - drain the out-chan before closing the output-stream.
71+
(log/info "closing output stream")
72+
(.close output-stream)
73+
(log/info "closing input stream")
7174
(.close input-stream)
72-
(.close output-stream))
75+
(log/info "input and output streams closed"))
7376

74-
(defn new*
77+
(defn new
7578
[input-stream output-stream]
7679
(let [in-chan (create-input-channel input-stream)
80+
input-stream (DataInputStream. input-stream)
81+
output-stream (DataOutputStream. output-stream)
7782
message-table (atom {})
7883
method-table (atom {})
7984
component {:input-stream input-stream
@@ -99,11 +104,3 @@
99104
component (->response-msg (id msg) result) nil)))
100105
(recur))))
101106
component))
102-
103-
(defn new
104-
"Connect to msgpack-rpc channel via standard io or TCP socket."
105-
([] (new* System/in System/out))
106-
([host port]
107-
(let [socket (java.net.Socket. host port)]
108-
(.setTcpNoDelay socket true)
109-
(new* (.getInputStream socket) (.getOutputStream socket)))))

0 commit comments

Comments
 (0)