|
9 | 9 | [neovim-client.message :as message] |
10 | 10 | [neovim-client.nvim :as nvim] |
11 | 11 | [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]])) |
13 | 14 |
|
14 | 15 | (defn position |
15 | 16 | "Find the position in a code string given line and column." |
|
79 | 80 | [{:keys [debug nvim repl-log socket-repl code-channel] :as plugin}] |
80 | 81 |
|
81 | 82 | ;; 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" |
109 | 93 | (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 #":"))] |
112 | 98 | (try |
113 | | - (async/>!! code-channel (get-form-at buffer-text coords)) |
| 99 | + (socket-repl/connect socket-repl host port) |
114 | 100 | (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)) |
177 | 180 |
|
178 | 181 | (defn stop |
179 | 182 | [plugin] |
180 | | - (async/close! (:code-channel plugin)) |
181 | | - plugin) |
| 183 | + (log-stop |
| 184 | + "plugin" |
| 185 | + (async/close! (:code-channel plugin)) |
| 186 | + plugin)) |
182 | 187 |
|
183 | 188 | (defn new |
184 | 189 | [debug nvim repl-log socket-repl] |
|
0 commit comments