88
99@hookimpl
1010def pyls_document_symbols (config , document ):
11- # all_scopes = config.plugin_settings('jedi_symbols').get('all_scopes', True)
11+ if not config .capabilities .get ("documentSymbol" , {}).get ("hierarchicalDocumentSymbolSupport" , False ):
12+ return pyls_document_symbols_legacy (config , document )
13+ # returns DocumentSymbol[]
14+ hide_imports = config .plugin_settings ('jedi_symbols' ).get ('hide_imports' , False )
1215 definitions = document .jedi_names (all_scopes = False )
1316 def transform (d ):
14- include_d = _include_def (d )
17+ include_d = _include_def (d , hide_imports )
1518 if include_d is None :
1619 return None
1720 children = [dt for dt in (transform (d1 ) for d1 in d .defined_names ()) if dt ] if include_d else None
@@ -28,17 +31,33 @@ def transform(d):
2831 }
2932 return [dt for dt in (transform (d ) for d in definitions ) if dt ]
3033
31- def _include_def (definition ):
34+ def pyls_document_symbols_legacy (config , document ):
35+ # returns SymbolInformation[]
36+ all_scopes = config .plugin_settings ('jedi_symbols' ).get ('all_scopes' , True )
37+ hide_imports = config .plugin_settings ('jedi_symbols' ).get ('hide_imports' , False )
38+ definitions = document .jedi_names (all_scopes = all_scopes )
39+ return [{
40+ 'name' : d .name ,
41+ 'containerName' : _container (d ),
42+ 'location' : {
43+ 'uri' : document .uri ,
44+ 'range' : _range (d ),
45+ },
46+ 'kind' : _kind (d ),
47+ } for d in definitions if _include_def (d , hide_imports ) is not None ]
48+
49+ def _include_def (definition , hide_imports = True ):
3250 # True: include def and sub-defs
3351 # False: include def but do not include sub-defs
3452 # None: Do not include def or sub-defs
3553 if (# Unused vars should also be skipped
3654 definition .name != '_' and
37- not definition ._name .is_import () and
3855 definition .is_definition () and
3956 not definition .in_builtin_module () and
4057 _kind (definition ) is not None
4158 ):
59+ if definition ._name .is_import ():
60+ return None if hide_imports else False
4261 # for `statement`, we do not enumerate its child nodes. It tends to cause Error.
4362 return definition .type not in ("statement" ,)
4463 return None
0 commit comments