Skip to content

Commit

Permalink
Keep legacy documentSymbol implementation working.
Browse files Browse the repository at this point in the history
... depends on whether LSP client supports hierarchicalDocumentSymbol.
  • Loading branch information
CXuesong committed Apr 7, 2019
1 parent 024c036 commit 8ceb315
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
27 changes: 23 additions & 4 deletions pyls/plugins/symbols.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@

@hookimpl
def pyls_document_symbols(config, document):
# all_scopes = config.plugin_settings('jedi_symbols').get('all_scopes', True)
if not config.capabilities.get("documentSymbol", {}).get("hierarchicalDocumentSymbolSupport", False):
return pyls_document_symbols_legacy(config, document)
# returns DocumentSymbol[]
hide_imports = config.plugin_settings('jedi_symbols').get('hide_imports', False)
definitions = document.jedi_names(all_scopes=False)
def transform(d):
include_d = _include_def(d)
include_d = _include_def(d, hide_imports)
if include_d is None:
return None
children = [dt for dt in (transform(d1) for d1 in d.defined_names()) if dt] if include_d else None
Expand All @@ -28,17 +31,33 @@ def transform(d):
}
return [dt for dt in (transform(d) for d in definitions) if dt]

def _include_def(definition):
def pyls_document_symbols_legacy(config, document):
# returns SymbolInformation[]
all_scopes = config.plugin_settings('jedi_symbols').get('all_scopes', True)
hide_imports = config.plugin_settings('jedi_symbols').get('hide_imports', False)
definitions = document.jedi_names(all_scopes=all_scopes)
return [{
'name': d.name,
'containerName': _container(d),
'location': {
'uri': document.uri,
'range': _range(d),
},
'kind': _kind(d),
} for d in definitions if _include_def(d, hide_imports) is not None]

def _include_def(definition, hide_imports=True):
# True: include def and sub-defs
# False: include def but do not include sub-defs
# None: Do not include def or sub-defs
if (# Unused vars should also be skipped
definition.name != '_' and
not definition._name.is_import() and
definition.is_definition() and
not definition.in_builtin_module() and
_kind(definition) is not None
):
if definition._name.is_import():
return None if hide_imports else False
# for `statement`, we do not enumerate its child nodes. It tends to cause Error.
return definition.type not in ("statement",)
return None
Expand Down
2 changes: 1 addition & 1 deletion test/plugins/test_symbols.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def main(x):

def test_symbols(config):
doc = Document(DOC_URI, DOC)
config.update({'plugins': {'jedi_symbols': {'all_scopes': False}}})
config.update({'plugins': {'jedi_symbols': {'all_scopes': False, 'hide_imports': False}}})
symbols = pyls_document_symbols(config, doc)

# All four symbols (import sys, a, B, main, y)
Expand Down

0 comments on commit 8ceb315

Please sign in to comment.