1
1
local api = require (" copilot.api" )
2
2
local config = require (" copilot.config" )
3
3
local util = require (" copilot.util" )
4
+ local logger = require (" copilot.logger" )
4
5
5
6
local is_disabled = false
6
7
@@ -9,6 +10,7 @@ local M = {
9
10
id = nil ,
10
11
--- @class copilot_capabilities : lsp.ClientCapabilities
11
12
--- @field copilot table< ' openURL' , boolean>
13
+ --- @field workspace table< ' workspaceFolders' , boolean>
12
14
capabilities = nil ,
13
15
config = nil ,
14
16
node_version = nil ,
@@ -21,7 +23,8 @@ local M = {
21
23
local function store_client_id (id )
22
24
if M .id and M .id ~= id then
23
25
if vim .lsp .get_client_by_id (M .id ) then
24
- error (" unexpectedly started multiple copilot servers" )
26
+ logger .error (" unexpectedly started multiple copilot servers" )
27
+ return
25
28
end
26
29
end
27
30
92
95
--- @param force ? boolean
93
96
function M .buf_attach (force )
94
97
if is_disabled then
95
- print ( " [Copilot] Offline " )
98
+ logger . warn ( " copilot is disabled " )
96
99
return
97
100
end
98
101
@@ -101,7 +104,7 @@ function M.buf_attach(force)
101
104
end
102
105
103
106
if not M .config then
104
- vim . notify ( " [Copilot] Cannot attach: configuration not initialized" , vim . log . levels . ERROR )
107
+ logger . error ( " cannot attach: configuration not initialized" )
105
108
return
106
109
end
107
110
@@ -110,14 +113,14 @@ function M.buf_attach(force)
110
113
111
114
local ok , client_id_or_err = pcall (lsp_start , M .config )
112
115
if not ok then
113
- vim . notify (string.format (" [Copilot] Failed to start LSP client: %s" , client_id_or_err ), vim . log . levels . ERROR )
116
+ logger . error (string.format (" failed to start LSP client: %s" , client_id_or_err ))
114
117
return
115
118
end
116
119
117
120
if client_id_or_err then
118
121
store_client_id (client_id_or_err )
119
122
else
120
- vim . notify ( " [Copilot] LSP client failed to start (no client ID returned)" , vim . log . levels . ERROR )
123
+ logger . error ( " LSP client failed to start (no client ID returned)" )
121
124
end
122
125
end
123
126
@@ -138,21 +141,22 @@ end
138
141
--- @param callback fun ( client : table ): nil
139
142
function M .use_client (callback )
140
143
if is_disabled then
141
- print ( " [Copilot] Offline " )
144
+ logger . warn ( " copilot is offline " )
142
145
return
143
146
end
144
147
145
148
local client = M .get () --[[ @as table]]
146
149
147
150
if not client then
148
151
if not M .config then
149
- error (" copilot.setup is not called yet" )
152
+ logger .error (" copilot.setup is not called yet" )
153
+ return
150
154
end
151
155
152
156
local client_id , err = vim .lsp .start_client (M .config )
153
157
154
158
if not client_id then
155
- error (string.format (" [Copilot] Error starting LSP Client : %s" , err ))
159
+ logger . error (string.format (" error starting LSP client : %s" , err ))
156
160
return
157
161
end
158
162
@@ -169,7 +173,7 @@ function M.use_client(callback)
169
173
local timer , err , _ = vim .loop .new_timer ()
170
174
171
175
if not timer then
172
- error (string.format (" [Copilot] Error creating timer: %s" , err ))
176
+ logger . error (string.format (" error creating timer: %s" , err ))
173
177
return
174
178
end
175
179
@@ -191,15 +195,15 @@ local function prepare_client_config(overrides)
191
195
192
196
if vim .fn .executable (node ) ~= 1 then
193
197
local err = string.format (" copilot_node_command(%s) is not executable" , node )
194
- vim . notify ( " [Copilot] " .. err , vim . log . levels . ERROR )
198
+ logger . error ( err )
195
199
M .startup_error = err
196
200
return
197
201
end
198
202
199
203
local agent_path = vim .api .nvim_get_runtime_file (" copilot/dist/language-server.js" , false )[1 ]
200
204
if not agent_path or vim .fn .filereadable (agent_path ) == 0 then
201
205
local err = string.format (" Could not find language-server.js (bad install?) : %s" , tostring (agent_path ))
202
- vim . notify ( " [Copilot] " .. err , vim . log . levels . ERROR )
206
+ logger . error ( err )
203
207
M .startup_error = err
204
208
return
205
209
end
@@ -272,11 +276,14 @@ local function prepare_client_config(overrides)
272
276
set_editor_info_params .authProvider = provider_url and {
273
277
url = provider_url ,
274
278
} or nil
279
+
280
+ logger .debug (" data for setEditorInfo LSP call" , set_editor_info_params )
275
281
api .set_editor_info (client , set_editor_info_params , function (err )
276
282
if err then
277
- vim . notify (string.format (" [copilot] setEditorInfo failure: %s" , err ), vim . log . levels . ERROR )
283
+ logger . error (string.format (" setEditorInfo failure: %s" , err ))
278
284
end
279
285
end )
286
+ logger .trace (" setEditorInfo has been called" )
280
287
M .initialized = true
281
288
end )
282
289
end ,
@@ -299,6 +306,7 @@ local function prepare_client_config(overrides)
299
306
copilotIntegrationId = " vscode-chat" ,
300
307
},
301
308
workspace_folders = workspace_folders ,
309
+ trace = config .get (" trace" ) or " off" ,
302
310
}, overrides )
303
311
end
304
312
@@ -339,12 +347,12 @@ end
339
347
340
348
function M .add_workspace_folder (folder_path )
341
349
if type (folder_path ) ~= " string" then
342
- vim . notify ( " [Copilot] Workspace folder path must be a string" , vim . log . levels . ERROR )
350
+ logger . error ( " workspace folder path must be a string" )
343
351
return false
344
352
end
345
353
346
354
if vim .fn .isdirectory (folder_path ) ~= 1 then
347
- vim . notify ( " [Copilot] Invalid workspace folder: " .. folder_path , vim . log . levels . ERROR )
355
+ logger . error ( " invalid workspace folder: " .. folder_path )
348
356
return false
349
357
end
350
358
@@ -379,9 +387,9 @@ function M.add_workspace_folder(folder_path)
379
387
removed = {},
380
388
},
381
389
})
382
- vim .notify (" [Copilot] Added workspace folder: " .. folder_path , vim . log . levels . INFO )
390
+ logger .notify (" added workspace folder: " .. folder_path )
383
391
else
384
- vim .notify (" [Copilot] Workspace folder added for next session: " .. folder_path , vim . log . levels . INFO )
392
+ logger .notify (" workspace folder will be added on next session: " .. folder_path )
385
393
end
386
394
387
395
return true
0 commit comments