forked from zbirenbaum/copilot.lua
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcopilot.lua
More file actions
83 lines (73 loc) · 2.25 KB
/
copilot.lua
File metadata and controls
83 lines (73 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
local completion_store = {
[""] = { "auth", "attach", "detach", "disable", "enable", "panel", "status", "suggestion", "toggle", "version" },
auth = { "signin", "signout", "info" },
panel = { "accept", "jump_next", "jump_prev", "open", "refresh" },
suggestion = { "accept", "accept_line", "accept_word", "dismiss", "next", "prev", "toggle_auto_trigger" },
workspace = { "add" },
}
vim.api.nvim_create_user_command("Copilot", function(opts)
local params = vim.split(opts.args, "%s+", { trimempty = true })
local mod_name, action_name = params[1], params[2]
if not mod_name then
mod_name = "status"
end
local ok, mod = pcall(require, "copilot." .. mod_name)
if not ok then
action_name = mod_name
mod_name = "command"
mod = require("copilot.command")
end
if not action_name then
if mod_name == "auth" then
action_name = "signin"
elseif mod_name == "panel" then
action_name = "open"
elseif mod_name == "suggestion" then
action_name = "toggle_auto_trigger"
elseif mod_name == "status" then
action_name = "status"
end
end
if not mod[action_name] then
return
end
if mod_name == "command" then
mod[action_name]({
force = opts.bang,
})
return
end
local remaining_args = ""
if #params > 2 then
remaining_args = table.concat(params, " ", 3)
end
require("copilot.client").use_client(function()
mod[action_name]({
force = opts.bang,
args = remaining_args,
})
end)
end, {
bang = true,
nargs = "?",
complete = function(_, cmd_line)
local has_space = string.match(cmd_line, "%s$")
local params = vim.split(cmd_line, "%s+", { trimempty = true })
if #params == 1 then
return completion_store[""]
elseif #params == 2 and not has_space then
return vim.tbl_filter(function(cmd)
return not not string.find(cmd, "^" .. params[2])
end, completion_store[""])
end
if #params >= 2 and completion_store[params[2]] then
if #params == 2 then
return completion_store[params[2]]
elseif #params == 3 and not has_space then
return vim.tbl_filter(function(cmd)
return not not string.find(cmd, "^" .. params[3])
end, completion_store[params[2]])
end
end
end,
})