def project(workspace: Workspace) -> Project: """Simplifies getting jedi project.""" return Project( path=workspace.root_path, smart_sys_path=True, load_unsafe_extensions=False, )
def test_implicit_namespace_package_import_autocomplete(Script): code = 'from implicit_name' project = Project('.', sys_path=[example_dir]) script = Script(code, project=project) compl = script.complete() assert [c.name for c in compl] == ['implicit_namespace_package']
def bf_initialize(self, params: InitializeParams) -> InitializeResult: """Override built-in initialization. Here, we can conditionally register functions to features based on client capabilities and initializationOptions. """ server: "JediLanguageServer" = self._server ip = server.initialize_params # pylint: disable=invalid-name ip.set_initialize_params(params) jedi_utils.set_jedi_settings(ip) if ip.initializationOptions_diagnostics_enable: if ip.initializationOptions_diagnostics_didOpen: server.feature(TEXT_DOCUMENT_DID_OPEN)(did_open) if ip.initializationOptions_diagnostics_didChange: server.feature(TEXT_DOCUMENT_DID_CHANGE)(did_change) if ip.initializationOptions_diagnostics_didSave: server.feature(TEXT_DOCUMENT_DID_SAVE)(did_save) initialize_result = super().bf_initialize(params) server.project = Project( path=server.workspace.root_path, added_sys_path=ip.initializationOptions_workspace_extraPaths, smart_sys_path=True, load_unsafe_extensions=False, ) return initialize_result
def test_wrong_encoding(Script, tmpdir): x = tmpdir.join('x.py') # Use both latin-1 and utf-8 (a really broken file). x.write_binary('foobar = 1\nä'.encode('latin-1') + 'ä'.encode('utf-8')) project = Project(tmpdir.strpath) c, = Script('import x; x.foo', project=project).complete() assert c.name == 'foobar'
def test_namespace_package_in_multiple_directories_goto_definition(Script): code = 'from pkg import ns1_file' sys_path = [get_example_dir('implicit_namespace_package', 'ns1'), get_example_dir('implicit_namespace_package', 'ns2')] project = Project('.', sys_path=sys_path) script = Script(code, project=project) result = script.infer() assert len(result) == 1
def test_nested_namespace_package(Script): code = 'from nested_namespaces.namespace.pkg import CONST' sys_path = [example_dir] project = Project('.', sys_path=sys_path) result = Script(code, project=project).infer(line=1, column=45) assert len(result) == 1
def test_namespace_name_autocompletion_full_name(Script): code = 'from pk' sys_path = [get_example_dir('implicit_namespace_package', 'ns1'), get_example_dir('implicit_namespace_package', 'ns2')] project = Project('.', sys_path=sys_path) script = Script(code, project=project) compl = script.complete() assert set(c.full_name for c in compl) == set(['pkg'])
def test_namespace_package_in_multiple_directories_autocompletion(Script): code = 'from pkg.' sys_path = [get_example_dir('implicit_namespace_package', 'ns1'), get_example_dir('implicit_namespace_package', 'ns2')] project = Project('.', sys_path=sys_path) script = Script(code, project=project) compl = script.complete() assert set(c.name for c in compl) == set(['ns1_file', 'ns2_file'])
def project(workspace: Workspace) -> Project: """Simplifies getting jedi project""" added_sys_path = [] extra_paths = os.getenv("JEDI_LANGUAGE_SERVER_EXTRA_PATHS") if extra_paths: added_sys_path = extra_paths.split(":") return Project(path=workspace.root_path, smart_sys_path=True, load_unsafe_extensions=False, added_sys_path=added_sys_path)
def test_implicit_nested_namespace_package(Script): code = 'from implicit_nested_namespaces.namespace.pkg.module import CONST' project = Project('.', sys_path=[example_dir]) script = Script(code, project=project) result = script.infer(line=1, column=61) assert len(result) == 1 implicit_pkg, = Script(code, project=project).infer(column=10) assert implicit_pkg.type == 'module' assert implicit_pkg.module_path is None
def lsp_initialize(self, params: InitializeParams) -> InitializeResult: """Override built-in initialization. Here, we can conditionally register functions to features based on client capabilities and initializationOptions. """ server: "JediLanguageServer" = self._server try: server.initialization_options = InitializationOptions.parse_obj( {} if params.initialization_options is None else params. initialization_options) except ValidationError as error: msg = f"Invalid InitializationOptions, using defaults: {error}" server.show_message(msg, msg_type=MessageType.Error) server.show_message_log(msg, msg_type=MessageType.Error) server.initialization_options = InitializationOptions() initialization_options = server.initialization_options jedi_utils.set_jedi_settings(initialization_options) # Configure didOpen, didChange, and didSave # currently need to be configured manually diagnostics = initialization_options.diagnostics did_open = (did_open_diagnostics if diagnostics.enable and diagnostics.did_open else did_open_default) did_change = (did_change_diagnostics if diagnostics.enable and diagnostics.did_change else did_change_default) did_save = (did_save_diagnostics if diagnostics.enable and diagnostics.did_save else did_save_default) did_close = (did_close_diagnostics if diagnostics.enable else did_close_default) server.feature(TEXT_DOCUMENT_DID_OPEN)(did_open) server.feature(TEXT_DOCUMENT_DID_CHANGE)(did_change) server.feature(TEXT_DOCUMENT_DID_SAVE)(did_save) server.feature(TEXT_DOCUMENT_DID_CLOSE)(did_close) if server.initialization_options.hover.enable: server.feature(HOVER)(hover) initialize_result: InitializeResult = super().lsp_initialize(params) server.project = (Project( path=server.workspace.root_path, added_sys_path=initialization_options.workspace.extra_paths, smart_sys_path=True, load_unsafe_extensions=False, ) if server.workspace.root_path else None) return initialize_result
def test_implicit_namespace_package(Script): sys_path = [ get_example_dir('implicit_namespace_package', 'ns1'), get_example_dir('implicit_namespace_package', 'ns2') ] project = Project('.', sys_path=sys_path) def script_with_path(*args, **kwargs): return Script(project=project, *args, **kwargs) # goto definition assert script_with_path('from pkg import ns1_file').infer() assert script_with_path('from pkg import ns2_file').infer() assert not script_with_path('from pkg import ns3_file').infer() # goto assignment tests = { 'from pkg.ns2_file import foo': 'ns2_file!', 'from pkg.ns1_file import foo': 'ns1_file!', } for source, solution in tests.items(): ass = script_with_path(source).goto() assert len(ass) == 1 assert ass[0].description == "foo = '%s'" % solution # completion completions = script_with_path('from pkg import ').complete() names = [c.name for c in completions] compare = ['ns1_file', 'ns2_file'] # must at least contain these items, other items are not important assert set(compare) == set(names) tests = { 'from pkg import ns2_file as x': 'ns2_file!', 'from pkg import ns1_file as x': 'ns1_file!' } for source, solution in tests.items(): for c in script_with_path(source + '; x.').complete(): if c.name == 'foo': completion = c solution = "foo = '%s'" % solution assert completion.description == solution
def change_workspace(self, project_path): self.project = (Project(project_path) if project_path and os.path.exists(project_path) else None) self._source = "" self.script = None
def script_with_path(Script, *args, **kwargs): return Script(project=Project('.', sys_path=SYS_PATH), *args, **kwargs)
def test_can_complete_when_shadowing(Script): path = helpers.get_example_dir('absolute_import', 'unittest.py') script = Script(path=path, project=Project( helpers.get_example_dir('absolute_import'))) assert script.complete()
""" A helper module for testing, improves compatibility for testing (as ``jedi._compatibility``) as well as introducing helper functions. """ from contextlib import contextmanager import os import pytest from functools import partial, wraps from jedi import Project from pathlib import Path test_dir = Path(__file__).absolute().parent test_dir_project = Project(test_dir) root_dir = test_dir.parent example_dir = test_dir.joinpath('examples') sample_int = 1 # This is used in completion/imports.py skip_if_windows = partial(pytest.param, marks=pytest.mark.skipif("sys.platform=='win32'")) skip_if_not_windows = partial(pytest.param, marks=pytest.mark.skipif("sys.platform!='win32'")) def get_example_dir(*names): return example_dir.joinpath(*names) def cwd_at(path):
def __init__(self, *, project_path=None): self.project = (Project(project_path) if project_path and os.path.exists(project_path) else None) self._source = "" self.script = None