66import traceback
77
88WORD_RE = re .compile (r'\w' )
9+ jediPreview = False
910
1011
1112class JediCompletion (object ):
@@ -314,7 +315,7 @@ def _extract_range(self, definition):
314315 'end_line' : definition .line - 1 ,
315316 'end_column' : definition .column
316317 }
317- def _get_definitions (self , definitions , identifier = None ):
318+ def _get_definitionsx (self , definitions , identifier = None , ignoreNoModulePath = False ):
318319 """Serialize response to be read from VSCode.
319320
320321 Args:
@@ -327,25 +328,39 @@ def _get_definitions(self, definitions, identifier=None):
327328 _definitions = []
328329 for definition in definitions :
329330 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 :
334345 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 )
349364 except Exception as e :
350365 pass
351366 return _definitions
@@ -379,7 +394,10 @@ def _serialize_definitions(self, definitions, identifier=None):
379394 'raw_type' : definition .type ,
380395 'fileName' : definition .module_path ,
381396 '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 )
383401 }
384402 _definitions .append (_definition )
385403 except Exception as e :
@@ -405,7 +423,9 @@ def _serialize_tooltip(self, definitions, identifier=None):
405423 description = definition .docstring ().strip ()
406424 _definition = {
407425 'type' : self ._get_definition_type (definition ),
426+ 'text' : definition .name ,
408427 'description' : description ,
428+ 'docstring' : description ,
409429 'signature' : signature
410430 }
411431 _definitions .append (_definition )
@@ -477,15 +497,20 @@ def _process_request(self, request):
477497 script = jedi .api .Script (
478498 source = request .get ('source' , None ), line = request ['line' ] + 1 ,
479499 column = request ['column' ], path = request .get ('path' , '' ))
480-
500+
481501 if lookup == 'definitions' :
482- defs = self ._get_definitions (script .goto_definitions (), request ['id' ])
502+ defs = self ._get_definitionsx (script .goto_definitions (), request ['id' ])
483503 if len (defs ) == 0 :
484- defs = self ._get_definitions (script .goto_assignments (), request ['id' ])
504+ defs = self ._get_definitionsx (script .goto_assignments (), request ['id' ])
485505 return self ._write_response (json .dumps ({'id' : request ['id' ], 'results' : defs }))
486506 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' ]))
489514 elif lookup == 'arguments' :
490515 return self ._write_response (self ._serialize_arguments (
491516 script , request ['id' ]))
@@ -514,7 +539,6 @@ def watch(self):
514539 sys .stderr .flush ()
515540
516541if __name__ == '__main__' :
517- jediPreview = False
518542 cachePrefix = 'v'
519543 if len (sys .argv ) > 0 and sys .argv [1 ] == 'preview' :
520544 jediPath = os .path .join (os .path .dirname (__file__ ), 'preview' )
0 commit comments