11local logger = require (" copilot.logger" )
2- local mod = {}
2+ --- @class CopilotApi
3+ local M = {}
34
45--- @param callback ? fun ( err : any | nil , data : table , ctx : table ): nil
56--- @return any | nil err
67--- @return any data
78--- @return table ctx
8- function mod .request (client , method , params , callback )
9+ function M .request (client , method , params , callback )
910 logger .trace (" api request:" , method , params )
1011 -- hack to convert empty table to json object,
1112 -- empty table is convert to json array by default.
@@ -26,7 +27,7 @@ function mod.request(client, method, params, callback)
2627end
2728
2829--- @return boolean sent
29- function mod .notify (client , method , params )
30+ function M .notify (client , method , params )
3031 logger .trace (" api notify:" , method , params )
3132
3233 if vim .fn .has (" nvim-0.11" ) == 1 then
5051--- @alias copilot_workspace_configurations { settings : copilot_workspace_configuration }
5152
5253--- @param params copilot_workspace_configurations
53- function mod .notify_change_configuration (client , params )
54- return mod .notify (client , " workspace/didChangeConfiguration" , params )
54+ function M .notify_change_configuration (client , params )
55+ return M .notify (client , " workspace/didChangeConfiguration" , params )
5556end
5657
5758--- @alias copilot_nofify_set_trace_params { value : ' off' | ' messages' | ' verbose' }
5859
5960--- @param params copilot_nofify_set_trace_params
60- function mod .notify_set_trace (client , params )
61- return mod .notify (client , " $/setTrace" , params )
61+ function M .notify_set_trace (client , params )
62+ return M .notify (client , " $/setTrace" , params )
6263end
6364
6465--- @alias copilot_check_status_params { options ?: { localChecksOnly ?: boolean } }
6869--- @return any | nil err
6970--- @return copilot_check_status_data data
7071--- @return table ctx
71- function mod .check_status (client , params , callback )
72+ function M .check_status (client , params , callback )
7273 if type (params ) == " function" then
7374 callback = params
7475 params = {}
7576 end
76- return mod .request (client , " checkStatus" , params or {}, callback )
77+ return M .request (client , " checkStatus" , params or {}, callback )
7778end
7879
7980--- @alias copilot_sign_in_initiate_data { verificationUri ?: string , userCode ?: string }
8081
8182--- @return any | nil err
8283--- @return copilot_sign_in_initiate_data data
8384--- @return table ctx
84- function mod .sign_in_initiate (client , callback )
85- return mod .request (client , " signInInitiate" , {}, callback )
85+ function M .sign_in_initiate (client , callback )
86+ return M .request (client , " signInInitiate" , {}, callback )
8687end
8788
8889--- @alias copilot_sign_in_confirm_params { userId : string }
9293--- @return any | nil err
9394--- @return copilot_sign_in_confirm_data data
9495--- @return table ctx
95- function mod .sign_in_confirm (client , params , callback )
96- return mod .request (client , " signInConfirm" , params , callback )
96+ function M .sign_in_confirm (client , params , callback )
97+ return M .request (client , " signInConfirm" , params , callback )
9798end
9899
99- function mod .sign_out (client , callback )
100- return mod .request (client , " signOut" , {}, callback )
100+ function M .sign_out (client , callback )
101+ return M .request (client , " signOut" , {}, callback )
101102end
102103
103104--- @alias copilot_get_version_data { version : string }
104105
105106--- @return any | nil err
106107--- @return copilot_get_version_data data
107108--- @return table ctx
108- function mod .get_version (client , callback )
109- return mod .request (client , " getVersion" , {}, callback )
109+ function M .get_version (client , callback )
110+ return M .request (client , " getVersion" , {}, callback )
110111end
111112
112113--- @alias copilot_notify_accepted_params { uuid : string , acceptedLength ?: integer }
113114
114115--- @param params copilot_notify_accepted_params
115- function mod .notify_accepted (client , params , callback )
116- return mod .request (client , " notifyAccepted" , params , callback )
116+ function M .notify_accepted (client , params , callback )
117+ return M .request (client , " notifyAccepted" , params , callback )
117118end
118119
119120--- @alias copilot_notify_rejected_params { uuids : string[] }
120121
121122--- @param params copilot_notify_rejected_params
122- function mod .notify_rejected (client , params , callback )
123- return mod .request (client , " notifyRejected" , params , callback )
123+ function M .notify_rejected (client , params , callback )
124+ return M .request (client , " notifyRejected" , params , callback )
124125end
125126
126127--- @alias copilot_notify_shown_params { uuid : string }
127128
128129--- @param params copilot_notify_shown_params
129- function mod .notify_shown (client , params , callback )
130- return mod .request (client , " notifyShown" , params , callback )
130+ function M .notify_shown (client , params , callback )
131+ return M .request (client , " notifyShown" , params , callback )
131132end
132133
133134--- @alias copilot_get_completions_data_completion { displayText : string , position : { character : integer , line : integer }, range : { [ ' end' ] : { character : integer , line : integer }, start : { character : integer , line : integer } }, text : string , uuid : string }
@@ -136,110 +137,22 @@ end
136137--- @return any | nil err
137138--- @return copilot_get_completions_data data
138139--- @return table ctx
139- function mod .get_completions (client , params , callback )
140- return mod .request (client , " getCompletions" , params , callback )
140+ function M .get_completions (client , params , callback )
141+ return M .request (client , " getCompletions" , params , callback )
141142end
142143
143- function mod .get_completions_cycling (client , params , callback )
144- return mod .request (client , " getCompletionsCycling" , params , callback )
144+ function M .get_completions_cycling (client , params , callback )
145+ return M .request (client , " getCompletionsCycling" , params , callback )
145146end
146147
147- --- @alias copilot_panel_solution_data { panelId : string , completionText : string , displayText : string , range : { [ ' end' ] : { character : integer , line : integer }, start : { character : integer , line : integer } }, score : number , solutionId : string }
148- --- @alias copilot_panel_on_solution_handler fun ( result : copilot_panel_solution_data ): nil
149- --- @alias copilot_panel_solutions_done_data { panelId : string , status : ' OK' | ' Error' , message ?: string }
150- --- @alias copilot_panel_on_solutions_done_handler fun ( result : copilot_panel_solutions_done_data ): nil
151-
152148--- @return any | nil err
153149--- @return integer data
154150--- @return table ctx
155- function mod .get_panel_completions (client , params , callback )
156- return mod .request (client , " getPanelCompletions" , params , callback )
157- end
158-
159- local panel = {
160- callback = {
161- PanelSolution = {},
162- PanelSolutionsDone = {},
163- },
164- }
165-
166- panel .handlers = {
167- --- @param result copilot_panel_solution_data
168- PanelSolution = function (_ , result )
169- if panel .callback .PanelSolution [result .panelId ] then
170- panel .callback .PanelSolution [result .panelId ](result )
171- end
172- end ,
173-
174- --- @param result copilot_panel_solutions_done_data
175- PanelSolutionsDone = function (_ , result )
176- if panel .callback .PanelSolutionsDone [result .panelId ] then
177- panel .callback .PanelSolutionsDone [result .panelId ](result )
178- end
179- end ,
180- }
181-
182- --- @param panelId string
183- --- @param handlers { on_solution : copilot_panel_on_solution_handler , on_solutions_done : copilot_panel_on_solutions_done_handler }
184- function mod .register_panel_handlers (panelId , handlers )
185- assert (type (panelId ) == " string" , " missing panelId" )
186- panel .callback .PanelSolution [panelId ] = handlers .on_solution
187- panel .callback .PanelSolutionsDone [panelId ] = handlers .on_solutions_done
188- end
189-
190- --- @param panelId string
191- function mod .unregister_panel_handlers (panelId )
192- assert (type (panelId ) == " string" , " missing panelId" )
193- panel .callback .PanelSolution [panelId ] = nil
194- panel .callback .PanelSolutionsDone [panelId ] = nil
195- end
196-
197- --- @alias copilot_status_notification_data { status : ' ' | ' Normal' | ' InProgress' | ' Warning' , message : string }
198-
199- local status = {
200- client_id = nil ,
201- --- @type copilot_status_notification_data
202- data = {
203- status = " " ,
204- message = " " ,
205- },
206- callback = {},
207- }
208-
209- status .handlers = {
210- --- @param result copilot_status_notification_data
211- --- @param ctx { client_id : integer , method : string }
212- statusNotification = function (_ , result , ctx )
213- status .client_id = ctx .client_id
214- status .data = result
215-
216- for callback in pairs (status .callback ) do
217- callback (status .data )
218- end
219- end ,
220- }
221-
222- --- @param handler fun ( data : copilot_status_notification_data ): nil
223- function mod .register_status_notification_handler (handler )
224- status .callback [handler ] = true
225- handler (status .data )
226- end
227-
228- --- @param handler fun ( data : copilot_status_notification_data ): nil
229- function mod .unregister_status_notification_handler (handler )
230- status .callback [handler ] = nil
151+ function M .get_panel_completions (client , params , callback )
152+ return M .request (client , " getPanelCompletions" , params , callback )
231153end
232154
233155--- @alias copilot_window_show_document { uri : string , external ?: boolean , takeFocus ?: boolean , selection ?: boolean }
234156--- @alias copilot_window_show_document_result { success : boolean }
235157
236- mod .handlers = {
237- PanelSolution = panel .handlers .PanelSolution ,
238- PanelSolutionsDone = panel .handlers .PanelSolutionsDone ,
239- statusNotification = status .handlers .statusNotification ,
240- }
241-
242- mod .panel = panel
243- mod .status = status
244-
245- return mod
158+ return M
0 commit comments