Skip to content

Commit 1e80524

Browse files
authored
Admin nrepl (#63)
* Add command for connecting to the nrepl * Deprecate UUID in favor or ULID * Short-circuit out of failure condition * Fixes on nrepl alias spawning + skipping autocmds * Add deprecation warning. * Add admin session * Auto-spawn admin session if configured.
1 parent f4f6466 commit 1e80524

File tree

6 files changed

+48
-8
lines changed

6 files changed

+48
-8
lines changed

lua/acid/connections.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ end
1919
--- Stores connection for reuse later
2020
-- @tparam {string,string} addr Address tuple with ip and port.
2121
connections.add = function(addr)
22-
local uuid = utils.uuid()
23-
connections.store[uuid] = addr
24-
return uuid
22+
local ulid = utils.ulid()
23+
connections.store[ulid] = addr
24+
return ulid
2525
end
2626

2727
connections.remove = function(addr)

lua/acid/core.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ core.send = function(conn, obj, handler)
2727

2828
if conn == nil then
2929
log.msg("No active connection to a nrepl session. Aborting")
30+
return
3031
end
3132

3233
if obj.id == nil then

lua/acid/init.lua

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,29 @@ acid.callback = function(ret)
5151
end
5252
end
5353

54+
--- Setup admin nrepl session
55+
-- This nrepl session should be used by plugins to deal with clojure code
56+
-- without injecting things in the user nrepl session
57+
-- or for things that clojure could deal with better while not having a
58+
-- nrepl session to use.
59+
acid.admin_session_start = function()
60+
local pwd = "/tmp/acid/admin/"
61+
if require("acid.nrepl").cache[pwd] ~= nil then
62+
return acid.admin_session()
63+
end
64+
65+
local nrepl = require("acid.nrepl")
66+
vim.api.nvim_call_function("mkdir", {pwd, "p"})
67+
nrepl.start{pwd = pwd, skip_autocmd = true}
68+
end
69+
70+
acid.admin_session = function()
71+
local pwd = "/tmp/acid/admin/"
72+
local conn = connections.get(pwd)
73+
if conn ~= nil and conn[2] ~= nil then
74+
return conn
75+
end
76+
return
77+
end
78+
5479
return acid

lua/acid/nrepl.lua

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ local build_cmd = function(obj)
6464
end
6565

6666
if obj.alias ~= nil then
67-
table.insert(opts, 4, obj.alias)
67+
table.insert(opts, 4, "-C" .. table.concat(obj.alias, ""))
68+
table.insert(opts, 4, "-R" .. table.concat(obj.alias, ""))
6869
end
6970

7071
if obj.port ~= nil then
@@ -89,7 +90,6 @@ local build_cmd = function(obj)
8990
return opts
9091
end
9192

92-
9393
nrepl.cache = {}
9494

9595
--- Default middlewares that will be used by the nrepl server
@@ -100,9 +100,10 @@ nrepl.default_middlewares = {'nrepl/nrepl', 'cider/cider-nrepl', 'refactor-nrepl
100100
-- @tparam table obj Configuration for the nrepl process to be spawn
101101
-- @tparam[opt] string obj.pwd Path where the nrepl process will be started
102102
-- @tparam[opt] table obj.middlewares List of middlewares.
103-
-- @tparam[opt] string obj.alias alias on the local deps.edn
103+
-- @tparam[opt] string obj.alias aliases on the local deps.edn
104104
-- @tparam[opt] string obj.connect -c parameter for the nrepl process
105105
-- @tparam[opt] string obj.bind -b parameter for the nrepl process
106+
-- @tparam[opt] boolean obj.skip_autocmd don't fire an autocmd after starting this repl
106107
-- @treturn boolean Whether it was possible to spawn a nrepl process
107108
nrepl.start = function(obj)
108109
local pwd = obj.pwd or vim.api.nvim_call_function("getcwd", {})
@@ -141,6 +142,7 @@ nrepl.start = function(obj)
141142
local conn = {bind, obj.port}
142143

143144
nrepl.cache[pwd] = {
145+
skip_autocmd = obj.skip_autocmd,
144146
job = ret,
145147
addr = conn
146148
}
@@ -178,9 +180,11 @@ nrepl.handle = {
178180
local port = ln:match("%d+")
179181
connections.store[opts.ix][2] = port
180182
connections.select(opts.pwd, opts.ix)
181-
log.msg("Connected on port", tostring(port))
182-
vim.api.nvim_command("doautocmd User AcidConnected")
183183
pending[ch] = nil
184+
if not nrepl.cache[opts.pwd].skip_autocmd then
185+
log.msg("Connected on port", tostring(port))
186+
vim.api.nvim_command("doautocmd User AcidConnected")
187+
end
184188
end
185189
end
186190
end

lua/acid/utils.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ utils.random = function(sz)
130130
end
131131

132132
utils.uuid = function()
133+
vim.api.nvim_err_writeln("Deprecated. Please use utils.ulid instead")
133134
return vim.api.nvim_call_function("AcidNewUUID", {})
134135
end
135136

plugin/acid.vim

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ if !exists("g:acid_skip_test_paths")
22
let g:acid_skip_test_paths = 1
33
endif
44

5+
if !exists("g:acid_start_admin_nrepl")
6+
let g:acid_start_admin_nrepl = 0
7+
endif
8+
59
let g:acid_no_default_keymappings = get(g:, 'acid_no_default_keymappings', 0)
610

711
function! AcidWrappedSend(payload, handler)
@@ -121,6 +125,11 @@ if !g:acid_no_default_keymappings
121125
augroup END
122126
endif
123127

128+
if g:acid_start_admin_nrepl
129+
lua require('acid').admin_session_start()
130+
endif
131+
132+
command! -nargs=0 AcidConnectNrepl lua require('acid.nrepl').start{}
124133
command! -nargs=? AcidClearVtext lua require('acid.middlewares.virtualtext').clear(<f-args>)
125134
command! -nargs=* AcidRequire lua require('acid.features').do_require(<f-args>)
126135
command! -nargs=1 AcidAddRequire call AcidFnAddRequire("[<args>]")

0 commit comments

Comments
 (0)