Skip to content

Commit 882dd0c

Browse files
committed
Added eval form under cursor
1 parent 584ba2c commit 882dd0c

File tree

4 files changed

+71
-8
lines changed

4 files changed

+71
-8
lines changed

socket-repl-plugin/TODO

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,21 @@ TODO
22
[X] Move all of the socket repl stuff to a plugin project
33
[X] Make buffer-get-text-async work, just test it at the repl
44
[X] Get "eval-code" working
5-
[_] Figure out how to accumulate / display results in vim
5+
[X] Figure out how to accumulate / display results in vim
66
[X] append it all to a file
77
[X] preferably a temp file (could be a lot of data)
8-
[_] `:ShowRepl`
8+
[X] `:ShowRepl`
99
no vimscript, invoke a plugin function
1010
show the contents of the temp file in the current buffer
1111
make it autoread
1212
[X] Name vim functions correctly
13+
[_] Rather than explicit repl output logging, can we intercept
14+
channel
15+
stream implementation
16+
[_] tmux dev env script
1317
[_] Pass host, port in connect
1418
[_] Shut down the plugin if no input received for one minute
19+
[X] evaluate form under cursor
1520

1621
Goals
1722
-Not a proper nRepl plugin, primary use is as an thought exercise
@@ -32,6 +37,8 @@ Goals
3237
Forces you to explicity connect
3338
One connection only `:call ConnectClojurePlugin(host, port)`
3439
Accessible from vim buffer `:call ShowClojurePluginRepl()`
40+
-Not a way of life
41+
Does not include paredit, syntax highlight, etc
3542

3643
Questions
3744
Figwheel cljs-repl support

socket-repl-plugin/plugin/socketrepl.vim

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ command! EvalBuffer call EvalBuffer()
2929

3030
function! EvalCode()
3131
"call StartIfNotRunning()
32-
let res = rpcrequest(g:channel, 'eval-code', [])
32+
"let res = rpcrequest(g:channel, 'eval-code', [])
33+
let res = rpcrequest(1, 'eval-code', [])
3334
return res
3435
endfunction
3536
command! EvalCode call EvalCode()

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

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,35 @@
44
(:require
55
[clojure.java.io :as io]
66
[clojure.core.async :as async :refer [go go-loop >! <!]]
7+
[clojure.string :as string]
78
[neovim-client.nvim :as nvim])
89
(:import
910
(java.net Socket)
1011
(java.io PrintStream File)))
1112

1213
(def current-connection (atom nil))
1314

15+
(defn position
16+
"Find the position in a code string given line and column."
17+
[code-str [y x]]
18+
(->> code-str
19+
string/split-lines
20+
(take (dec y))
21+
(string/join "\n")
22+
count
23+
(+ (inc x))))
24+
25+
(defn get-form-at
26+
"Returns the enclosing form from a string a code using [row col]
27+
coordinates."
28+
[code-str coords]
29+
(let [pos (position code-str coords)]
30+
(read-string
31+
;; Start at the last index of paren on or before `pos`, read a form.
32+
(subs code-str (if (= \( (.charAt code-str pos))
33+
pos
34+
(.lastIndexOf (subs code-str 0 pos) "("))))))
35+
1436
(defn output-file
1537
[]
1638
(File/createTempFile "socket-repl" ".txt"))
@@ -96,8 +118,21 @@
96118
(nvim/register-method!
97119
"eval-code"
98120
(fn [msg]
99-
;; TODO: Get the cursor location, find current form
100-
))
121+
(nvim/get-cursor-location-async
122+
(fn [coords]
123+
(nvim/get-current-buffer-text-async
124+
(fn [x]
125+
(try
126+
(let [form (get-form-at x coords)]
127+
(write-output! (str form "\n"))
128+
(write-code! form))
129+
(catch Throwable t
130+
;; TODO: Use more general plugin-level ereror handling
131+
(write-output!
132+
(str "\n##### PLUGIN ERR #####\n"
133+
(.getMessage t) "\n"
134+
(string/join "\n" (map str (.getStackTrace t)))
135+
\n"######################\n"))))))))))
101136

102137
(nvim/register-method!
103138
"eval-buffer"
@@ -138,4 +173,3 @@
138173
;; Let nvim know we're shutting down.
139174
(nvim/run-command! ":let g:is_running=0")
140175
(nvim/run-command! ":echo 'plugin stopping.'")))
141-

src/neovim_client/nvim.clj

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,34 @@
2222

2323
(defn get-api-info [] (send-message! "vim_get_api_info"))
2424

25+
;; ***** Cursor *****
26+
27+
(declare get-current-window-async)
28+
(defn get-cursor-location-async
29+
"Gets the cursor's current position as a tuple (row, col) asynchronously."
30+
[f]
31+
(get-current-window-async
32+
(fn [window]
33+
(rpc/send-message-async!
34+
(->request-msg "window_get_cursor" [window]) f))))
35+
36+
;; ***** Lines *****
37+
2538
(defn get-current-line [] (send-message! "vim_get_current_line"))
2639

2740
(defn set-current-line! [line] (send-message! "vim_set_current_line" line))
2841

42+
;; ***** Windows *****
43+
44+
(defn get-current-window-async
45+
"Gets a reference to the current window asynchronously."
46+
[f]
47+
(rpc/send-message-async! (->request-msg "vim_get_current_window" []) f))
48+
49+
;; ***** Buffers *****
50+
2951
(defn get-current-buffer
30-
;; TODO: fix comment
31-
"Returns ??? (object, map, ?) representing the current buffer."
52+
"Returns a reference to the current buffer."
3253
[]
3354
(send-message! "vim_get_current_buffer"))
3455

0 commit comments

Comments
 (0)