Skip to content

Commit dff1520

Browse files
authored
Fix/motion (#47)
* #39: Fix Motion * #39: Fix for cpp * #39: Add visual send * #39: Also print the result * Fix import * Fix log * Fix matching * Avoid committing unwanted functions
1 parent 9667705 commit dff1520

File tree

8 files changed

+99
-26
lines changed

8 files changed

+99
-26
lines changed

API.md

+15-2
Original file line numberDiff line numberDiff line change
@@ -160,13 +160,26 @@ Returns the coordinates for the boundaries of the current form
160160
**(table)** coordinates {from = {row,col}, to = {row,col}}
161161

162162

163+
## `acid.forms.form_from_motion(mode[, bufnr])`
164+
Extracts the form according to given motion (or visual mode)
165+
166+
mode **(string)**: Motion mode or 'visual'
167+
168+
*bufnr* **(int)**: Buffer number in neovim. Will take current if none given
169+
170+
171+
**(string)** symbol under cursor
172+
173+
**(table)** coordinates {from = {row,col}, to = {row,col}, bufnr = 1}
174+
175+
163176
## `acid.forms.form_under_cursor()`
164177
Extracts the innermost form under the cursor
165178

166179

167180
**(string)** symbol under cursor
168181

169-
**(table)** coordinates {from = {row,col}, to = {row,col}}
182+
**(table)** coordinates {from = {row,col}, to = {row,col}, bufnr = 1}
170183

171184

172185
## `acid.forms.symbol_under_cursor()`
@@ -175,7 +188,7 @@ Extracts the symbol under the cursor
175188

176189
**(string)** symbol under cursor
177190

178-
**(table)** coordinates {from = {row,col}, to = {row,col}}
191+
**(table)** coordinates {from = {row,col}, to = {row,col}, bofnr = 1}
179192

180193

181194
---

githooks/pre-commit/no-taps

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/bin/bash
2+
ROOT_DIR="$(git rev-parse --show-toplevel)"
3+
4+
grep -q tap "${ROOT_DIR}"/lua/**/*.lua && {
5+
echo "Don't commit a 'tap'."
6+
exit 1
7+
}
8+
9+
exit 0
10+

lua/acid/commands.lua

+9
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,15 @@ commands.req = function(obj)
3636
return ops.eval{code = code}
3737
end
3838

39+
commands.import = function(obj)
40+
41+
local code = "(import '(" ..
42+
obj.java_ns .. " " .. table.concat(obj.symbols, " ") ..
43+
"))"
44+
45+
return ops.eval{code = code}
46+
end
47+
3948
commands.ns_load_all = function()
4049
return ops['ns-load-all']{}
4150
end

lua/acid/features.lua

+10-12
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,16 @@ features.eval_expr = function(mode, ns)
6161
local form = forms.form_under_cursor()
6262
payload.code = table.concat(form, "\n")
6363
else
64-
local bufnr = vim.api.nvim_call_function("bufnr", {"%"})
65-
payload.code = table.concat(forms.extract(bufnr, mode), "\n")
64+
local lines = forms.form_from_motion(mode)
65+
payload.code = table.concat(lines, "\n")
6666
end
6767
ns = ns or vim.api.nvim_call_function("AcidGetNs", {})
6868
if ns ~= nil or ns ~= "" then
6969
payload.ns = ns
7070
end
7171

7272
acid.run(ops.eval(payload):with_handler(middlewares
73+
.print{}
7374
.clipboard{}
7475
.virtualtext{}
7576
))
@@ -94,7 +95,7 @@ end
9495
-- @tparam string java_ns Namespace of the java symbols that are being imported.
9596
-- @tparam {string,...} symbols List of java symbols to be imported
9697
features.do_import = function(java_ns, symbols)
97-
acid.run(commands.req{java_ns = java_ns, symbols = symbols}:with_handler(middlewares
98+
acid.run(commands.import{java_ns = java_ns, symbols = symbols}:with_handler(middlewares
9899
.doautocmd{autocmd = "AcidImported"}
99100
))
100101
end
@@ -177,42 +178,39 @@ end
177178
--`(:requires ...)` section.
178179
-- @tparam string req require vector, such as `[clojure.string :as str]`.
179180
features.add_require = function(req)
180-
local bufnr = vim.api.nvim_call_function("bufnr", {"%"})
181-
local lines, coords = forms.extract(bufnr)
181+
local lines, coords = forms.form_under_cursor()
182182
local content = table.concat(lines, "")
183183

184184
local code = "(format-code (upd-ns '" .. content .. " :require (partial add-req '" .. req .. ")))"
185185

186186
acid.run(ops.eval{code = code, ns = "acid.inject"}:with_handler(middlewares
187-
.refactor{from = coords.from, to = coords.to, bufnr = bufnr}
187+
.refactor(coords)
188188
))
189189
end
190190

191191
--- Refactor the current file to remove the given argument from the
192192
--`(:requires ...)` section.
193193
-- @tparam string req require namespace, such as `clojure.string`.
194194
features.remove_require = function(req)
195-
local bufnr = vim.api.nvim_call_function("bufnr", {"%"})
196-
local lines, coords = forms.extract(bufnr)
195+
local lines, coords = forms.form_under_cursor()
197196
local content = table.concat(lines, "")
198197

199198
local code = "(format-code (upd-ns '" .. content .. " :require (partial rem-req '" .. req .. ")))"
200199

201200
acid.run(ops.eval{code = code, ns = "acid.inject"}:with_handler(middlewares
202-
.refactor{from = coords.from, to = coords.to, bufnr = bufnr}
201+
.refactor(coords)
203202
))
204203
end
205204

206205
--- Refactor the current file so the `(:require ...)` form is sorted.
207206
features.sort_requires = function()
208-
local bufnr = vim.api.nvim_call_function("bufnr", {"%"})
209-
local lines, coords = forms.extract(bufnr)
207+
local lines, coords = forms.form_under_cursor()
210208
local content = table.concat(lines, "")
211209

212210
local code = "(format-code (upd-ns '" .. content .. " :require sort-reqs))"
213211

214212
acid.run(ops.eval{code = code, ns = "acid.inject"}:with_handler(middlewares
215-
.refactor{from = coords.from, to = coords.to, bufnr = bufnr}
213+
.refactor(coords)
216214
))
217215

218216
end

lua/acid/forms.lua

+50-9
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,41 @@ forms.get_form_boundaries = function()
3030

3131
return {
3232
from = from,
33-
to = to
33+
to = to,
34+
bufnr = vim.api.nvim_get_current_buf()
3435
}
3536
end
3637

37-
forms.extract = function(bufnr)
38-
local coordinates = forms.get_form_boundaries()
38+
forms.motion = function(bufnr, mode)
39+
local b_line, b_col, e_line, e_col, _
40+
41+
if mode == 'visual' then
42+
_, b_line, b_col = unpack(vim.api.nvim_call_function("getpos", {"v"}))
43+
_, e_line, e_col = unpack(vim.api.nvim_call_function("getpos", {"."}))
44+
45+
b_col = b_col - 1
46+
e_col = e_col - 1
47+
elseif mode == 'block' then
48+
b_line, b_col = unpack(vim.api.nvim_buf_get_mark(bufnr, '<'))
49+
e_line, e_col = unpack(vim.api.nvim_buf_get_mark(bufnr, '>'))
50+
else
51+
b_line, b_col = unpack(vim.api.nvim_buf_get_mark(bufnr, '['))
52+
e_line, e_col = unpack(vim.api.nvim_buf_get_mark(bufnr, ']'))
53+
end
54+
55+
b_col = b_col + 1
56+
e_col = e_col + 2
57+
58+
return {
59+
from = {b_line, b_col},
60+
to = {e_line, e_col},
61+
bufnr = bufnr
62+
}
63+
end
3964

40-
local lines = vim.api.nvim_buf_get_lines(bufnr, coordinates.from[1] - 1, coordinates.to[1], 0)
65+
66+
forms.extract = function(coordinates)
67+
local lines = vim.api.nvim_buf_get_lines(coordinates.bufnr, coordinates.from[1] - 1, coordinates.to[1], 0)
4168

4269
if coordinates.from[2] ~= 0 then
4370
lines[1] = string.sub(lines[1], coordinates.from[2])
@@ -54,26 +81,40 @@ forms.extract = function(bufnr)
5481
return lines, coordinates
5582
end
5683

84+
--- Extracts the form according to given motion (or visual mode)
85+
-- @tparam string mode Motion mode or 'visual'
86+
-- @tparam[opt] int bufnr Buffer number in neovim. Will take current if none given
87+
-- @treturn string symbol under cursor
88+
-- @treturn table coordinates {from = {row,col}, to = {row,col}, bufnr = 1}
89+
forms.form_from_motion = function(mode, bufnr)
90+
bufnr = bufnr or vim.api.nvim_get_current_buf()
91+
local coordinates = forms.motion(bufnr, mode)
92+
93+
return forms.extract(coordinates)
94+
95+
end
96+
5797
--- Extracts the innermost form under the cursor
5898
-- @treturn string symbol under cursor
59-
-- @treturn table coordinates {from = {row,col}, to = {row,col}}
99+
-- @treturn table coordinates {from = {row,col}, to = {row,col}, bufnr = 1}
60100
forms.form_under_cursor = function()
61-
local cb = vim.api.nvim_get_current_buf()
101+
local coordinates = forms.get_form_boundaries()
62102

63-
return forms.extract(cb)
103+
return forms.extract(coordinates)
64104
end
65105

66106
--- Extracts the symbol under the cursor
67107
-- @treturn string symbol under cursor
68-
-- @treturn table coordinates {from = {row,col}, to = {row,col}}
108+
-- @treturn table coordinates {from = {row,col}, to = {row,col}, bofnr = 1}
69109
forms.symbol_under_cursor = function()
70110
local cw = vim.api.nvim_call_function("expand", {"<cword>"})
71111
local from = vim.api.nvim_call_function("searchpos", {cw, "nc"})
72112
local to = vim.api.nvim_call_function("searchpos", {cw, "nce"})
73113

74114
return cw, {
75115
from = from,
76-
to = to
116+
to = to,
117+
bufnr = vim.api.nvim_get_current_buf()
77118
}
78119
end
79120

lua/acid/log.lua

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
-- luacheck: globals vim
22
local log = {}
33

4-
log.msg = function(msg)
5-
vim.api.nvim_out_write("[Acid] " .. msg .. "\n")
4+
log.msg = function(...)
5+
vim.api.nvim_out_write("[Acid] " .. table.concat({...}, " ") .. "\n")
66
end
77

88
return log

lua/acid/nrepl.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ nrepl.handle = {
174174
local opts = pending[ch]
175175
local port = connections.store[opts.ix][2]
176176
connections:select(opts.pwd, opts.ix)
177-
log.msg("Connected on port" .. tostring(port))
177+
log.msg("Connected on port", tostring(port))
178178
vim.api.nvim_command("doautocmd User AcidConnected")
179179
pending[ch] = nil
180180
end

plugin/acid.vim

+2
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ map <Plug>(acid-go-to) <Cmd>lua require("acid.features").go_to()<CR>
5454
map <Plug>(acid-docs) <Cmd>lua require("acid.features").docs()<CR>
5555
map <Plug>(acid-eval-cmdline) <Cmd>call AcidSendEval("eval_cmdline")<CR>
5656
map <Plug>(acid-motion-op) <Cmd>set opfunc=AcidMotion<CR>g@
57+
map <Plug>(acid-eval-visual) <Cmd>call AcidMotion("visual")<CR>
5758
map <Plug>(acid-eval-expr) <Cmd>lua require("acid.features").eval_expr()<CR>
5859
map <Plug>(acid-eval-print) <Cmd>call AcidSendEval("eval_print")<CR>
5960
@@ -82,6 +83,7 @@ if !g:acid_no_default_keymappings
8283
autocmd FileType clojure nmap <buffer> <silent> <C-c>x <Plug>(acid-eval-cmdline)
8384
autocmd FileType clojure imap <buffer> <silent> <C-c>x <Plug>(acid-eval-cmdline)
8485
autocmd FileType clojure map <buffer> <silent> cp <Plug>(acid-motion-op)
86+
autocmd FileType clojure vmap <buffer> <silent> cp <Plug>(acid-eval-visual)
8587
autocmd FileType clojure map <buffer> <silent> cpp <Plug>(acid-eval-expr)
8688
autocmd FileType clojure map <buffer> <silent> cqp <Plug>(acid-eval-print)
8789

0 commit comments

Comments
 (0)