forked from zbirenbaum/copilot.lua
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand.lua
More file actions
192 lines (156 loc) · 4.65 KB
/
command.lua
File metadata and controls
192 lines (156 loc) · 4.65 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
local a = require("copilot.api")
local c = require("copilot.client")
local config = require("copilot.config")
local u = require("copilot.util")
local mod = {}
local function node_version_warning(node_version)
if string.match(node_version, "^16%.") then
local line = "Warning: Node.js 16 is approaching end of life and support will be dropped in a future release."
if config.get("copilot_node_command") ~= "node" then
line = line
.. " 'copilot_node_command' is set to a non-default value. Consider removing it from your configuration."
end
return { line, "MoreMsg" }
end
end
function mod.version()
local info = u.get_editor_info()
---@type (string|table)[]
local lines = {
info.editorInfo.name .. " " .. info.editorInfo.version,
"copilot.vim" .. " " .. info.editorPluginInfo.version,
"copilot.lua" .. " " .. u.get_copilot_lua_version(),
}
local client = c.get()
coroutine.wrap(function()
if client then
local _, data = a.get_version(client)
lines[#lines + 1] = "copilot/dist/language-server.js" .. " " .. data.version
else
lines[#lines + 1] = "copilot/dist/language-server.js" .. " " .. "not running"
end
local node_version, node_version_error = c.get_node_version()
lines[#lines + 1] = "Node.js" .. " " .. (#node_version == 0 and "(unknown)" or node_version)
if node_version_error then
lines[#lines + 1] = { node_version_error, "WarningMsg" }
end
lines[#lines + 1] = node_version_warning(node_version)
local chunks = {}
for _, line in ipairs(lines) do
chunks[#chunks + 1] = type(line) == "table" and line or { line }
chunks[#chunks + 1] = { "\n", "NONE" }
end
vim.api.nvim_echo(chunks, true, {})
end)()
end
function mod.status()
local lines = {}
local function add_line(line)
if not line then
return
end
lines[#lines + 1] = type(line) == "table" and { "[Copilot] " .. line[1], line[2] } or { "[Copilot] " .. line }
lines[#lines + 1] = { "\n", "NONE" }
end
local function flush_lines(last_line, is_off)
add_line(last_line)
if c.startup_error then
add_line({ c.startup_error, "WarningMsg" })
end
local node_version, node_version_error = c.get_node_version()
if node_version_error then
add_line({ node_version_error, "WarningMsg" })
end
if not is_off then
add_line(node_version_warning(node_version))
end
vim.api.nvim_echo(lines, true, {})
end
if c.is_disabled() then
flush_lines("Offline", true)
return
end
local client = c.get()
if not client then
flush_lines("Not Started", true)
return
end
add_line("Online")
coroutine.wrap(function()
local cserr, status = a.check_status(client)
if cserr then
flush_lines(cserr)
return
end
if not status.user then
flush_lines("Not authenticated. Run ':Copilot auth'")
return
elseif status.status == "NoTelemetryConsent" then
flush_lines("Telemetry terms not accepted")
return
elseif status.status == "NotAuthorized" then
flush_lines("Not authorized")
return
end
local should_attach, no_attach_reason = u.should_attach()
local is_attached = c.buf_is_attached()
if is_attached then
if not should_attach then
add_line("Enabled manually (" .. no_attach_reason .. ")")
else
add_line("Enabled for " .. vim.bo.filetype)
end
elseif not is_attached then
if should_attach then
add_line("Disabled manually for " .. vim.bo.filetype)
else
add_line("Disabled (" .. no_attach_reason .. ")")
end
end
if string.lower(a.status.data.status) == "error" then
add_line(a.status.data.message)
end
flush_lines()
end)()
end
---@param opts? { force?: boolean }
function mod.attach(opts)
opts = opts or {}
if not opts.force then
local should_attach, no_attach_reason = u.should_attach()
if not should_attach then
vim.api.nvim_echo({
{ "[Copilot] " .. no_attach_reason .. "\n" },
{ "[Copilot] to force attach, run ':Copilot! attach'" },
}, true, {})
return
end
opts.force = true
end
c.buf_attach(opts.force)
end
function mod.detach()
if c.buf_is_attached(0) then
c.buf_detach()
end
end
---@param opts? { force?: boolean }
function mod.toggle(opts)
opts = opts or {}
if c.buf_is_attached(0) then
mod.detach()
return
end
mod.attach(opts)
end
function mod.enable()
c.setup()
require("copilot.panel").setup()
require("copilot.suggestion").setup()
end
function mod.disable()
c.teardown()
require("copilot.panel").teardown()
require("copilot.suggestion").teardown()
end
return mod