Skip to content

Commit cdd1e1b

Browse files
authored
Deduplicate inplace (#69)
Also, makes virtualtext always show at the end of the form
1 parent 6527597 commit cdd1e1b

File tree

4 files changed

+29
-53
lines changed

4 files changed

+29
-53
lines changed

API.md

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -91,19 +91,12 @@ code **(string)**: Clojure s-expression to be evaluated on the nrepl
9191
Defaults to current file's ns.
9292

9393

94-
## `acid.features.eval_inplace([mode[, ns]])`
94+
## `acid.features.eval_expr([mode[, replace[, ns]]])`
9595
Evaluate the current form or the given motion.
9696

9797
*mode* **(string)**: motion mode
9898

99-
*ns* **(string)**: Namespace to be used when evaluating the code.
100-
Defaults to current file's ns.
101-
102-
103-
## `acid.features.eval_expr([mode[, ns]])`
104-
Evaluate the current form or the given motion.
105-
106-
*mode* **(string)**: motion mode
99+
*replace* **(boolean)**: whether it should replace the form with its result
107100

108101
*ns* **(string)**: Namespace to be used when evaluating the code.
109102
Defaults to current file's ns.

lua/acid/features.lua

Lines changed: 20 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,15 @@ features.eval_print = function(code, ns)
6060
end
6161

6262
--- Evaluate the current form or the given motion.
63-
-- The result will replace the current form
63+
-- The result will be shown on a virtualtext next to the current form
64+
-- and also stored on the clipboard.
6465
-- @tparam[opt] string mode motion mode
66+
-- @tparam[opt] boolean replace whether it should replace the form with its result
6567
-- @tparam[opt] string ns Namespace to be used when evaluating the code.
6668
-- Defaults to current file's ns.
67-
features.eval_inplace = function(mode, ns)
69+
features.eval_expr = function(mode, replace, ns)
6870
local payload = {}
71+
local midlws
6972
local coord, form
7073
if mode == nil then
7174
form, coord = forms.form_under_cursor()
@@ -83,47 +86,27 @@ features.eval_inplace = function(mode, ns)
8386
if ns ~= nil or ns ~= "" then
8487
payload.ns = ns
8588
end
86-
acid.run(ops.eval(payload):with_handler(middlewares
87-
.refactor(utils.merge(coord, {accessor = function(dt)
88-
if dt.value ~= nil then
89-
return dt.value
90-
else
91-
return dt.out
92-
end
93-
end}))))
94-
end
9589

96-
--- Evaluate the current form or the given motion.
97-
-- The result will be shown on a virtualtext next to the current form
98-
-- and also stored on the clipboard.
99-
-- @tparam[opt] string mode motion mode
100-
-- @tparam[opt] string ns Namespace to be used when evaluating the code.
101-
-- Defaults to current file's ns.
102-
features.eval_expr = function(mode, ns)
103-
local payload = {}
104-
if mode == nil then
105-
local form = forms.form_under_cursor()
106-
payload.code = table.concat(form, "\n")
107-
elseif mode == "symbol" then
108-
payload.code = forms.symbol_under_cursor()
109-
elseif mode == "top" then
110-
local form = forms.form_under_cursor(true)
111-
payload.code = table.concat(form, "\n")
90+
if replace then
91+
midlws = middlewares
92+
.refactor(utils.merge(coord, {accessor = function(dt)
93+
if dt.value ~= nil then
94+
return dt.value
95+
else
96+
return dt.out
97+
end
98+
end}))
11299
else
113-
local lines = forms.form_from_motion(mode)
114-
payload.code = table.concat(lines, "\n")
115-
end
116-
ns = ns or vim.api.nvim_call_function("AcidGetNs", {})
117-
if ns ~= nil or ns ~= "" then
118-
payload.ns = ns
119-
end
120-
acid.run(ops.eval(payload):with_handler(middlewares
100+
midlws = middlewares
121101
.print{}
122102
.clipboard{}
123-
.virtualtext{}
124-
))
103+
.virtualtext(coord)
104+
end
105+
106+
acid.run(ops.eval(payload):with_handler(midlws))
125107
end
126108

109+
127110
--- Sends a `(require '[...])` function to the nrepl.
128111
-- Will send an `AcidRequired` autocommand after complete.
129112
-- @tparam[opt] string ns Namespace to be used when evaluating the code.

lua/acid/middlewares/virtualtext.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@ virtualtext.toggle = function()
2929

3030
end
3131

32-
virtualtext.middleware = function(_)
32+
virtualtext.middleware = function(config)
3333
return function(middleware)
3434
return function(data)
3535
local cb = vim.api.nvim_get_current_buf()
36-
local ln = vim.api.nvim_call_function("line", {"."}) - 1
36+
local ln = config.to[1] - 1
3737
local key = tostring(cb) .. "/" .. tostring(ln)
3838

3939
if data.status then
@@ -58,13 +58,13 @@ virtualtext.middleware = function(_)
5858

5959
if #vt > 0 then
6060
table.insert(vt, 1, {";; => ", "Comment"})
61-
6261
table.insert(virtualtext.cache[key], vt)
6362

6463
virtualtext.cache_index[key] = #virtualtext.cache[key]
6564

6665
-- TODO split_lines
6766
vim.api.nvim_buf_set_virtual_text(cb, virtualtext.ns, ln, vt, {})
67+
6868
end
6969
return middleware(data)
7070
end

plugin/acid.vim

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,11 @@ function! AcidCleanNs()
6060
endfunction
6161

6262
function! AcidMotion(mode)
63-
exec 'lua require("acid.features").eval_expr("'.a:mode.'")'
63+
exec 'lua require("acid.features").eval_expr("'.a:mode.'", false)'
6464
endfunction
6565

6666
function! AcidEvalInplace(mode)
67-
exec 'lua require("acid.features").eval_inplace("'.a:mode.'")'
67+
exec 'lua require("acid.features").eval_expr("'.a:mode.'", true)'
6868
endfunction
6969

7070
function! AcidSendEval(handler)
@@ -105,8 +105,8 @@ map <Plug>(acid-eval-print) <Cmd>call AcidSendEval("eval_print")<CR>
105105
map <Plug>(acid-replace-op) <Cmd>set opfunc=AcidEvalInplace<CR>g@
106106
map <Plug>(acid-replace-symbol) <Cmd>call AcidEvalInplace("symbol")<CR>
107107
map <Plug>(acid-replace-visual) <Cmd>call AcidEvalInplace("visual")<CR>
108-
map <Plug>(acid-replace-top-expr) <Cmd>lua require("acid.features").eval_inplace("top")<CR>
109-
map <Plug>(acid-replace-expr) <Cmd>lua require("acid.features").eval_inplace()<CR>
108+
map <Plug>(acid-replace-top-expr) <Cmd>lua require("acid.features").eval_expr("top", true)<CR>
109+
map <Plug>(acid-replace-expr) <Cmd>lua require("acid.features").eval_expr(nil, true)<CR>
110110
111111
map <Plug>(acid-thread-first) <Cmd>lua require("acid.features").thread_first()<CR>
112112
map <Plug>(acid-thread-last) <Cmd>lua require("acid.features").thread_last()<CR>

0 commit comments

Comments
 (0)