Skip to content

Commit 0633f62

Browse files
committed
Keep legacy documentSymbol implementation working.
... depends on whether LSP client supports hierarchicalDocumentSymbol.
1 parent a76f2e6 commit 0633f62

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed

pyls/plugins/symbols.py

+23-4
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,13 @@
88

99
@hookimpl
1010
def 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

test/plugins/test_symbols.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def main(x):
2323

2424
def test_symbols(config):
2525
doc = Document(DOC_URI, DOC)
26-
config.update({'plugins': {'jedi_symbols': {'all_scopes': False}}})
26+
config.update({'plugins': {'jedi_symbols': {'all_scopes': False, 'hide_imports' : False}}})
2727
symbols = pyls_document_symbols(config, doc)
2828

2929
# All four symbols (import sys, a, B, main, y)
@@ -48,7 +48,7 @@ def sym(name):
4848
def test_symbols_all_scopes(config):
4949
doc = Document(DOC_URI, DOC)
5050
symbols = pyls_document_symbols(config, doc)
51-
51+
print(symbols)
5252
# All eight symbols (import sys, a, B, __init__, x, y, main, y)
5353
assert len(symbols) == 8
5454

0 commit comments

Comments
 (0)