Skip to content

Commit bb9f830

Browse files
committed
Fix skipping imported symbols
1 parent 8e53e96 commit bb9f830

File tree

2 files changed

+31
-13
lines changed

2 files changed

+31
-13
lines changed

Diff for: pylsp/plugins/symbols.py

+29-11
Original file line numberDiff line numberDiff line change
@@ -19,25 +19,43 @@ def pylsp_document_symbols(config, document):
1919
symbols_settings = config.plugin_settings('jedi_symbols')
2020
all_scopes = symbols_settings.get('all_scopes', True)
2121
add_import_symbols = symbols_settings.get('include_import_symbols', True)
22-
23-
use_document_path = False
24-
document_dir = os.path.normpath(os.path.dirname(document.path))
25-
if not os.path.isfile(os.path.join(document_dir, '__init__.py')):
26-
use_document_path = True
27-
28-
definitions = document.jedi_names(use_document_path, all_scopes=all_scopes)
29-
module_name = document.dot_path
22+
definitions = document.jedi_names(all_scopes=all_scopes)
3023
symbols = []
3124
exclude = set({})
3225
redefinitions = {}
3326
while definitions != []:
3427
d = definitions.pop(0)
28+
29+
# Skip symbols imported from other modules.
3530
if not add_import_symbols:
31+
# Skip if there's an import in the code the symbol is defined.
32+
code = d.get_line_code()
33+
if ' import ' in code or 'import ' in code:
34+
continue
35+
36+
# Skip comparing module names.
3637
sym_full_name = d.full_name
38+
module_name = document.dot_path
3739
if sym_full_name is not None:
38-
if (not sym_full_name.startswith(module_name) and
39-
not sym_full_name.startswith('__main__')):
40-
continue
40+
# module_name returns where the symbol is imported, whereas
41+
# full_name says where it really comes from. So if the parent
42+
# modules in full_name are not in module_name, it means the
43+
# symbol was not defined there.
44+
# Note: The last element of sym_full_name is the symbol itself,
45+
# so we don't need to use it below.
46+
imported_symbol = True
47+
for mod in sym_full_name.split('.')[:-1]:
48+
if mod in module_name:
49+
imported_symbol = False
50+
51+
# When there's no __init__.py next to a file or in one of its
52+
# parents, the check above fails. However, Jedi has a nice way
53+
# to tell if the symbol was declared in the same file: if
54+
# full_name starts by __main__.
55+
if imported_symbol:
56+
if not sym_full_name.startswith('__main__'):
57+
continue
58+
4159
try:
4260
docismodule = os.path.samefile(document.path, d.module_path)
4361
except (TypeError, FileNotFoundError):

Diff for: pylsp/workspace.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -238,8 +238,8 @@ def word_at_position(self, position):
238238
return m_start[0] + m_end[-1]
239239

240240
@lock
241-
def jedi_names(self, use_document_path, all_scopes=False, definitions=True, references=False):
242-
script = self.jedi_script(use_document_path=use_document_path)
241+
def jedi_names(self, all_scopes=False, definitions=True, references=False):
242+
script = self.jedi_script()
243243
return script.get_names(all_scopes=all_scopes, definitions=definitions,
244244
references=references)
245245

0 commit comments

Comments
 (0)