From cf59b8f558cc8121c5fe2de6db70ab91560e2da8 Mon Sep 17 00:00:00 2001 From: Carlos Cordoba Date: Tue, 3 Nov 2020 19:17:30 -0800 Subject: [PATCH] Pass document path to jedi_names when a file is not placed in a module (#882) --- pyls/plugins/symbols.py | 10 +++++++++- pyls/workspace.py | 6 +++--- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/pyls/plugins/symbols.py b/pyls/plugins/symbols.py index 0acf6292..6468dd83 100644 --- a/pyls/plugins/symbols.py +++ b/pyls/plugins/symbols.py @@ -1,5 +1,7 @@ # Copyright 2017 Palantir Technologies, Inc. import logging +import os + from pyls import hookimpl from pyls.lsp import SymbolKind @@ -15,7 +17,13 @@ def pyls_document_symbols(config, document): symbols_settings = config.plugin_settings('jedi_symbols') all_scopes = symbols_settings.get('all_scopes', True) add_import_symbols = symbols_settings.get('include_import_symbols', True) - definitions = document.jedi_names(all_scopes=all_scopes) + + use_document_path = False + document_dir = os.path.normpath(os.path.dirname(document.path)) + if not os.path.isfile(os.path.join(document_dir, '__init__.py')): + use_document_path = True + + definitions = document.jedi_names(use_document_path, all_scopes=all_scopes) module_name = document.dot_path symbols = [] exclude = set({}) diff --git a/pyls/workspace.py b/pyls/workspace.py index 00c3b779..78674d31 100644 --- a/pyls/workspace.py +++ b/pyls/workspace.py @@ -234,8 +234,8 @@ def word_at_position(self, position): return m_start[0] + m_end[-1] @lock - def jedi_names(self, all_scopes=False, definitions=True, references=False): - script = self.jedi_script() + def jedi_names(self, use_document_path, all_scopes=False, definitions=True, references=False): + script = self.jedi_script(use_document_path=use_document_path) return script.get_names(all_scopes=all_scopes, definitions=definitions, references=references) @@ -263,7 +263,7 @@ def jedi_script(self, position=None, use_document_path=False): # Extend sys_path with document's path if requested if use_document_path: - sys_path += [os.path.dirname(self.path)] + sys_path += [os.path.normpath(os.path.dirname(self.path))] kwargs = { 'code': self.source,