Skip to content

Commit 6501e9e

Browse files
authored
PR: Include all symbols that Jedi reports as declared in a file when add_import_symbols is False (#261)
1 parent 2c3470c commit 6501e9e

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

pylsp/plugins/symbols.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,9 @@ def pylsp_document_symbols(config, document):
3838

3939
# Skip imported symbols comparing module names.
4040
sym_full_name = d.full_name
41-
document_dot_path = document.dot_path
4241
if sym_full_name is not None:
42+
document_dot_path = document.dot_path
43+
4344
# We assume a symbol is imported from another module to start
4445
# with.
4546
imported_symbol = True
@@ -48,6 +49,8 @@ def pylsp_document_symbols(config, document):
4849
# we need to discard it to do module comparisons below.
4950
if '.' in sym_full_name:
5051
sym_module_name = sym_full_name.rpartition('.')[0]
52+
else:
53+
sym_module_name = sym_full_name
5154

5255
# This is necessary to display symbols in init files (the checks
5356
# below fail without it).
@@ -56,9 +59,9 @@ def pylsp_document_symbols(config, document):
5659

5760
# document_dot_path is the module where the symbol is imported,
5861
# whereas sym_module_name is the one where it was declared.
59-
if sym_module_name.startswith(document_dot_path):
60-
# If sym_module_name starts with the same string as document_dot_path,
61-
# we can safely assume it was declared in the document.
62+
if document_dot_path in sym_module_name:
63+
# If document_dot_path is in sym_module_name, we can safely assume
64+
# that the symbol was declared in the document.
6265
imported_symbol = False
6366
elif sym_module_name.split('.')[0] in document_dot_path.split('.'):
6467
# If the first module in sym_module_name is one of the modules in
@@ -74,10 +77,19 @@ def pylsp_document_symbols(config, document):
7477
# When there's no __init__.py next to a file or in one of its
7578
# parents, the checks above fail. However, Jedi has a nice way
7679
# to tell if the symbol was declared in the same file: if
77-
# full_name starts by __main__.
80+
# sym_module_name starts by __main__.
7881
if imported_symbol:
7982
if not sym_module_name.startswith('__main__'):
8083
continue
84+
else:
85+
# We need to skip symbols if their definition doesn't have `full_name` info, they
86+
# are detected as a definition, but their description (e.g. `class Foo`) doesn't
87+
# match the code where they're detected by Jedi. This happens for relative imports.
88+
if _include_def(d):
89+
if d.description not in d.get_line_code():
90+
continue
91+
else:
92+
continue
8193

8294
try:
8395
docismodule = os.path.samefile(document.path, d.module_path)

0 commit comments

Comments
 (0)