@@ -266,34 +266,77 @@ def _extract_range(self, definition):
266266 from jedi import common
267267 from jedi .parser .utils import load_parser
268268 # get the scope range
269- if definition .type in ['class' , 'function' ]:
270- scope = definition ._definition
271- start_line = scope .start_pos [0 ] - 1
272- start_column = scope .start_pos [1 ]
273- end_line = scope .end_pos [0 ] - 1
274- end_column = scope .end_pos [1 ]
275- # get the lines
276- path = definition ._definition .get_parent_until ().path
277- parser = load_parser (path )
278- lines = common .splitlines (parser .source )
279- lines [end_line ] = lines [end_line ][:end_column ]
280- # trim the lines
281- lines = lines [start_line :end_line + 1 ]
282- lines = '\n ' .join (lines ).rstrip ().split ('\n ' )
283- end_line = start_line + len (lines ) - 1
284- end_column = len (lines [- 1 ]) - 1
285- else :
286- symbol = definition ._name
287- start_line = symbol .start_pos [0 ] - 1
288- start_column = symbol .start_pos [1 ]
289- end_line = symbol .end_pos [0 ] - 1
290- end_column = symbol .end_pos [1 ]
291- return {
292- 'start_line' : start_line ,
293- 'start_column' : start_column ,
294- 'end_line' : end_line ,
295- 'end_column' : end_column
296- }
269+ try :
270+ if definition .type in ['class' , 'function' ] and hasattr (definition , '_definition' ):
271+ scope = definition ._definition
272+ start_line = scope .start_pos [0 ] - 1
273+ start_column = scope .start_pos [1 ]
274+ end_line = scope .end_pos [0 ] - 1
275+ end_column = scope .end_pos [1 ]
276+ # get the lines
277+ path = definition ._definition .get_parent_until ().path
278+ parser = load_parser (path )
279+ lines = common .splitlines (parser .source )
280+ lines [end_line ] = lines [end_line ][:end_column ]
281+ # trim the lines
282+ lines = lines [start_line :end_line + 1 ]
283+ lines = '\n ' .join (lines ).rstrip ().split ('\n ' )
284+ end_line = start_line + len (lines ) - 1
285+ end_column = len (lines [- 1 ]) - 1
286+ else :
287+ symbol = definition ._name
288+ start_line = symbol .start_pos [0 ] - 1
289+ start_column = symbol .start_pos [1 ]
290+ end_line = symbol .end_pos [0 ] - 1
291+ end_column = symbol .end_pos [1 ]
292+ return {
293+ 'start_line' : start_line ,
294+ 'start_column' : start_column ,
295+ 'end_line' : end_line ,
296+ 'end_column' : end_column
297+ }
298+ except Exception as e :
299+ return {
300+ 'start_line' : definition .line - 1 ,
301+ 'start_column' : definition .column ,
302+ 'end_line' : definition .line - 1 ,
303+ 'end_column' : definition .column
304+ }
305+ def _get_definitions (self , definitions , identifier = None ):
306+ """Serialize response to be read from VSCode.
307+
308+ Args:
309+ definitions: List of jedi.api.classes.Definition objects.
310+ identifier: Unique completion identifier to pass back to VSCode.
311+
312+ Returns:
313+ Serialized string to send to VSCode.
314+ """
315+ _definitions = []
316+ for definition in definitions :
317+ try :
318+ if definition .module_path :
319+ if definition .type == 'import' :
320+ definition = self ._top_definition (definition )
321+ if not definition .module_path :
322+ continue
323+ try :
324+ parent = definition .parent ()
325+ container = parent .name if parent .type != 'module' else ''
326+ except Exception :
327+ container = ''
328+ _definition = {
329+ 'text' : definition .name ,
330+ 'type' : self ._get_definition_type (definition ),
331+ 'raw_type' : definition .type ,
332+ 'fileName' : definition .module_path ,
333+ 'container' : container ,
334+ 'range' : self ._extract_range (definition )
335+ }
336+ _definitions .append (_definition )
337+ except Exception as e :
338+ pass
339+ return _definitions
297340
298341 def _serialize_definitions (self , definitions , identifier = None ):
299342 """Serialize response to be read from VSCode.
@@ -424,8 +467,10 @@ def _process_request(self, request):
424467 column = request ['column' ], path = request .get ('path' , '' ))
425468
426469 if lookup == 'definitions' :
427- return self ._write_response (self ._serialize_definitions (
428- script .goto_assignments (), request ['id' ]))
470+ defs = self ._get_definitions (script .goto_definitions (), request ['id' ])
471+ if len (defs ) == 0 :
472+ defs = self ._get_definitions (script .goto_assignments (), request ['id' ])
473+ return self ._write_response (json .dumps ({'id' : request ['id' ], 'results' : defs }))
429474 if lookup == 'tooltip' :
430475 return self ._write_response (self ._serialize_tooltip (
431476 script .goto_definitions (), request ['id' ]))
0 commit comments