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
211 lines (175 loc) · 5.04 KB
/
init.lua
File metadata and controls
211 lines (175 loc) · 5.04 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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
local uv = vim.uv
---@class logger
local M = {
log_file = vim.fn.stdpath("log") .. "/copilot-lua.log",
file_log_level = vim.log.levels.OFF,
print_log_level = vim.log.levels.WARN,
logger_id = 0,
}
local log_level_names = {
[vim.log.levels.ERROR] = "ERROR", --4
[vim.log.levels.WARN] = "WARN", --3
[vim.log.levels.INFO] = "INFO", --2
[vim.log.levels.DEBUG] = "DEBUG", --1
[vim.log.levels.TRACE] = "TRACE", --0
}
---@return string timestamp
local function get_timestamp_with_ms()
local seconds = os.time()
local milliseconds = math.floor((os.clock() % 1) * 1000)
return string.format("%s.%03d", os.date("%Y-%m-%d %H:%M:%S", seconds), milliseconds)
end
---@param log_level integer --vim.log.levels
---@param msg string
---@param ... any
---@return string log_msg
local function format_log(log_level, msg, ...)
local log_level_name = log_level_names[log_level]
M.logger_id = M.logger_id + 1
if M.logger_id > 1000000 then
M.logger_id = 1
end
-- we add an id as this process is asynchronous and the logs end up in a different order
local log_msg = string.format("%s [%d] [%s]: %s", get_timestamp_with_ms(), M.logger_id, log_level_name, msg)
local args = { ... }
for _, v in ipairs(args) do
log_msg = string.format("%s\n%s", log_msg, vim.inspect(v))
end
return log_msg
end
---@param msg string
---@param ... any
---@return string log_msg
local function format_notify(msg, ...)
-- we add an id as this process is asynchronous and the logs end up in a different order
local log_msg = string.format("[Copilot.lua] %s", msg)
local args = { ... }
for _, v in ipairs(args) do
log_msg = string.format("%s\n%s", log_msg, vim.inspect(v))
end
return log_msg
end
---@param log_level integer -- one of the vim.log.levels
---@param msg string
---@param ... any
local function notify_log(log_level, msg, ...)
local log_msg = format_notify(msg, ...)
if vim.in_fast_event then
vim.schedule(function()
vim.notify(log_msg, log_level)
end)
else
vim.notify(log_msg, log_level)
end
end
---@param log_level integer -- one of the vim.log.levels
---@param log_file string
---@param msg string
---@param ... any
local function write_log(log_level, log_file, msg, ...)
local log_msg = format_log(log_level, msg, ...) .. "\n"
uv.fs_open(log_file, "a", tonumber("644", 8), function(err, fd)
if err or not fd then
notify_log(vim.log.levels.ERROR, "Failed to open log file: " .. err)
return
end
uv.fs_write(fd, log_msg, -1, function(write_err)
if write_err then
notify_log(vim.log.levels.ERROR, "Failed to write to log file: " .. write_err)
end
uv.fs_close(fd)
end)
end)
end
---@param log_level integer -- one of the vim.log.levels
---@param msg string
---@param ... any
function M.log(log_level, msg, ...)
if M.file_log_level <= log_level then
write_log(log_level, M.log_file, msg, ...)
end
if M.print_log_level <= log_level then
notify_log(log_level, msg, ...)
end
end
---@param msg string
---@param ... any
function M.debug(msg, ...)
M.log(vim.log.levels.DEBUG, msg, ...)
end
---@param msg string
---@param ... any
function M.trace(msg, ...)
M.log(vim.log.levels.TRACE, msg, ...)
end
---@param msg string
---@param ... any
function M.error(msg, ...)
M.log(vim.log.levels.ERROR, msg, ...)
end
---@param msg string
---@param ... any
function M.warn(msg, ...)
M.log(vim.log.levels.WARN, msg, ...)
end
---@param msg string
---@param ... any
function M.info(msg, ...)
M.log(vim.log.levels.INFO, msg, ...)
end
---@param msg string
---@param ... any
function M.notify(msg, ...)
notify_log(vim.log.levels.INFO, msg, ...)
end
---@param conf LoggerConfig
function M.setup(conf)
M.log_file = conf.file
M.file_log_level = conf.file_log_level
M.print_log_level = conf.print_log_level
end
function M.handle_lsp_trace(_, result, _)
if not result then
return
end
M.trace(string.format("LSP trace - %s", result.message), result.verbose)
end
function M.handle_lsp_progress(_, result, _)
if not result then
return
end
M.trace(string.format("LSP progress - token %s", result.token), result.value)
end
-- Known noisy errors that we do not want to show as they seem to be out of our control
---@param msg string
---@return boolean
local function force_log_to_trace(msg)
if
msg:match(".*Request textDocument/copilotInlineEdit: AbortError: The operation was aborted.*")
or msg:match(".*AsyncCompletionManager.*Request errored with AbortError: The operation was aborted.*")
then
return true
end
return false
end
function M.handle_log_lsp_messages(_, result, _)
if not result then
return
end
local message = string.format("LSP message: %s", result.message)
local message_type = result.type --[[@as integer]]
if message_type == 1 and not force_log_to_trace(message) then
M.error(message)
elseif message_type == 2 then
M.warn(message)
elseif message_type == 3 then
M.info(message)
elseif message_type == 4 then
M.info(message)
elseif message_type == 5 then
M.debug(message)
else
M.trace(message)
end
end
return M