11local api = require (" copilot.api" )
22local config = require (" copilot.config" )
33local util = require (" copilot.util" )
4+ local logger = require (" copilot.logger" )
45
56local is_disabled = false
67
@@ -9,6 +10,7 @@ local M = {
910 id = nil ,
1011 --- @class copilot_capabilities : lsp.ClientCapabilities
1112 --- @field copilot table< ' openURL' , boolean>
13+ --- @field workspace table< ' workspaceFolders' , boolean>
1214 capabilities = nil ,
1315 config = nil ,
1416 node_version = nil ,
@@ -21,7 +23,8 @@ local M = {
2123local function store_client_id (id )
2224 if M .id and M .id ~= id then
2325 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
2528 end
2629 end
2730
9295--- @param force ? boolean
9396function M .buf_attach (force )
9497 if is_disabled then
95- print ( " [Copilot] Offline " )
98+ logger . warn ( " copilot is disabled " )
9699 return
97100 end
98101
@@ -101,7 +104,7 @@ function M.buf_attach(force)
101104 end
102105
103106 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" )
105108 return
106109 end
107110
@@ -110,14 +113,14 @@ function M.buf_attach(force)
110113
111114 local ok , client_id_or_err = pcall (lsp_start , M .config )
112115 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 ))
114117 return
115118 end
116119
117120 if client_id_or_err then
118121 store_client_id (client_id_or_err )
119122 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)" )
121124 end
122125end
123126
@@ -138,21 +141,22 @@ end
138141--- @param callback fun ( client : table ): nil
139142function M .use_client (callback )
140143 if is_disabled then
141- print ( " [Copilot] Offline " )
144+ logger . warn ( " copilot is offline " )
142145 return
143146 end
144147
145148 local client = M .get () --[[ @as table]]
146149
147150 if not client then
148151 if not M .config then
149- error (" copilot.setup is not called yet" )
152+ logger .error (" copilot.setup is not called yet" )
153+ return
150154 end
151155
152156 local client_id , err = vim .lsp .start_client (M .config )
153157
154158 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 ))
156160 return
157161 end
158162
@@ -169,7 +173,7 @@ function M.use_client(callback)
169173 local timer , err , _ = vim .loop .new_timer ()
170174
171175 if not timer then
172- error (string.format (" [Copilot] Error creating timer: %s" , err ))
176+ logger . error (string.format (" error creating timer: %s" , err ))
173177 return
174178 end
175179
@@ -191,15 +195,15 @@ local function prepare_client_config(overrides)
191195
192196 if vim .fn .executable (node ) ~= 1 then
193197 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 )
195199 M .startup_error = err
196200 return
197201 end
198202
199203 local agent_path = vim .api .nvim_get_runtime_file (" copilot/dist/language-server.js" , false )[1 ]
200204 if not agent_path or vim .fn .filereadable (agent_path ) == 0 then
201205 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 )
203207 M .startup_error = err
204208 return
205209 end
@@ -272,11 +276,14 @@ local function prepare_client_config(overrides)
272276 set_editor_info_params .authProvider = provider_url and {
273277 url = provider_url ,
274278 } or nil
279+
280+ logger .debug (" data for setEditorInfo LSP call" , set_editor_info_params )
275281 api .set_editor_info (client , set_editor_info_params , function (err )
276282 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 ))
278284 end
279285 end )
286+ logger .trace (" setEditorInfo has been called" )
280287 M .initialized = true
281288 end )
282289 end ,
@@ -299,6 +306,7 @@ local function prepare_client_config(overrides)
299306 copilotIntegrationId = " vscode-chat" ,
300307 },
301308 workspace_folders = workspace_folders ,
309+ trace = config .get (" trace" ) or " off" ,
302310 }, overrides )
303311end
304312
@@ -339,12 +347,12 @@ end
339347
340348function M .add_workspace_folder (folder_path )
341349 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" )
343351 return false
344352 end
345353
346354 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 )
348356 return false
349357 end
350358
@@ -379,9 +387,9 @@ function M.add_workspace_folder(folder_path)
379387 removed = {},
380388 },
381389 })
382- vim .notify (" [Copilot] Added workspace folder: " .. folder_path , vim . log . levels . INFO )
390+ logger .notify (" added workspace folder: " .. folder_path )
383391 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 )
385393 end
386394
387395 return true
0 commit comments