@@ -19,25 +19,43 @@ def pylsp_document_symbols(config, document):
19
19
symbols_settings = config .plugin_settings ('jedi_symbols' )
20
20
all_scopes = symbols_settings .get ('all_scopes' , True )
21
21
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 )
30
23
symbols = []
31
24
exclude = set ({})
32
25
redefinitions = {}
33
26
while definitions != []:
34
27
d = definitions .pop (0 )
28
+
29
+ # Skip symbols imported from other modules.
35
30
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.
36
37
sym_full_name = d .full_name
38
+ module_name = document .dot_path
37
39
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
+
41
59
try :
42
60
docismodule = os .path .samefile (document .path , d .module_path )
43
61
except (TypeError , FileNotFoundError ):
0 commit comments