1
0
forked from VimPlug/jedi

Start merging efforts for project search and file search

First project tests are passing
This commit is contained in:
Dave Halter
2020-03-06 13:32:04 +01:00
parent e6bdaea73e
commit 6e3bd38600
4 changed files with 75 additions and 26 deletions

View File

@@ -26,7 +26,7 @@ from jedi.api import classes
from jedi.api import interpreter from jedi.api import interpreter
from jedi.api import helpers from jedi.api import helpers
from jedi.api.helpers import validate_line_column from jedi.api.helpers import validate_line_column
from jedi.api.completion import Completion, complete_trailer from jedi.api.completion import Completion, search_in_module
from jedi.api.keywords import KeywordName from jedi.api.keywords import KeywordName
from jedi.api.environment import InterpreterEnvironment from jedi.api.environment import InterpreterEnvironment
from jedi.api.project import get_default_project, Project from jedi.api.project import get_default_project, Project
@@ -354,27 +354,17 @@ class Script(object):
@to_list @to_list
def _search(self, string, complete=False, all_scopes=False, def _search(self, string, complete=False, all_scopes=False,
fuzzy=False): fuzzy=False):
wanted_type, wanted_names = helpers.split_search_string(string)
names = self._names(all_scopes=all_scopes) names = self._names(all_scopes=all_scopes)
for s in wanted_names[:-1]: wanted_type, wanted_names = helpers.split_search_string(string)
new_names = [] return search_in_module(
for n in names: self._inference_state,
if s == n.string_name: self._get_module_context(),
new_names += complete_trailer( names=names,
self._get_module_context(), wanted_type=wanted_type,
n.infer() wanted_names=wanted_names,
) complete=complete,
names = new_names fuzzy=fuzzy,
)
last_name = wanted_names[-1].lower()
for n in names:
string = n.string_name.lower()
if complete and helpers.match(string, last_name, fuzzy=fuzzy) \
or not complete and string == last_name:
def_ = classes.Definition(self._inference_state, n)
if not wanted_type or wanted_type == def_.type:
yield def_
@validate_line_column @validate_line_column
def help(self, line=None, column=None): def help(self, line=None, column=None):

View File

@@ -573,3 +573,25 @@ def _complete_getattr(user_context, instance):
objects = context.infer_node(object_node) objects = context.infer_node(object_node)
return complete_trailer(user_context, objects) return complete_trailer(user_context, objects)
return [] return []
def search_in_module(inference_state, module_context, names, wanted_names,
wanted_type, complete=False, fuzzy=False):
for s in wanted_names[:-1]:
new_names = []
for n in names:
if s == n.string_name:
new_names += complete_trailer(
module_context,
n.infer()
)
names = new_names
last_name = wanted_names[-1].lower()
for n in names:
string = n.string_name.lower()
if complete and helpers.match(string, last_name, fuzzy=fuzzy) \
or not complete and string == last_name:
def_ = classes.Definition(inference_state, n)
if not wanted_type or wanted_type == def_.type:
yield def_

View File

@@ -1,12 +1,14 @@
import os import os
import errno import errno
import json import json
import sys
from jedi._compatibility import FileNotFoundError, PermissionError, IsADirectoryError from jedi._compatibility import FileNotFoundError, PermissionError, IsADirectoryError
from jedi._compatibility import scandir from jedi._compatibility import scandir
from jedi.api.environment import get_cached_default_environment, create_environment from jedi.api.environment import get_cached_default_environment, create_environment
from jedi.api.exceptions import WrongVersion from jedi.api.exceptions import WrongVersion
from jedi.api.classes import Definition from jedi.api.completion import search_in_module
from jedi.api.helpers import split_search_string
from jedi._compatibility import force_unicode from jedi._compatibility import force_unicode
from jedi.inference.sys_path import discover_buildout_paths from jedi.inference.sys_path import discover_buildout_paths
from jedi.inference.cache import inference_state_as_method_param_cache from jedi.inference.cache import inference_state_as_method_param_cache
@@ -176,10 +178,25 @@ class Project(object):
Returns a generator of names Returns a generator of names
""" """
inference_state = InferenceState(self) inference_state = InferenceState(self)
if inference_state.grammar.version_info < (3, 6) or sys.version_info < (3, 6):
raise NotImplementedError(
"No support for refactorings/search on Python 2/3.5"
)
wanted_type, wanted_names = split_search_string(string)
name = wanted_names[0]
file_io_iterator = recurse_find_python_files(FolderIO(self._path)) file_io_iterator = recurse_find_python_files(FolderIO(self._path))
for module_context in search_in_file_ios(inference_state, file_io_iterator, string): for module_context in search_in_file_ios(inference_state, file_io_iterator, name):
for name in get_module_names(module_context.ree_node, all_scopes=all_scopes): names = get_module_names(module_context.tree_node, all_scopes=all_scopes)
yield Definition(inference_state, module_context.create_name(name)) for x in search_in_module(
inference_state,
module_context,
names=[module_context.create_name(n) for n in names],
wanted_type=wanted_type,
wanted_names=wanted_names,
complete=complete
):
yield x # Python 2...
def __repr__(self): def __repr__(self):
return '<%s: %s>' % (self.__class__.__name__, self._path) return '<%s: %s>' % (self.__class__.__name__, self._path)

View File

@@ -1,6 +1,9 @@
import os import os
import sys
from ..helpers import get_example_dir, set_cwd, root_dir import pytest
from ..helpers import get_example_dir, set_cwd, root_dir, test_dir
from jedi import Interpreter from jedi import Interpreter
from jedi.api import Project, get_default_project from jedi.api import Project, get_default_project
@@ -38,3 +41,20 @@ def test_load_save_project(tmpdir):
loaded = Project.load(tmpdir.strpath) loaded = Project.load(tmpdir.strpath)
assert loaded.added_sys_path == ['/foo'] assert loaded.added_sys_path == ['/foo']
@pytest.mark.parametrize(
'string, full_names, kwargs', [
('test_load_save_project', ['test_api.test_project.test_load_save_project'], {}),
('test_load_savep', [], {'complete': True}),
('test_load_save_p', ['test_api.test_project.test_load_save_project'],
dict(complete=True)),
('test_load_save_p', ['test_api.test_project.test_load_save_project'],
dict(complete=True, all_scopes=True)),
]
)
@pytest.mark.skipif(sys.version_info < (3, 6), reason="Ignore Python 2, because EOL")
def test_search(string, full_names, kwargs, skip_pre_python36):
project = Project(test_dir)
defs = project.search(string, **kwargs)
assert [d.full_name for d in defs] == full_names