forked from zbirenbaum/copilot.lua
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.lua
More file actions
112 lines (92 loc) · 2.41 KB
/
init.lua
File metadata and controls
112 lines (92 loc) · 2.41 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
local u = require("copilot.util")
local logger = require("copilot.logger")
---@alias copilot_status_notification_data { status: ''|'Normal'|'InProgress'|'Warning', message: string }
local M = {
client_id = nil,
---@type copilot_status_notification_data
data = {
status = "",
message = "",
},
callback = {},
handlers = {},
}
M.handlers = {
---@param result copilot_status_notification_data
---@param ctx { client_id: integer, method: string }
statusNotification = function(_, result, ctx)
M.client_id = ctx.client_id
M.data = result
for callback in pairs(M.callback) do
callback(M.data)
end
end,
}
---@param handler fun(data: copilot_status_notification_data): nil
function M.register_status_notification_handler(handler)
M.callback[handler] = true
handler(M.data)
end
---@param handler fun(data: copilot_status_notification_data): nil
function M.unregister_status_notification_handler(handler)
M.callback[handler] = nil
end
function M.status()
local c = require("copilot.client")
local a = require("copilot.api")
logger.trace("Status called")
local lines = "Status:"
---@param line string|nil
local function add_line(line)
if not line then
return
end
if lines ~= "" then
lines = lines .. "\n" .. line
else
lines = line
end
end
---@param last_line string|nil
local function flush_lines(last_line)
add_line(last_line)
if c.startup_error then
add_line(c.startup_error)
end
logger.notify(lines)
end
if c.is_disabled() then
flush_lines("Offline")
return
end
local client = c.get()
if not client then
flush_lines("Not Started")
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 buffer_status = u.get_buffer_attach_status(vim.api.nvim_get_current_buf())
add_line("Buffer status: " .. buffer_status)
if string.lower(M.data.status) == "error" then
add_line(M.data.message)
end
flush_lines()
end)()
end
return M