Skip to content

Commit 09c962f

Browse files
authored
Babashka nrepl (#80)
* Adapt to modern API * Drop unused nrepl flags * Fix print not working for bad statuses * Remove TAP * Add require to utils * Add babashka nrepl server * Replace nrepl server for admin sessions * Add flag for not autosaving * Improvements to output buffer * Ensure uses babashka only if it exists
1 parent 43b0d15 commit 09c962f

11 files changed

+131
-49
lines changed

lua/acid/connections.lua

+3-3
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ end
5757
-- @tparam[opt] string pwd path (usually project root).
5858
-- @treturn string Id of the current connection for the path or nil.
5959
connections.peek = function(pwd)
60-
pwd = pwd_to_key(pwd or vim.api.nvim_call_function("getcwd", {}))
60+
pwd = pwd_to_key(pwd or vim.fn.getcwd())
6161
return connections.current[pwd]
6262
end
6363

@@ -76,9 +76,9 @@ end
7676

7777
connections.search = function(pwd)
7878
pwd = pwd_to_key(pwd)
79-
local fpath = vim.api.nvim_call_function("findfile", {pwd .. ".nrepl-port"})
79+
local fpath = vim.fn.findfile(pwd .. ".nrepl-port")
8080
if fpath ~= "" then
81-
local portno = table.concat(vim.api.nvim_call_function("readfile", {fpath}), "")
81+
local portno = table.concat(vim.fn.readfile(fpath), "")
8282
local conn = {"127.0.0.1", utils.trim(portno)}
8383
return connections.add(conn)
8484
end

lua/acid/core.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ core.send = function(connection, obj, handler)
4343
end
4444

4545
if obj.code ~= nil then
46-
output.draw(conn_id, {(obj.ns or "") .. "=> " .. obj.code})
46+
output.draw(conn_id, (obj.ns or "") .. "=> " .. obj.code)
4747
end
4848

4949
if obj.id == nil then

lua/acid/init.lua

+22-8
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ local connections = require("acid.connections")
88
local utils = require("acid.utils")
99
local sessions = require("acid.sessions")
1010
local admin_session_path
11+
local has_bb
1112
local acid = {}
1213

1314
--- Checks whether a connection exists for supplied path or not.
@@ -57,24 +58,37 @@ end
5758
-- or for things that clojure could deal with better while not having a
5859
-- nrepl session to use.
5960
acid.admin_session_start = function()
61+
local nrepl = require("acid.nrepl")
6062
if admin_session_path == nil then
61-
admin_session_path = vim.fn.fnamemodify(vim.fn.findfile("admin_deps.edn", vim.api.nvim_get_option('rtp')), ":p:h")
63+
admin_session_path = utils.ensure_path(
64+
vim.fn.fnamemodify(vim.fn.findfile("admin_deps.edn", vim.api.nvim_get_option('rtp')), ":p:h")
65+
)
6266
end
6367

64-
if require("acid.nrepl").cache[admin_session_path] ~= nil then
68+
if has_bb == nil then
69+
has_bb = vim.fn.executable('bb') == 1
70+
end
6571

72+
if nrepl.cache[admin_session_path] ~= nil then
6673
return acid.admin_session()
6774
end
6875

69-
local nrepl = require("acid.nrepl")
70-
nrepl.start{
71-
pwd = admin_session_path,
72-
skip_autocmd = true,
73-
deps_file = admin_session_path .. "/admin_deps.edn"
74-
}
76+
if has_bb then
77+
nrepl.bbnrepl{
78+
pwd = admin_session_path,
79+
}
80+
else
81+
nrepl.start{
82+
pwd = admin_session_path,
83+
skip_autocmd = true,
84+
deps_file = admin_session_path .. "/admin_deps.edn"
85+
}
86+
end
87+
7588
end
7689

7790
acid.admin_session = function()
91+
7892
local conn = connections.get(admin_session_path)
7993

8094
if conn ~= nil and conn[2] ~= nil then

lua/acid/middlewares/output.lua

+5-8
Original file line numberDiff line numberDiff line change
@@ -12,26 +12,23 @@ output.middleware = function(config)
1212
local conn_id = sessions.reverse_lookup(session)
1313

1414
if data.out ~= nil then
15-
local out = {}
16-
data.out:gsub("[^\n]+", function(dt) table.insert(out, dt) end)
17-
output.draw(conn_id, out)
15+
output.draw(conn_id, data.out)
1816
end
1917
if data.value ~= nil then
20-
output.draw(conn_id, {"=> " .. data.value})
18+
output.draw(conn_id, "=> " .. data.value)
2119
end
2220
if data.ex ~= nil then
23-
output.draw(conn_id, {"!! " .. data.ex})
21+
output.draw(conn_id, "!! " .. data.ex)
2422
end
2523
if data.err ~= nil then
2624
local out = {}
27-
data.err:gsub("[^\n]+", function(dt) table.insert(out, "!! " .. dt) end)
28-
output.draw(conn_id, out)
25+
output.draw(conn_id, data.err)
2926
end
3027

3128
if config.accessor ~= nil then
3229
local msg = config.accessor(data)
3330
if msg ~= nil and msg ~= "" then
34-
output.draw(conn_id, {"<> " .. msg})
31+
output.draw(conn_id, "<> " .. msg)
3532
end
3633
end
3734

lua/acid/middlewares/print.lua

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ do_print.middleware = function(config)
2626
end
2727
end
2828

29-
if utils.find(config.status, "namespace-not-found") then
29+
if utils.find(data.status, "namespace-not-found") then
3030
log.msg("Namespace not found")
31-
elseif utils.find(config.status, "error") then
31+
elseif utils.find(data.status, "error") then
3232
log.msg("Error")
3333
end
3434

lua/acid/middlewares/quickfix.lua

-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ quickfix.set = function(config)
3030
'**/' .. assert.file,
3131
false,
3232
true)[1]
33-
tap(fpath)
3433
table.insert(qf, {
3534
module = ns .. "/" .. test,
3635
lnum = assert.line,
@@ -42,7 +41,6 @@ quickfix.set = function(config)
4241
end
4342
end
4443
end
45-
tap(qf)
4644
vim.fn.setqflist({}, 'r', {
4745
id = config.qflistid,
4846
items = qf

lua/acid/nrepl.lua

+41-19
Original file line numberDiff line numberDiff line change
@@ -79,15 +79,6 @@ local build_cmd = function(obj)
7979
table.insert(opts, obj.bind)
8080
end
8181

82-
if obj.host ~= nil or obj.connect ~= nil then
83-
table.insert(opts,"-c")
84-
end
85-
86-
if obj.host ~= nil then
87-
table.insert(opts,"-h")
88-
table.insert(opts, obj.host)
89-
end
90-
9182
return opts
9283
end
9384

@@ -102,17 +93,13 @@ nrepl.default_middlewares = {'nrepl/nrepl', 'cider/cider-nrepl', 'refactor-nrepl
10293
-- @tparam[opt] string obj.pwd Path where the nrepl process will be started
10394
-- @tparam[opt] table obj.middlewares List of middlewares.
10495
-- @tparam[opt] string obj.alias aliases on the local deps.edn
105-
-- @tparam[opt] string obj.connect -c parameter for the nrepl process
96+
-- @tparam[opt] int obj.port -p parameter for the nrepl process
10697
-- @tparam[opt] string obj.bind -b parameter for the nrepl process
10798
-- @tparam[opt] boolean obj.skip_autocmd don't fire an autocmd after starting this repl
10899
-- @tparam[opt] boolean obj.disable_output_capture disables output capturing.
109100
-- @treturn boolean Whether it was possible to spawn a nrepl process
110101
nrepl.start = function(obj)
111-
local pwd = obj.pwd or vim.api.nvim_call_function("getcwd", {})
112-
113-
if not utils.ends_with(pwd, "/") then
114-
pwd = pwd .. "/"
115-
end
102+
local pwd = utils.ensure_path(obj.pwd or vim.api.nvim_call_function("getcwd", {}))
116103

117104
local selected = obj.middlewares or nrepl.default_middlewares
118105
local bind = obj.bind
@@ -121,8 +108,6 @@ nrepl.start = function(obj)
121108
port = obj.port,
122109
alias = obj.alias,
123110
bind = obj.bind,
124-
host = obj.host,
125-
connect = obj.connect,
126111
deps_file = obj.deps_file
127112
}
128113

@@ -139,7 +124,7 @@ nrepl.start = function(obj)
139124

140125
if ret <= 0 then
141126
-- TODO log, inform..
142-
return
127+
return false
143128
end
144129

145130
local conn = {bind, obj.port}
@@ -155,12 +140,49 @@ nrepl.start = function(obj)
155140
nrepl.cache[pwd].id = conn_id
156141

157142
job_mapping[ret] = {pwd = pwd, conn = conn_id, init = false}
158-
if obj.disable_output_capture == false then
143+
144+
if not obj.disable_output_capture then
159145
output.buffer(conn_id)
160146
end
161147
return true
162148
end
163149

150+
nrepl.bbnrepl = function(obj)
151+
local pwd = utils.ensure_path(obj.pwd or vim.api.nvim_call_function("getcwd", {}))
152+
153+
obj.port = obj.port or "1667"
154+
155+
local cmd = {
156+
"bb", "--nrepl-server", obj.port
157+
}
158+
159+
local ret = nvim.nvim_call_function('jobstart', {
160+
cmd , {
161+
on_exit = "AcidJobCleanup",
162+
cwd = pwd
163+
}
164+
})
165+
166+
if ret <= 0 then
167+
-- TODO log, inform..
168+
return false
169+
end
170+
171+
local conn = {"127.0.0.1", obj.port}
172+
173+
nrepl.cache[pwd] = {
174+
skip_autocmd = true,
175+
job = ret,
176+
addr = conn
177+
}
178+
179+
local conn_id = connections.add(conn)
180+
connections.select(obj.pwd, conn_id)
181+
182+
nrepl.cache[pwd].id = conn_id
183+
184+
end
185+
164186
--- Stops a nrepl process managed by acid
165187
-- @tparam table obj Configuration for the nrepl process to be stopped
166188
-- @tparam string obj.pwd Path where the nrepl process was started

lua/acid/output.lua

+32-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
-- luacheck: globals vim
22

3+
local connections = require("acid.connections")
34
local output = {}
45

56
local location = function(opts)
@@ -10,7 +11,6 @@ local location = function(opts)
1011
width = opts.width or 60,
1112
height = height,
1213
row = 0,
13-
focusable = false,
1414
col = width - (opts.width or 60)
1515
}
1616
end
@@ -29,7 +29,7 @@ output.buffer = function(conn)
2929
end
3030

3131
output.window = function(conn, opts)
32-
local buf = output.buffer(conn)
32+
local buf = output.buffer(conn)
3333
local winnr = vim.fn.bufwinnr(buf)
3434
local winid
3535

@@ -45,7 +45,7 @@ output.window = function(conn, opts)
4545
vim.api.nvim_set_current_win(winid)
4646
end
4747

48-
output.close_window = function(conn, opts)
48+
output.close_window = function(conn)
4949
local buf = output.buffer(conn)
5050
local winnr = vim.fn.bufwinnr(buf)
5151

@@ -55,14 +55,42 @@ output.close_window = function(conn, opts)
5555
end
5656
end
5757

58-
5958
output.draw = function(conn, lines)
6059
local buf = output.conn_to_buf[conn]
60+
61+
if type(lines) == "string" then
62+
old = lines
63+
lines = {}
64+
old:gsub("[^\n]+", function(dt) table.insert(lines, dt) end)
65+
end
6166
if buf == nil then
6267
return
6368
end
6469

6570
vim.api.nvim_buf_set_lines(buf, -1, -1, false, lines)
6671
end
6772

73+
74+
output.open = function()
75+
local conn = connections.peek()
76+
77+
output.window(conn)
78+
end
79+
80+
output.close = function()
81+
local conn = connections.peek()
82+
output.close_window(conn)
83+
end
84+
85+
output.clear = function(conn)
86+
conn = conn or connections.peek()
87+
88+
local buf = output.conn_to_buf[conn]
89+
if buf == nil then
90+
return
91+
end
92+
93+
vim.api.nvim_buf_set_lines(buf, 0, -1, false, {})
94+
end
95+
6896
return output

lua/acid/sessions.lua

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
-- luacheck: globals vim
22
local connections = require("acid.connections")
33
local ops = require("acid.ops")
4+
local utils = require("acid.utils")
45
local core = require("acid.core")
56

67
local pwd_to_key = function(pwd)

lua/acid/utils.lua

+7
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@ utils.pack = function (...)
55
return {n=select('#',...); ...}
66
end
77

8+
utils.ensure_path = function(pwd)
9+
if not utils.ends_with(pwd, "/") then
10+
pwd = pwd .. "/"
11+
end
12+
return pwd
13+
end
14+
815

916

1017
utils.trim = vim.trim or function(str)

0 commit comments

Comments
 (0)