-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstackgen.lua
More file actions
93 lines (89 loc) · 2.85 KB
/
Copy pathstackgen.lua
File metadata and controls
93 lines (89 loc) · 2.85 KB
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
local config = require "stackgen.config"
local cmds = require "stackgen.commands"
---@class MySubCommand
---@field impl fun(args: string[], opts: table) The command implementation
---@field complete? fun(subcmd_arg_lead: string): string[] (optional)
---Command completions callback, taking the lead of the subcommand's arguments
---@type table<string, MySubCommand>
local subcommand_tbl = {
module_get = {
impl = function(args, _)
local id = args[1]
if not id or id == "" then
vim.notify("StackGen: Please provide a module ID.", vim.log.levels.ERROR)
return
end
cmds.get_module(id)
end,
},
module_list = {
impl = cmds.list_modules,
},
module_sync = {
impl = cmds.sync_module,
},
module_publish = {
impl = function(args, _)
local version_name = args[1]
if not version_name or version_name == "" then
vim.notify("StackGen: Please provide a version name.", vim.log.levels.ERROR)
return
end
cmds.publish_module(version_name)
end,
},
show_config = {
impl = cmds.show_config,
},
set_token = {
impl = function(args, _)
local token = args[1]
if not token or token == "" then
vim.notify("StackGen: Please provide a token.", vim.log.levels.ERROR)
return
end
config.set_token(token)
---@diagnostic disable-next-line: missing-fields
vim.notify("Stackgen token set successfully.", vim.log.levels.INFO, {
title = "Stackgen",
})
end,
},
}
---@param opts table :h lua-guide-commands-create
local function my_cmd(opts)
local fargs = opts.fargs
local subcommand_key = fargs[1]
-- Get the subcommand's arguments, if any
local args = #fargs > 1 and vim.list_slice(fargs, 2, #fargs) or {}
local subcommand = subcommand_tbl[subcommand_key]
if not subcommand then
vim.notify("StackGen: Unknown command: " .. subcommand_key, vim.log.levels.ERROR)
return
end
-- Invoke the subcommand
subcommand.impl(args, opts)
end
vim.api.nvim_create_user_command("StackGen", my_cmd, {
nargs = "+",
complete = function(arg_lead, cmdline, _)
-- Get the subcommand.
local subcmd_key, subcmd_arg_lead = cmdline:match "^['<,'>]*StackGen[!]*%s(%S+)%s(.*)$"
if subcmd_key and subcmd_arg_lead and subcommand_tbl[subcmd_key] and subcommand_tbl[subcmd_key].complete then
-- The subcommand has completions. Return them.
return subcommand_tbl[subcmd_key].complete(subcmd_arg_lead)
end
-- Check if cmdline is a subcommand
if cmdline:match "^['<,'>]*StackGen[!]*%s+%w*$" then
-- Filter subcommands that match
local subcommand_keys = vim.tbl_keys(subcommand_tbl)
return vim
.iter(subcommand_keys)
:filter(function(key)
return key:find(arg_lead) ~= nil
end)
:totable()
end
end,
bang = true, -- If you want to support ! modifiers
})