forked from github/copilot.vim
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathservice.py
More file actions
344 lines (328 loc) · 12.5 KB
/
Copy pathservice.py
File metadata and controls
344 lines (328 loc) · 12.5 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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
import subprocess
from typing import Callable
import semver
import os
import pylspclient
import pathlib
from model import CompletionRequestParams, CompletionResponse, SignInInitiative
class CopilotService(object):
"""
Copilot service
"""
@staticmethod
def _check_dependency():
"""
Check if the dependency is installed.
The Copilot LSP server requires Node.js 16.x.x.
The node executable should be in the PATH.
:raises: Exception if the dependency is not satisfied.
"""
p = subprocess.Popen(["node", "-v"], stdout=subprocess.PIPE)
out, err = p.communicate()
if err:
raise Exception(f"Node.js is not executable: {err.decode('utf-8')}")
ver = out.decode("utf-8").strip().replace("v", "")
if not (semver.compare("16.0.0", ver) <= 0 and semver.compare(ver, "17.0.0")) < 0:
raise Exception(f"Node.js version {ver} is not supported. Please install Node.js 16")
def __init__(self, root_path: str,
copilot_agent_path: str = None):
"""
Initialize the Copilot service.
:param root_path: the root directory that Copilot LSP server runs on. Usually this should be the root directory
of the project for which Copilot suggests code completions.
:param copilot_agent_path: optional, the path to the Copilot LSP server (agent.js).
Defaults to the bundled Copilot LSP
"""
self.root_path = root_path
self.workspace_folders = None
self.copilot_agent_path = copilot_agent_path
self._check_dependency()
lsp_cmd = ["node", self.copilot_agent_path if self.copilot_agent_path is not None else os.path.join(
os.path.dirname(__file__), "..", "..", "copilot", "dist", "agent.js")]
p = subprocess.Popen(lsp_cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
self.p = p
json_rpc_endpoint = pylspclient.JsonRpcEndpoint(p.stdin, p.stdout)
self.lsp_endpoint = pylspclient.LspEndpoint(json_rpc_endpoint,
timeout=10,
method_callback=self._callback,
notify_callback=self._callback)
self.lsp_client = pylspclient.LspClient(self.lsp_endpoint)
self._initialize()
def _initialize(self):
self.lsp_client.initialize(self.p.pid, self.root_path, pathlib.Path(self.root_path).as_uri(), None,
self._client_capabilities, "off",
self.workspace_folders)
self.lsp_client.initialized()
def shutdown(self):
"""
Shutdown the Copilot service.
"""
self.lsp_client.shutdown()
self.p.terminate()
def get_completions(self, completion_request_params: CompletionRequestParams) -> CompletionResponse:
"""
Get code completions.
:param completion_request_params: a CompletionRequestParams object that specifies the source code file
to suggest code completions for.
:return: CompletionResponse object, containing all the candidate code completions.
"""
return self.lsp_endpoint.call_method("getCompletions", **completion_request_params.to_dict(self.root_path))
def sign_in(self, callback: Callable[[SignInInitiative], None]):
"""
Sign in to Copilot.
:param callback: a function that takes a SignInInitiative object as input. Users of Copilot should authorize
Copilot at https://github.com/login/device using the userCode given in the SignInInitiative
object. When the callback returns, the Copilot must be already authorized since the Copilot LSP
server will check the sign-in status after the callback returns. After signing in, there is
no need to re-sign in even if the program is restarted.
"""
resp = self.lsp_endpoint.call_method("signInInitiate", **dict())
callback(resp)
return self.lsp_endpoint.call_method("signInConfirm", **dict())
def sign_out(self):
"""
Sign out the current user of Copilot.
:return:
"""
self.lsp_endpoint.call_method("signOut", **dict())
def signed_in(self) -> bool:
"""
Check if a user is already signed in to Copilot.
:return:
"""
resp = self.lsp_endpoint.call_method("checkStatus", **{'options': {'localChecksOnly': True}})
return resp['status'] == 'OK' or resp['status'] == 'MaybeOK'
def _callback(self, key: str, data: dict):
pass
@property
def _client_capabilities(self):
return {
"window": {
"showDocument": {
"support": False
},
"workDoneProgress": True,
"showMessage": {
"messageActionItem": {
"additionalPropertiesSupport": False
}
}
},
"callHierarchy": {
"dynamicRegistration": False
},
"textDocument": {
"implementation": {
"linkSupport": True
},
"typeDefinition": {
"linkSupport": True
},
"completion": {
"contextSupport": False,
"completionItemKind": {
"valueSet": [
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
]
},
"dynamicRegistration": False,
"completionItem": {
"preselectSupport": False,
"deprecatedSupport": False,
"documentationFormat": [
"markdown",
"plaintext"
],
"snippetSupport": False,
"commitCharactersSupport": False
}
},
"signatureHelp": {
"signatureInformation": {
"activeParameterSupport": True,
"documentationFormat": [
"markdown",
"plaintext"
],
"parameterInformation": {
"labelOffsetSupport": True
}
},
"dynamicRegistration": False
},
"hover": {
"contentFormat": [
"markdown",
"plaintext"
],
"dynamicRegistration": False
},
"codeAction": {
"codeActionLiteralSupport": {
"codeActionKind": {
"valueSet": [
"",
"Empty",
"QuickFix",
"Refactor",
"RefactorExtract",
"RefactorInline",
"RefactorRewrite",
"Source",
"SourceOrganizeImports",
"quickfix",
"refactor",
"refactor.extract",
"refactor.inline",
"refactor.rewrite",
"source",
"source.organizeImports"
]
}
},
"isPreferredSupport": True,
"dataSupport": True,
"resolveSupport": {
"properties": [
"edit"
]
},
"dynamicRegistration": False
},
"references": {
"dynamicRegistration": False
},
"documentHighlight": {
"dynamicRegistration": False
},
"synchronization": {
"willSave": False,
"willSaveWaitUntil": False,
"didSave": True,
"dynamicRegistration": False
},
"rename": {
"prepareSupport": True,
"dynamicRegistration": False
},
"documentSymbol": {
"hierarchicalDocumentSymbolSupport": True,
"dynamicRegistration": False,
"symbolKind": {
"valueSet": [
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
]
}
},
"declaration": {
"linkSupport": True
},
"publishDiagnostics": {
"tagSupport": {
"valueSet": [
1,
2
]
},
"relatedInformation": True
},
"definition": {
"linkSupport": True
}
},
"workspace": {
"applyEdit": True,
"workspaceFolders": True,
"workspaceEdit": {
"resourceOperations": [
"rename",
"create",
"delete"
]
},
"symbol": {
"hierarchicalWorkspaceSymbolSupport": True,
"dynamicRegistration": False,
"symbolKind": {
"valueSet": [
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
]
}
},
"configuration": True
}
}