-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnrepl.lua
324 lines (276 loc) · 7.59 KB
/
nrepl.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
-- luacheck: globals unpack vim
local impromptu = require("impromptu")
local connections = require('acid.connections')
local nrepl = require('acid.nrepl')
local acid_utils = require('acid.utils')
local acid = require("acid")
local eval = require("acid.ops").eval
local find_value = function(tbl, val)
for ix, v in ipairs(tbl) do
if v == val then
return ix
end
end
return -1
end
local jazz_nrepl = {}
local select_portno = function(handler)
return impromptu.new.form{
title = "🎵 Select port number:",
questions = {portno = {description = "Port number"}},
handler = function(session, result)
return handler(session, result)
end
}
end
local existing = function(obj)
return select_portno(function(_, handler)
local ix = connections.add{"127.0.0.1", tonumber(handler.portno)}
connections.select(obj.pwd, ix)
return true
end)
end
local run_alias = function(obj)
local options = {
start = {
description = "Start",
hl = "Function"
}
}
local spawn_options = {
pwd = obj.pwd,
alias = {}
}
local new_session = obj.session:stack(impromptu.new.ask{
title = "🎵 From deps.edn",
options = options,
handler = function(session, selected)
if selected.tp == "alias" then
table.insert(spawn_options.alias, selected.description)
session.lines[selected.description].hl = "String"
else
nrepl.start(spawn_options)
return true
end
end
})
local admin = acid.admin_session()
acid.run(eval{
code = '(map println (keys (:aliases (clojure.edn/read-string (slurp "' ..
obj.pwd .. "/" ..obj.file ..
'")))))'}:with_handler(function(data)
if data.status or data.value then
return
end
for _, v in ipairs(acid_utils.split_lines(data.out)) do
options[v] = {
description = v,
tp = "alias",
hl = "Comment"
}
end
new_session:render()
end),
admin)
return false
end
local toolsdeps = function(obj)
local files = vim.api.nvim_call_function("expand", {"**/*.edn", true, true})
if #files == 1 and files[1] == "deps.edn" then
local admin = acid.admin_session()
if admin == nil then
nrepl.start{pwd = obj.pwd}
return true
else
return run_alias{file = files[1], session = obj.session, pwd = obj.pwd}
end
end
local options = {}
for _, v in ipairs(files) do
options[v] = {
description = v,
hl = "Function"
}
end
obj.session:stack(impromptu.new.ask{
title = "🎵 Select deps.edn file",
options = options,
handler = function(session, selected)
return run_alias{file = selected.description, session = session, pwd = obj.pwd}
end
})
return false
end
local connect_nrepl = function(obj)
return impromptu.new.form{
title = "🎵 Connect nrepl to:",
questions = {
portno = {description = "Port number"},
host = {description = "Host address"}
},
handler = function(session, result)
obj.port = tonumber(result.portno)
obj.host = result.host
obj.connect = true
session:pop()
session.lines.connect.description = "Connect to remote nrepl? (true)"
session.lines.connect.hl = "String"
session.lines.host.description = "Connect to address(" .. result.host .. ")"
session.lines.host.hl = "String"
session.lines.port.description = "Port number (" .. result.portno .. ")"
session.lines.port.hl = "String"
return false
end
}
end
local custom_nrepl = function(obj)
local opts = {}
local nrepl_config = {middlewares = nrepl.default_middlewares, pwd = obj.pwd}
for k, _ in pairs(nrepl.middlewares) do
opts[k] = {
description = k,
hl = "Comment"
}
end
for _, k in ipairs(nrepl.default_middlewares) do
opts[k].hl = "String"
end
opts.port = {
description = "Port number (auto)",
hl = "Comment"
}
--opts.bind = {
--description = "Bind to address (127.0.0.1)",
--hl = "Comment"
--}
--opts.host = {
--description = "Connect to address (127.0.0.1)",
--hl = "Comment"
--}
--opts.connect = {
--description = "Connect to remote nrepl? (false)",
--hl = "Comment"
--}
--opts.pwd = {
--description = "Directory (" .. obj.pwd .. ")",
--hl = "Comment"
--}
opts.start = {description = "Start with custom configuration"}
opts.abort = {
description = "Abort",
key = "q"
}
return impromptu.new.ask{
title = "🎵 Configure the custom nrepl:",
quitable = false,
options = opts,
handler = function(session, selected)
if selected.index == "start" then
nrepl.start(nrepl_config)
return true
elseif selected.index == "port" then
session:stack(select_portno(function(ss, result)
ss:pop()
ss.lines.port.description = "Port number (" .. result.portno .. ")"
ss.lines.port.hl = "String"
return false
end))
elseif selected.index == "connect" then
session:stack(connect_nrepl(nrepl_config))
elseif selected.index == "abort" then
session:pop()
else
local ix = find_value(nrepl_config.middlewares, selected.index)
if ix == -1 then
table.insert(nrepl_config.middlewares, selected.index)
selected.hl = "String"
else
table.remove(nrepl_config.middlewares, ix)
selected.hl = "Comment"
end
end
end
}
end
jazz_nrepl.nrepl_menu = function(pwd)
pwd = pwd or vim.api.nvim_call_function("getcwd", {})
if not acid_utils.ends_with(pwd, "/") then
pwd = pwd .. "/"
end
local current = require('acid.connections').current[pwd]
local opts = {}
local check
if current ~= nil then
current = connections.store[current]
check = function (v)
return v[2] == current[2] and v[1] == current[1]
end
else
check = function(_) return false end
end
for ix, v in pairs(connections.store) do
if #v == 2 then
local opt = {}
local str = "nrepl://" .. v[1] .. ":" .. v[2]
if check(v) then
opts.close = {
description = "Close connection to " .. str ,
index = ix,
hl = "Function"
}
opts.refresh = {
description = "Refresh connections",
index = ix,
hl = "Function"
}
str = str .. " (current)"
opt.hl = "String"
end
opt.description = str
opt.index = ix
opts["conn" .. ix] = opt
end
end
opts.new = {
description = "Spawn new nrepl",
hl = "Function"
}
opts.custom = {
description = "Spawn custom nrepl",
hl = "Function"
}
opts.existing = {
description = "Connect to existing nrepl",
hl = "Function"
}
opts.toolsdeps = {
description = "Spawn from deps.edn",
hl = "Function"
}
impromptu.ask{
title = "🎵 Select nrepl to connect to:",
options = opts,
handler = function(session, selected)
if selected.index == "new" then
nrepl.start{pwd = pwd}
elseif selected.index == "close" then
nrepl.stop{pwd = pwd}
elseif selected.index == "refresh" then
nrepl.stop{pwd = pwd}
nrepl.start{pwd = pwd}
elseif selected.index == "toolsdeps" then
return toolsdeps{pwd = pwd, session = session}
elseif selected.index == "existing" then
session:stack(existing{pwd = pwd})
return false
elseif selected.index == "custom" then
session:stack(custom_nrepl{pwd = pwd})
return false
else
connections.select(pwd, selected.index)
end
return true
end
}
end
return jazz_nrepl