Repository navigation
Expand file tree
/
Copy pathparse.lua
More file actions
94 lines (82 loc) · 2.25 KB
/
Copy pathparse.lua
File metadata and controls
94 lines (82 loc) · 2.25 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
94
local M = {}
---@param opts OpencodeCommandRouteOpts
---@return OpencodeSelectionRange|nil
local function parse_range(opts)
if not (opts.range and opts.range > 0) then
return nil
end
return {
start = opts.line1,
stop = opts.line2,
}
end
---@param command_name string
---@param command_def OpencodeUICommand
---@param args string[]
---@return OpencodeCommandParseError|nil
local function validate_nested_subcommand(command_name, command_def, args)
local validation = command_def.nested_subcommand
if not validation then
return nil
end
local nested_subcommand = args[1]
if not nested_subcommand then
if validation.allow_empty then
return nil
end
return {
code = 'invalid_subcommand',
message = 'Invalid ' .. command_name .. ' subcommand. Use: ' .. table.concat(command_def.completions or {}, ', '),
subcommand = command_name,
}
end
if vim.tbl_contains(command_def.completions or {}, nested_subcommand) then
return nil
end
return {
code = 'invalid_subcommand',
message = 'Invalid ' .. command_name .. ' subcommand. Use: ' .. table.concat(command_def.completions or {}, ', '),
subcommand = command_name,
}
end
---@param opts OpencodeCommandRouteOpts
---@param commands table<string, OpencodeUICommand>
---@return OpencodeCommandParseResult
function M.command(opts, commands)
local raw_args = opts.args or ''
local argv = vim.split(raw_args, '%s+', { trimempty = true })
local subcommand = #argv == 0 and 'toggle' or argv[1]
local subcmd_def = commands[subcommand]
local range = parse_range(opts)
if not subcmd_def then
return {
ok = false,
error = {
code = 'unknown_subcommand',
message = 'Unknown subcommand: ' .. subcommand,
subcommand = subcommand,
},
}
end
local args = #argv == 0 and {} or vim.list_slice(argv, 2)
local nested_subcommand_error = validate_nested_subcommand(subcommand, subcmd_def, args)
if nested_subcommand_error then
return {
ok = false,
error = nested_subcommand_error,
}
end
return {
ok = true,
intent = {
name = subcommand,
args = args,
range = range,
source = {
raw_args = raw_args,
argv = argv,
},
},
}
end
return M