Skip to content

Commit 3b23e6a

Browse files
committed
cmds
1 parent 0f237ed commit 3b23e6a

File tree

16 files changed

+520
-329
lines changed

16 files changed

+520
-329
lines changed

lua/down.lua

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ Down = {
1515
}
1616

1717
Down.default = {
18-
['lsp'] = {},
18+
-- ['lsp'] = {},
19+
['template'] = {},
20+
['mod'] = {},
1921
['cmd'] = {},
2022
['link'] = {},
2123
['tool.telescope'] = {},
@@ -30,8 +32,8 @@ Down.default = {
3032
--- @param ... string The arguments to pass into an optional user hook
3133
function Down.setup(user, ...)
3234
Down.util.log.trace('Setting up Down')
33-
Down.config:setup(user, Down.default, ...)
34-
Down:start()
35+
Down.config.setup(Down.config, user, Down.default, ...)
36+
Down.start(Down)
3537
end
3638

3739
function Down:start()

lua/down/mod.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,9 @@ Mod.handle_cmd = function(self, e, cmd, cmds, ...)
382382
if not cmds or type(cmds) ~= 'table' or not cmds[cmd] then
383383
return false
384384
end
385+
if cmds.enabled ~= nil and cmds.enabled == false then return false end
385386
local cc = cmds[cmd]
387+
if cc.enabled ~= nil and cc.enabled == false then return false end
386388
if cc.name and cc.name == cmd and cc.callback then
387389
if not self.handle then
388390
self.handle = {}

lua/down/mod/cmd/init.lua

Lines changed: 80 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,58 @@ M.setup = function()
1313
return { loaded = true, dependencies = {} }
1414
end
1515

16-
M.commands = {}
16+
M.commands = {
17+
cmd = {
18+
enabled = true,
19+
name = 'cmd',
20+
args = 0,
21+
max_args = 1,
22+
callback = function(e)
23+
log.trace 'Cmd callback'
24+
end,
25+
subcommands = {
26+
add = {
27+
name = 'cmd.add',
28+
args = 0,
29+
max_args = 1,
30+
callback = function(e)
31+
log.trace 'Cmd add callback'
32+
end,
33+
},
34+
edit = {
35+
name = 'cmd.edit',
36+
args = 0,
37+
max_args = 1,
38+
callback = function(e)
39+
log.trace 'Cmd edit callback'
40+
end,
41+
},
42+
remove = {
43+
name = 'cmd.remove',
44+
args = 0,
45+
max_args = 1,
46+
callback = function(e)
47+
log.trace 'Cmd remove callback'
48+
end,
49+
},
50+
update = {
51+
name = 'cmd.update',
52+
args = 0,
53+
max_args = 1,
54+
callback = function(e)
55+
log.trace 'Cmd update callback'
56+
end,
57+
},
58+
},
59+
60+
},
61+
}
1762

1863
--- Handles the calling of the appropriate function based on the command the user entered
1964
M.cb = function(data)
20-
-- vim.print(data)
2165
local args = data.fargs
22-
23-
local current_buf = vim.api.nvim_get_current_buf()
24-
local is_down = vim.bo[current_buf].filetype == 'markdown'
66+
local buf = vim.api.nvim_get_current_buf()
67+
local is_down = vim.bo[buf].filetype == 'markdown'
2568

2669
local function check_condition(condition)
2770
if condition == nil then
@@ -33,7 +76,7 @@ M.cb = function(data)
3376
end
3477

3578
if type(condition) == 'function' then
36-
return condition(current_buf, is_down)
79+
return condition(buf, is_down)
3780
end
3881

3982
return condition
@@ -50,6 +93,7 @@ M.cb = function(data)
5093
end
5194

5295
ref = ref.subcommands[cmd]
96+
if ref.enabled == false then return end
5397

5498
if not ref then
5599
log.error(
@@ -126,10 +170,10 @@ M.cb = function(data)
126170
vim.list_slice(args, argument_index + 1)
127171
)
128172
if ref.callback then
129-
log.trace('Cmd..cb: Running ', ref.name, ' callback')
173+
log.trace('Cmd.cb: Running ', ref.name, ' callback')
130174
ref.callback(e)
131175
else
132-
log.trace('Cmd..cb: Running ', ref.name, ' broadcast')
176+
log.trace('Cmd.cb: Running ', ref.name, ' broadcast')
133177
mod.broadcast(e)
134178
end
135179
end
@@ -173,6 +217,7 @@ M.generate_completions = function(_, command)
173217
local last_completion_level = 0
174218

175219
for _, cmd in ipairs(splitcmd) do
220+
if ref.enabled ~= nil and ref.enabled == false then return end
176221
if not ref or not M.check_condition(ref.condition) then
177222
break
178223
end
@@ -224,7 +269,9 @@ M.generate_completions = function(_, command)
224269
local subcommands = (ref and ref.subcommands or last_valid_ref.subcommands) or {}
225270

226271
return vim.tbl_filter(function(key)
227-
return M.check_condition(subcommands[key].condition)
272+
if type(subcommands[key]) == 'table' then
273+
return M.check_condition(subcommands[key].condition)
274+
end
228275
end, keys)
229276
end
230277
end
@@ -249,6 +296,7 @@ M.select_next_cmd_arg = function(qargs, choices)
249296

250297
query({
251298
prompt = current,
299+
252300
}, function(choice)
253301
if choice ~= nil then
254302
vim.cmd(('%s %s'):format(current, choice))
@@ -262,42 +310,31 @@ end
262310
M.add_commands = function(mod_name)
263311
local mod_config = mod.get_mod(mod_name)
264312

265-
if not mod_config or not mod_config.commands then
313+
if not mod_config or not mod_config.commands
314+
or (mod_config.commands.enabled ~= nil and mod_config.commands.enabled == false)
315+
then
266316
return
267317
end
268318

269319
M.commands = vim.tbl_extend('force', M.commands, mod_config.commands)
270320
end
271321

272322
--- Recursively merges the provided table with the mod.config.commands table.
273-
---@param functions down.Commands #A table that follows the mod.config.commands structure
274-
M.add_commands_from_table = function(functions)
275-
M.commands = vim.tbl_extend('force', M.commands, functions)
276-
end
277-
278-
--- Takes a relative path (e.g "list.mod") and loads it from the commands/ directory
279-
---@param name string #The relative path of the init we want to load
280-
M.add_commands_from_file = function(name)
281-
-- Attempt to require the file
282-
local err, ret = pcall(require, 'down.mod.cmd.' .. name)
283-
284-
-- If we've failed bail out
285-
if not err then
286-
log.warn(
287-
'Could not load command'
288-
.. name
289-
.. 'for init base.cmd - the corresponding mod.lua file does not exist.'
290-
)
291-
return
292-
end
293-
mod.load_mod_from_table(ret)
323+
---@param f down.Commands #A table that follows the mod.config.commands structure
324+
M.add_commands_from_table = function(f)
325+
M.commands = vim.tbl_extend('force', M.commands, f)
294326
end
295327

296328
--- Rereads data from all mod and rebuild the list of available autocompletiinitinitons and commands
297329
M.sync = function()
298330
for _, lm in pairs(mod.mods) do
299331
if lm.commands then
300-
M.add_commands_from_table(lm.commands)
332+
if lm.commands.enabled ~= nil and lm.commands.enabled == false then
333+
return
334+
end
335+
M.commands = vim.tbl_extend('force', M.commands, lm.commands)
336+
-- lm.commands = M.load_cmds(lm)
337+
-- M.add_commands_from_table(lm.commands)
301338
end
302339
end
303340
end
@@ -317,32 +354,25 @@ M.load = function()
317354
nargs = '*',
318355
complete = M.generate_completions,
319356
})
320-
for _, command in ipairs(M.config.load) do
321-
if command == 'default' then
322-
for _, basecmd in ipairs(M.config.base) do
323-
M.add_commands_from_file(basecmd)
324-
end
325-
end
326-
end
327357
end
328358

329359
---@class down.mod.cmd.Config
330360
M.config = {
331-
load = {
332-
'default',
333-
},
334-
335-
base = {
336-
'mod',
337-
},
338361
}
339362
---@class cmd
340363

341-
M.post_load = function()
342-
for _, l in pairs(mod.mods) do
343-
M.commands = vim.tbl_extend('force', M.commands, l.commands or {})
344-
M.add_commands_from_table(l.commands or {})
364+
M.load_cmds = function(m)
365+
for _, c in pairs(m.commands or {}) do
366+
local cmds = {}
367+
if m.commands.enabled ~= false then
368+
cmds = vim.tbl_extend('force', cmds, c)
369+
end
370+
l = vim.tbl_extend('force', l, cmds)
345371
end
372+
return l
373+
end
374+
375+
M.post_load = function()
346376
M.sync()
347377
end
348378

lua/down/mod/code/init.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Code.config = {
1515
Code.code = {}
1616

1717
Code.commands = {
18+
enabled = true,
1819
code = {
1920
name = 'code',
2021
condition = 'markdown',

0 commit comments

Comments
 (0)