aboutsummaryrefslogtreecommitdiff
path: root/lua/tasks/subcommands.lua
diff options
context:
space:
mode:
Diffstat (limited to 'lua/tasks/subcommands.lua')
-rw-r--r--lua/tasks/subcommands.lua79
1 files changed, 79 insertions, 0 deletions
diff --git a/lua/tasks/subcommands.lua b/lua/tasks/subcommands.lua
new file mode 100644
index 0000000..a4ec9ca
--- /dev/null
+++ b/lua/tasks/subcommands.lua
@@ -0,0 +1,79 @@
1local tasks = require('tasks')
2local utils = require('tasks.utils')
3local constants = require('tasks.constants')
4local subcommands = {}
5
6--- Completes `:Task` command.
7---@param arg string: Current argument under cursor.
8---@param cmd_line string: All arguments.
9---@return table: List of all commands matched with `arg`.
10function subcommands.complete(arg, cmd_line)
11 local matches = {}
12
13 local words = vim.split(cmd_line, ' ', { trimempty = true })
14 if not vim.endswith(cmd_line, ' ') then
15 -- Last word is not fully typed, don't count it
16 table.remove(words, #words)
17 end
18
19 if #words == 1 then
20 for subcommand in pairs(tasks) do
21 if vim.startswith(subcommand, arg) and subcommand ~= 'setup' then
22 table.insert(matches, subcommand)
23 end
24 end
25 elseif #words == 2 then
26 if words[2] == 'start' or words[2] == 'set_task_param' or words[2] == 'set_module_param' then
27 local module_names = utils.get_module_names()
28 table.insert(module_names, 'auto') -- Special value for automatic module detection
29 for _, module_name in ipairs(module_names) do
30 if vim.startswith(module_name, arg) then
31 table.insert(matches, module_name)
32 end
33 end
34 end
35 elseif #words == 3 then
36 if words[2] == 'start' or words[2] == 'set_task_param' or words[2] == 'set_module_param' then
37 local ok, module = pcall(require, 'tasks.module.' .. words[3])
38 if ok then
39 for key, value in pairs(words[2] == 'set_module_param' and module.params or module.tasks) do
40 local name = type(key) == 'number' and value or key -- Handle arrays
41 if vim.startswith(name, arg) then
42 table.insert(matches, name)
43 end
44 end
45 end
46 end
47 elseif #words == 4 then
48 if words[2] == 'set_task_param' then
49 for _, param_name in ipairs(constants.task_params) do
50 if vim.startswith(param_name, arg) then
51 table.insert(matches, param_name)
52 end
53 end
54 end
55 end
56
57 return matches
58end
59
60--- Run specified subcommand received from completion.
61---@param subcommand table
62function subcommands.run(subcommand)
63 local subcommand_func = tasks[subcommand.fargs[1]]
64 if not subcommand_func then
65 utils.notify(string.format('No such subcommand named "%s"', subcommand.fargs[1]), vim.log.levels.ERROR)
66 return
67 end
68 local subcommand_info = debug.getinfo(subcommand_func)
69 if subcommand_info.isvararg and #subcommand.fargs - 1 < subcommand_info.nparams then
70 utils.notify(string.format('Subcommand %s should have at least %s argument(s)', subcommand.fargs[1], subcommand_info.nparams + 1), vim.log.levels.ERROR)
71 return
72 elseif not subcommand_info.isvararg and #subcommand.fargs - 1 ~= subcommand_info.nparams then
73 utils.notify(string.format('Subcommand %s should have %s argument(s)', subcommand.fargs[1], subcommand_info.nparams + 1), vim.log.levels.ERROR)
74 return
75 end
76 subcommand_func(unpack(subcommand.fargs, 2))
77end
78
79return subcommands