@@ -19,28 +19,46 @@ 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 )
43- except TypeError :
61+ except ( TypeError , FileNotFoundError ) :
4462 # Python 2 on Windows has no .samefile, but then these are
4563 # strings for sure
4664 docismodule = document .path == d .module_path
0 commit comments