|
6 | 6 | import traceback |
7 | 7 |
|
8 | 8 | WORD_RE = re.compile(r'\w') |
| 9 | +jediPreview = False |
9 | 10 |
|
10 | 11 |
|
11 | 12 | class JediCompletion(object): |
@@ -314,7 +315,7 @@ def _extract_range(self, definition): |
314 | 315 | 'end_line': definition.line - 1, |
315 | 316 | 'end_column': definition.column |
316 | 317 | } |
317 | | - def _get_definitions(self, definitions, identifier=None): |
| 318 | + def _get_definitionsx(self, definitions, identifier=None, ignoreNoModulePath=False): |
318 | 319 | """Serialize response to be read from VSCode. |
319 | 320 |
|
320 | 321 | Args: |
@@ -327,25 +328,39 @@ def _get_definitions(self, definitions, identifier=None): |
327 | 328 | _definitions = [] |
328 | 329 | for definition in definitions: |
329 | 330 | try: |
330 | | - if definition.module_path: |
331 | | - if definition.type == 'import': |
332 | | - definition = self._top_definition(definition) |
333 | | - if not definition.module_path: |
| 331 | + if definition.type == 'import': |
| 332 | + definition = self._top_definition(definition) |
| 333 | + definitionRange = { |
| 334 | + 'start_line': 0, |
| 335 | + 'start_column': 0, |
| 336 | + 'end_line': 0, |
| 337 | + 'end_column': 0 |
| 338 | + } |
| 339 | + module_path = '' |
| 340 | + if hasattr(definition, 'module_path') and definition.module_path: |
| 341 | + module_path = definition.module_path |
| 342 | + definitionRange = self._extract_range(definition) |
| 343 | + else: |
| 344 | + if not ignoreNoModulePath: |
334 | 345 | continue |
335 | | - try: |
336 | | - parent = definition.parent() |
337 | | - container = parent.name if parent.type != 'module' else '' |
338 | | - except Exception: |
339 | | - container = '' |
340 | | - _definition = { |
341 | | - 'text': definition.name, |
342 | | - 'type': self._get_definition_type(definition), |
343 | | - 'raw_type': definition.type, |
344 | | - 'fileName': definition.module_path, |
345 | | - 'container': container, |
346 | | - 'range': self._extract_range(definition) |
347 | | - } |
348 | | - _definitions.append(_definition) |
| 346 | + try: |
| 347 | + parent = definition.parent() |
| 348 | + container = parent.name if parent.type != 'module' else '' |
| 349 | + except Exception: |
| 350 | + container = '' |
| 351 | + _definition = { |
| 352 | + 'text': definition.name, |
| 353 | + 'type': self._get_definition_type(definition), |
| 354 | + 'raw_type': definition.type, |
| 355 | + 'fileName': module_path, |
| 356 | + 'container': container, |
| 357 | + 'range': definitionRange, |
| 358 | + 'description': definition.description, |
| 359 | + 'docstring': definition.docstring(), |
| 360 | + 'raw_docstring': definition.docstring(raw=True), |
| 361 | + 'signature': self._generate_signature(definition) |
| 362 | + } |
| 363 | + _definitions.append(_definition) |
349 | 364 | except Exception as e: |
350 | 365 | pass |
351 | 366 | return _definitions |
@@ -379,7 +394,10 @@ def _serialize_definitions(self, definitions, identifier=None): |
379 | 394 | 'raw_type': definition.type, |
380 | 395 | 'fileName': definition.module_path, |
381 | 396 | 'container': container, |
382 | | - 'range': self._extract_range(definition) |
| 397 | + 'range': self._extract_range(definition), |
| 398 | + 'description': definition.description, |
| 399 | + 'docstring': definition.docstring(), |
| 400 | + 'raw_docstring': definition.docstring(raw=True) |
383 | 401 | } |
384 | 402 | _definitions.append(_definition) |
385 | 403 | except Exception as e: |
@@ -405,7 +423,9 @@ def _serialize_tooltip(self, definitions, identifier=None): |
405 | 423 | description = definition.docstring().strip() |
406 | 424 | _definition = { |
407 | 425 | 'type': self._get_definition_type(definition), |
| 426 | + 'text': definition.name, |
408 | 427 | 'description': description, |
| 428 | + 'docstring': description, |
409 | 429 | 'signature': signature |
410 | 430 | } |
411 | 431 | _definitions.append(_definition) |
@@ -477,15 +497,20 @@ def _process_request(self, request): |
477 | 497 | script = jedi.api.Script( |
478 | 498 | source=request.get('source', None), line=request['line'] + 1, |
479 | 499 | column=request['column'], path=request.get('path', '')) |
480 | | - |
| 500 | + |
481 | 501 | if lookup == 'definitions': |
482 | | - defs = self._get_definitions(script.goto_definitions(), request['id']) |
| 502 | + defs = self._get_definitionsx(script.goto_definitions(), request['id']) |
483 | 503 | if len(defs) == 0: |
484 | | - defs = self._get_definitions(script.goto_assignments(), request['id']) |
| 504 | + defs = self._get_definitionsx(script.goto_assignments(), request['id']) |
485 | 505 | return self._write_response(json.dumps({'id': request['id'], 'results': defs})) |
486 | 506 | if lookup == 'tooltip': |
487 | | - return self._write_response(self._serialize_tooltip( |
488 | | - script.goto_definitions(), request['id'])) |
| 507 | + if jediPreview: |
| 508 | + defs = self._get_definitionsx(script.goto_definitions(), request['id'], True) |
| 509 | + if len(defs) == 0: |
| 510 | + defs = self._get_definitionsx(script.goto_assignments(), request['id'], True) |
| 511 | + return self._write_response(json.dumps({'id': request['id'], 'results': defs})) |
| 512 | + else: |
| 513 | + return self._write_response(self._serialize_tooltip(script.goto_definitions(), request['id'])) |
489 | 514 | elif lookup == 'arguments': |
490 | 515 | return self._write_response(self._serialize_arguments( |
491 | 516 | script, request['id'])) |
@@ -514,7 +539,6 @@ def watch(self): |
514 | 539 | sys.stderr.flush() |
515 | 540 |
|
516 | 541 | if __name__ == '__main__': |
517 | | - jediPreview = False |
518 | 542 | cachePrefix = 'v' |
519 | 543 | if len(sys.argv) > 0 and sys.argv[1] == 'preview': |
520 | 544 | jediPath = os.path.join(os.path.dirname(__file__), 'preview') |
|