1
1
local logger = require (" copilot.logger" )
2
- local mod = {}
2
+ --- @class CopilotApi
3
+ local M = {}
3
4
4
5
--- @param callback ? fun ( err : any | nil , data : table , ctx : table ): nil
5
6
--- @return any | nil err
6
7
--- @return any data
7
8
--- @return table ctx
8
- function mod .request (client , method , params , callback )
9
+ function M .request (client , method , params , callback )
9
10
logger .trace (" api request:" , method , params )
10
11
-- hack to convert empty table to json object,
11
12
-- empty table is convert to json array by default.
@@ -26,7 +27,7 @@ function mod.request(client, method, params, callback)
26
27
end
27
28
28
29
--- @return boolean sent
29
- function mod .notify (client , method , params )
30
+ function M .notify (client , method , params )
30
31
logger .trace (" api notify:" , method , params )
31
32
32
33
if vim .fn .has (" nvim-0.11" ) == 1 then
50
51
--- @alias copilot_workspace_configurations { settings : copilot_workspace_configuration }
51
52
52
53
--- @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 )
55
56
end
56
57
57
58
--- @alias copilot_nofify_set_trace_params { value : ' off' | ' messages' | ' verbose' }
58
59
59
60
--- @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 )
62
63
end
63
64
64
65
--- @alias copilot_check_status_params { options ?: { localChecksOnly ?: boolean } }
68
69
--- @return any | nil err
69
70
--- @return copilot_check_status_data data
70
71
--- @return table ctx
71
- function mod .check_status (client , params , callback )
72
+ function M .check_status (client , params , callback )
72
73
if type (params ) == " function" then
73
74
callback = params
74
75
params = {}
75
76
end
76
- return mod .request (client , " checkStatus" , params or {}, callback )
77
+ return M .request (client , " checkStatus" , params or {}, callback )
77
78
end
78
79
79
80
--- @alias copilot_sign_in_initiate_data { verificationUri ?: string , userCode ?: string }
80
81
81
82
--- @return any | nil err
82
83
--- @return copilot_sign_in_initiate_data data
83
84
--- @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 )
86
87
end
87
88
88
89
--- @alias copilot_sign_in_confirm_params { userId : string }
92
93
--- @return any | nil err
93
94
--- @return copilot_sign_in_confirm_data data
94
95
--- @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 )
97
98
end
98
99
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 )
101
102
end
102
103
103
104
--- @alias copilot_get_version_data { version : string }
104
105
105
106
--- @return any | nil err
106
107
--- @return copilot_get_version_data data
107
108
--- @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 )
110
111
end
111
112
112
113
--- @alias copilot_notify_accepted_params { uuid : string , acceptedLength ?: integer }
113
114
114
115
--- @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 )
117
118
end
118
119
119
120
--- @alias copilot_notify_rejected_params { uuids : string[] }
120
121
121
122
--- @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 )
124
125
end
125
126
126
127
--- @alias copilot_notify_shown_params { uuid : string }
127
128
128
129
--- @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 )
131
132
end
132
133
133
134
--- @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
136
137
--- @return any | nil err
137
138
--- @return copilot_get_completions_data data
138
139
--- @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 )
141
142
end
142
143
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 )
145
146
end
146
147
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
-
152
148
--- @return any | nil err
153
149
--- @return integer data
154
150
--- @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 )
231
153
end
232
154
233
155
--- @alias copilot_window_show_document { uri : string , external ?: boolean , takeFocus ?: boolean , selection ?: boolean }
234
156
--- @alias copilot_window_show_document_result { success : boolean }
235
157
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