This repository has been archived by the owner on Dec 24, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 51
/
completion.py
711 lines (633 loc) · 26 KB
/
completion.py
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
import os
import os.path
import io
import re
import sys
import json
import traceback
import platform
jediPreview = False
class RedirectStdout(object):
def __init__(self, new_stdout=None):
"""If stdout is None, redirect to /dev/null"""
self._new_stdout = new_stdout or open(os.devnull, "w")
def __enter__(self):
sys.stdout.flush()
self.oldstdout_fno = os.dup(sys.stdout.fileno())
os.dup2(self._new_stdout.fileno(), 1)
def __exit__(self, exc_type, exc_value, traceback):
self._new_stdout.flush()
os.dup2(self.oldstdout_fno, 1)
os.close(self.oldstdout_fno)
class JediCompletion(object):
basic_types = {
"module": "import",
"instance": "variable",
"statement": "value",
"param": "variable",
}
def __init__(self):
self.default_sys_path = sys.path
self.environment = jedi.api.environment.create_environment(
sys.executable, safe=False
)
self._input = io.open(sys.stdin.fileno(), encoding="utf-8")
if (os.path.sep == "/") and (platform.uname()[2].find("Microsoft") > -1):
# WSL; does not support UNC paths
self.drive_mount = "/mnt/"
elif sys.platform == "cygwin":
# cygwin
self.drive_mount = "/cygdrive/"
else:
# Do no normalization, e.g. Windows build of Python.
# Could add additional test: ((os.path.sep == '/') and os.path.isdir('/mnt/c'))
# However, this may have more false positives trying to identify Windows/*nix hybrids
self.drive_mount = ""
def _get_definition_type(self, definition):
# if definition.type not in ['import', 'keyword'] and is_built_in():
# return 'builtin'
try:
if definition.type in ["statement"] and definition.name.isupper():
return "constant"
return self.basic_types.get(definition.type, definition.type)
except Exception:
return "builtin"
def _additional_info(self, completion):
"""Provide additional information about the completion object."""
if not hasattr(completion, "_definition") or completion._definition is None:
return ""
if completion.type == "statement":
nodes_to_display = ["InstanceElement", "String", "Node", "Lambda", "Number"]
return "".join(
c.get_code()
for c in completion._definition.children
if type(c).__name__ in nodes_to_display
).replace("\n", "")
return ""
@classmethod
def _get_top_level_module(cls, path):
"""Recursively walk through directories looking for top level module.
Jedi will use current filepath to look for another modules at same
path, but it will not be able to see modules **above**, so our goal
is to find the higher python module available from filepath.
"""
_path, _ = os.path.split(path)
if os.path.isfile(os.path.join(_path, "__init__.py")):
return cls._get_top_level_module(_path)
return path
def _generate_signature(self, completion):
"""Generate signature with function arguments.
"""
if completion.type in ["module"] or not hasattr(completion, "params"):
return ""
return "%s(%s)" % (
completion.name,
", ".join(p.description[6:] for p in completion.params if p),
)
def _get_call_signatures(self, script, line, column):
"""Extract call signatures from jedi.api.Script object in failsafe way.
Returns:
Tuple with original signature object, name and value.
"""
_signatures = []
try:
call_signatures = script.get_signatures(line, column)
except KeyError:
call_signatures = []
except:
call_signatures = []
for signature in call_signatures:
for pos, param in enumerate(signature.params):
if not param.name:
continue
name = self._get_param_name(param)
if param.name == "self" and pos == 0:
continue
if name.startswith("*"):
continue
value = self._get_param_value(param)
_signatures.append((signature, name, value))
return _signatures
def _get_param_name(self, p):
if p.name.startswith("param "):
return p.name[6:] # drop leading 'param '
return p.name
def _get_param_value(self, p):
pair = p.description.split("=")
if len(pair) > 1:
return pair[1]
return None
def _get_call_signatures_with_args(self, script, line, column):
"""Extract call signatures from jedi.api.Script object in failsafe way.
Returns:
Array with dictionary
"""
_signatures = []
try:
call_signatures = script.get_signatures(line, column)
except KeyError:
call_signatures = []
for signature in call_signatures:
sig = {
"name": "",
"description": "",
"docstring": "",
"paramindex": 0,
"params": [],
"bracketstart": [],
}
sig["description"] = signature.description
try:
sig["docstring"] = signature.docstring()
sig["raw_docstring"] = signature.docstring(raw=True)
except Exception:
sig["docstring"] = ""
sig["raw_docstring"] = ""
sig["name"] = signature.name
sig["paramindex"] = signature.index
sig["bracketstart"].append(signature.index)
_signatures.append(sig)
for pos, param in enumerate(signature.params):
if not param.name:
continue
name = self._get_param_name(param)
if param.name == "self" and pos == 0:
continue
value = self._get_param_value(param)
paramDocstring = ""
try:
paramDocstring = param.docstring()
except Exception:
paramDocstring = ""
sig["params"].append(
{
"name": name,
"value": value,
"docstring": paramDocstring,
"description": param.description,
}
)
return _signatures
def _serialize_completions(self, script, line, column, identifier=None, prefix=""):
"""Serialize response to be read from VSCode.
Args:
script: Instance of jedi.api.Script object.
identifier: Unique completion identifier to pass back to VSCode.
prefix: String with prefix to filter function arguments.
Used only when fuzzy matcher turned off.
Returns:
Serialized string to send to VSCode.
"""
_completions = []
for signature, name, value in self._get_call_signatures(script, line, column):
if not self.fuzzy_matcher and not name.lower().startswith(prefix.lower()):
continue
_completion = {
"type": "property",
"raw_type": "",
"rightLabel": self._additional_info(signature),
}
_completion["description"] = ""
_completion["raw_docstring"] = ""
# we pass 'text' here only for fuzzy matcher
if value:
_completion["snippet"] = "%s=${1:%s}$0" % (name, value)
_completion["text"] = "%s=" % (name)
else:
_completion["snippet"] = "%s=$1$0" % name
_completion["text"] = name
_completion["displayText"] = name
_completions.append(_completion)
try:
completions = script.complete(line, column)
except KeyError:
completions = []
except:
completions = []
for completion in completions:
try:
_completion = {
"text": completion.name,
"type": self._get_definition_type(completion),
"raw_type": completion.type,
"rightLabel": self._additional_info(completion),
}
except Exception:
continue
for c in _completions:
if c["text"] == _completion["text"]:
c["type"] = _completion["type"]
c["raw_type"] = _completion["raw_type"]
if any(
[c["text"].split("=")[0] == _completion["text"] for c in _completions]
):
# ignore function arguments we already have
continue
_completions.append(_completion)
return json.dumps({"id": identifier, "results": _completions})
def _serialize_methods(self, script, line, column, identifier=None, prefix=""):
_methods = []
try:
completions = script.complete(line, column)
except KeyError:
return []
for completion in completions:
if completion.name == "__autocomplete_python":
instance = completion.parent().name
break
else:
instance = "self.__class__"
for completion in completions:
params = []
if hasattr(completion, "params"):
params = [p.description for p in completion.params if p]
if completion.parent().type == "class":
_methods.append(
{
"parent": completion.parent().name,
"instance": instance,
"name": completion.name,
"params": params,
"moduleName": completion.module_name,
"fileName": completion.module_path,
"line": completion.line,
"column": completion.column,
}
)
return json.dumps({"id": identifier, "results": _methods})
def _serialize_arguments(self, script, line, column, identifier=None):
"""Serialize response to be read from VSCode.
Args:
script: Instance of jedi.api.Script object.
identifier: Unique completion identifier to pass back to VSCode.
Returns:
Serialized string to send to VSCode.
"""
return json.dumps(
{
"id": identifier,
"results": self._get_call_signatures_with_args(script, line, column),
}
)
def _top_definition(self, definition):
for d in definition.goto_assignments():
if d == definition:
continue
if d.type == "import":
return self._top_definition(d)
else:
return d
return definition
def _extract_range_jedi_0_11_1(self, definition):
from parso.utils import split_lines
# get the scope range
try:
if definition.type in ["class", "function"]:
tree_name = definition._name.tree_name
scope = tree_name.get_definition()
start_line = scope.start_pos[0] - 1
start_column = scope.start_pos[1]
# get the lines
code = scope.get_code(include_prefix=False)
lines = split_lines(code)
# trim the lines
lines = "\n".join(lines).rstrip().split("\n")
end_line = start_line + len(lines) - 1
end_column = len(lines[-1]) - 1
else:
symbol = definition._name.tree_name
start_line = symbol.start_pos[0] - 1
start_column = symbol.start_pos[1]
end_line = symbol.end_pos[0] - 1
end_column = symbol.end_pos[1]
return {
"start_line": start_line,
"start_column": start_column,
"end_line": end_line,
"end_column": end_column,
}
except Exception as e:
return {
"start_line": definition.line - 1,
"start_column": definition.column,
"end_line": definition.line - 1,
"end_column": definition.column,
}
def _extract_range(self, definition):
"""Provides the definition range of a given definition
For regular symbols it returns the start and end location of the
characters making up the symbol.
For scoped containers it will return the entire definition of the
scope.
The scope that jedi provides ends with the first character of the next
scope so it's not ideal. For vscode we need the scope to end with the
last character of actual code. That's why we extract the lines that
make up our scope and trim the trailing whitespace.
"""
return self._extract_range_jedi_0_11_1(definition)
def _get_definitionsx(self, definitions, identifier=None, ignoreNoModulePath=False):
"""Serialize response to be read from VSCode.
Args:
definitions: List of jedi.api.classes.Definition objects.
identifier: Unique completion identifier to pass back to VSCode.
Returns:
Serialized string to send to VSCode.
"""
_definitions = []
for definition in definitions:
try:
if definition.type == "import":
definition = self._top_definition(definition)
definitionRange = {
"start_line": 0,
"start_column": 0,
"end_line": 0,
"end_column": 0,
}
module_path = ""
if hasattr(definition, "module_path") and definition.module_path:
module_path = definition.module_path
definitionRange = self._extract_range(definition)
else:
if not ignoreNoModulePath:
continue
try:
parent = definition.parent()
container = parent.name if parent.type != "module" else ""
except Exception:
container = ""
try:
docstring = definition.docstring()
rawdocstring = definition.docstring(raw=True)
except Exception:
docstring = ""
rawdocstring = ""
_definition = {
"text": definition.name,
"type": self._get_definition_type(definition),
"raw_type": definition.type,
"fileName": str(module_path),
"container": container,
"range": definitionRange,
"description": definition.description,
"docstring": docstring,
"raw_docstring": rawdocstring,
"signature": self._generate_signature(definition),
}
_definitions.append(_definition)
except Exception as e:
pass
return _definitions
def _serialize_definitions(self, definitions, identifier=None):
"""Serialize response to be read from VSCode.
Args:
definitions: List of jedi.api.classes.Definition objects.
identifier: Unique completion identifier to pass back to VSCode.
Returns:
Serialized string to send to VSCode.
"""
_definitions = []
for definition in definitions:
try:
if definition.module_path:
if definition.type == "import":
definition = self._top_definition(definition)
if not definition.module_path:
continue
try:
parent = definition.parent()
container = parent.name if parent.type != "module" else ""
except Exception:
container = ""
try:
docstring = definition.docstring()
rawdocstring = definition.docstring(raw=True)
except Exception:
docstring = ""
rawdocstring = ""
_definition = {
"text": definition.name,
"type": self._get_definition_type(definition),
"raw_type": definition.type,
"fileName": definition.module_path,
"container": container,
"range": self._extract_range(definition),
"description": definition.description,
"docstring": docstring,
"raw_docstring": rawdocstring,
}
_definitions.append(_definition)
except Exception as e:
pass
return json.dumps({"id": identifier, "results": _definitions})
def _serialize_tooltip(self, definitions, identifier=None):
_definitions = []
for definition in definitions:
signature = definition.name
description = None
if definition.type in ["class", "function"]:
signature = self._generate_signature(definition)
try:
description = definition.docstring(raw=True).strip()
except Exception:
description = ""
if not description and not hasattr(definition, "get_line_code"):
# jedi returns an empty string for compiled objects
description = definition.docstring().strip()
if definition.type == "module":
signature = definition.full_name
try:
description = definition.docstring(raw=True).strip()
except Exception:
description = ""
if not description and hasattr(definition, "get_line_code"):
# jedi returns an empty string for compiled objects
description = definition.docstring().strip()
_definition = {
"type": self._get_definition_type(definition),
"text": definition.name,
"description": description,
"docstring": description,
"signature": signature,
}
_definitions.append(_definition)
return json.dumps({"id": identifier, "results": _definitions})
def _serialize_usages(self, usages, identifier=None):
_usages = []
for usage in usages:
_usages.append(
{
"name": usage.name,
"moduleName": usage.module_name,
"fileName": usage.module_path,
"line": usage.line,
"column": usage.column,
}
)
return json.dumps({"id": identifier, "results": _usages})
def _deserialize(self, request):
"""Deserialize request from VSCode.
Args:
request: String with raw request from VSCode.
Returns:
Python dictionary with request data.
"""
return json.loads(request)
def _set_request_config(self, config):
"""Sets config values for current request.
This includes sys.path modifications which is getting restored to
default value on each request so each project should be isolated
from each other.
Args:
config: Dictionary with config values.
"""
sys.path = self.default_sys_path
self.use_snippets = config.get("useSnippets")
self.show_doc_strings = config.get("showDescriptions", True)
self.fuzzy_matcher = config.get("fuzzyMatcher", False)
jedi.settings.case_insensitive_completion = config.get(
"caseInsensitiveCompletion", True
)
for path in config.get("extraPaths", []):
if path and path not in sys.path:
sys.path.insert(0, path)
def _normalize_request_path(self, request):
"""Normalize any Windows paths received by a *nix build of
Python. Does not alter the reverse os.path.sep=='\\',
i.e. *nix paths received by a Windows build of Python.
"""
if "path" in request:
if not self.drive_mount:
return
newPath = request["path"].replace("\\", "/")
if newPath[0:1] == "/":
# is absolute path with no drive letter
request["path"] = newPath
elif newPath[1:2] == ":":
# is path with drive letter, only absolute can be mapped
request["path"] = self.drive_mount + newPath[0:1].lower() + newPath[2:]
else:
# is relative path
request["path"] = newPath
def _process_request(self, request):
"""Accept serialized request from VSCode and write response.
"""
request = self._deserialize(request)
self._set_request_config(request.get("config", {}))
self._normalize_request_path(request)
path = self._get_top_level_module(request.get("path", ""))
if len(path) > 0 and path not in sys.path:
sys.path.insert(0, path)
lookup = request.get("lookup", "completions")
project = jedi.Project(os.path.dirname(path), sys_path=sys.path)
if lookup == "names":
return self._serialize_definitions(
jedi.Script(
code=request.get("source", None),
path=request.get("path", ""),
project=project,
environment=self.environment,
).get_names(all_scopes=True),
request["id"],
)
line = request["line"] + 1
column = request["column"]
script = jedi.Script(
code=request.get("source", None),
path=request.get("path", ""),
project=project,
environment=self.environment,
)
if lookup == "definitions":
defs = self._get_definitionsx(
script.goto(line, column, follow_imports=True), request["id"]
)
return json.dumps({"id": request["id"], "results": defs})
if lookup == "tooltip":
if jediPreview:
defs = []
try:
defs = self._get_definitionsx(
script.infer(line, column), request["id"], True
)
except:
pass
try:
if len(defs) == 0:
defs = self._get_definitionsx(
script.goto(line, column), request["id"], True
)
except:
pass
return json.dumps({"id": request["id"], "results": defs})
else:
try:
return self._serialize_tooltip(
script.infer(line, column), request["id"]
)
except:
return json.dumps({"id": request["id"], "results": []})
elif lookup == "arguments":
return self._serialize_arguments(script, line, column, request["id"])
elif lookup == "usages":
return self._serialize_usages(
script.get_references(line, column), request["id"]
)
elif lookup == "methods":
return self._serialize_methods(
script, line, column, request["id"], request.get("prefix", "")
)
else:
return self._serialize_completions(
script, line, column, request["id"], request.get("prefix", "")
)
def _write_response(self, response):
sys.stdout.write(response + "\n")
sys.stdout.flush()
def watch(self):
while True:
try:
rq = self._input.readline()
if len(rq) == 0:
# Reached EOF - indication our parent process is gone.
sys.stderr.write(
"Received EOF from the standard input,exiting" + "\n"
)
sys.stderr.flush()
return
with RedirectStdout():
response = self._process_request(rq)
self._write_response(response)
except Exception:
sys.stderr.write(traceback.format_exc() + "\n")
sys.stderr.flush()
if __name__ == "__main__":
cachePrefix = "v"
modulesToLoad = ""
if len(sys.argv) > 2 and sys.argv[1] == "custom":
jediPath = sys.argv[2]
jediPreview = True
cachePrefix = "custom_v"
if len(sys.argv) > 3:
modulesToLoad = sys.argv[3]
else:
# release
jediPath = os.path.join(os.path.dirname(__file__), "lib", "python")
if len(sys.argv) > 1:
modulesToLoad = sys.argv[1]
sys.path.insert(0, jediPath)
import jedi
digits = jedi.__version__.split(".")
if int(digits[0]) == 0 and int(digits[1]) < 17:
raise RuntimeError("Jedi version %s too old, requires >= 0.17.0" % (jedi.__version__))
else:
if jediPreview:
jedi.settings.cache_directory = os.path.join(
jedi.settings.cache_directory,
cachePrefix + jedi.__version__.replace(".", ""),
)
# remove jedi from path after we import it so it will not be completed
sys.path.pop(0)
if len(modulesToLoad) > 0:
jedi.preload_module(*modulesToLoad.split(","))
JediCompletion().watch()