usages -> find_references

This commit is contained in:
Dave Halter
2019-12-20 19:23:26 +01:00
parent adff6d34a4
commit e1d787821b
12 changed files with 26 additions and 25 deletions

View File

@@ -32,7 +32,7 @@ Jedi has a focus on autocompletion and goto functionality. Jedi is fast and is
very well tested. It understands Python and stubs on a deep level. very well tested. It understands Python and stubs on a deep level.
Jedi has support for different goto functions. It's possible to search for Jedi has support for different goto functions. It's possible to search for
usages and list names in a Python file to get information about them. references and list names in a Python file to get information about them.
Jedi uses a very simple API to connect with IDE's. There's a reference Jedi uses a very simple API to connect with IDE's. There's a reference
implementation as a `VIM-Plugin <https://github.com/davidhalter/jedi-vim>`_, implementation as a `VIM-Plugin <https://github.com/davidhalter/jedi-vim>`_,

View File

@@ -4,7 +4,7 @@ Jedi has a focus on autocompletion and goto functionality. Jedi is fast and is
very well tested. It understands Python and stubs on a deep level. very well tested. It understands Python and stubs on a deep level.
Jedi has support for different goto functions. It's possible to search for Jedi has support for different goto functions. It's possible to search for
usages and list names in a Python file to get information about them. references and list names in a Python file to get information about them.
Jedi uses a very simple API to connect with IDE's. There's a reference Jedi uses a very simple API to connect with IDE's. There's a reference
implementation as a `VIM-Plugin <https://github.com/davidhalter/jedi-vim>`_, implementation as a `VIM-Plugin <https://github.com/davidhalter/jedi-vim>`_,

View File

@@ -333,27 +333,27 @@ class Script(object):
""" """
Return :class:`classes.Definition` objects, which contain all Return :class:`classes.Definition` objects, which contain all
names that point to the definition of the name under the cursor. This names that point to the definition of the name under the cursor. This
is very useful for refactoring (renaming), or to show all usages of a is very useful for refactoring (renaming), or to show all references of
variable. a variable.
:param include_builtins: Default True, checks if a usage is a builtin :param include_builtins: Default True, checks if a reference is a
(e.g. ``sys``) and in that case does not return it. builtin (e.g. ``sys``) and in that case does not return it.
:rtype: list of :class:`classes.Definition` :rtype: list of :class:`classes.Definition`
""" """
def _usages(include_builtins=True): def _references(include_builtins=True):
tree_name = self._module_node.get_name_of_position((line, column)) tree_name = self._module_node.get_name_of_position((line, column))
if tree_name is None: if tree_name is None:
# Must be syntax # Must be syntax
return [] return []
names = usages.usages(self._get_module_context(), tree_name) names = usages.find_references(self._get_module_context(), tree_name)
definitions = [classes.Definition(self._inference_state, n) for n in names] definitions = [classes.Definition(self._inference_state, n) for n in names]
if not include_builtins: if not include_builtins:
definitions = [d for d in definitions if not d.in_builtin_module()] definitions = [d for d in definitions if not d.in_builtin_module()]
return helpers.sorted_definitions(definitions) return helpers.sorted_definitions(definitions)
return _usages(**kwargs) return _references(**kwargs)
def call_signatures(self): def call_signatures(self):
# Deprecated, will be removed. # Deprecated, will be removed.

View File

@@ -74,7 +74,7 @@ def dynamic_param_lookup(function_value, param_index):
path = function_value.get_root_context().py__file__() path = function_value.get_root_context().py__file__()
if path is not None and is_stdlib_path(path): if path is not None and is_stdlib_path(path):
# We don't want to search for usages in the stdlib. Usually people # We don't want to search for references in the stdlib. Usually people
# don't work with it (except if you are a core maintainer, sorry). # don't work with it (except if you are a core maintainer, sorry).
# This makes everything slower. Just disable it and run the tests, # This makes everything slower. Just disable it and run the tests,
# you will see the slowdown, especially in 3.6. # you will see the slowdown, especially in 3.6.

View File

@@ -32,7 +32,7 @@ def _find_names(module_context, tree_name):
return _dictionarize(_resolve_names(found_names)) return _dictionarize(_resolve_names(found_names))
def usages(module_context, tree_name): def find_references(module_context, tree_name):
search_name = tree_name.value search_name = tree_name.value
found_names = _find_names(module_context, tree_name) found_names = _find_names(module_context, tree_name)
module_contexts = set(d.get_root_context() for d in found_names.values()) module_contexts = set(d.get_root_context() for d in found_names.values())

View File

@@ -57,7 +57,7 @@ def rename(script, new_name):
:param script: The source Script object. :param script: The source Script object.
:return: list of changed lines/changed files :return: list of changed lines/changed files
""" """
return Refactoring(_rename(script.usages(), new_name)) return Refactoring(_rename(script.find_references(), new_name))
def _rename(names, replace_str): def _rename(names, replace_str):
@@ -169,8 +169,8 @@ def inline(script):
definitions = script.goto() definitions = script.goto()
assert len(definitions) == 1 assert len(definitions) == 1
stmt = definitions[0]._definition stmt = definitions[0]._definition
usages = script.usages() references = script.find_references()
inlines = [r for r in usages inlines = [r for r in references
if not stmt.start_pos <= (r.line, r.column) <= stmt.end_pos] if not stmt.start_pos <= (r.line, r.column) <= stmt.end_pos]
inlines = sorted(inlines, key=lambda x: (x.module_path, x.line, x.column), inlines = sorted(inlines, key=lambda x: (x.module_path, x.line, x.column),
reverse=True) reverse=True)

View File

@@ -1,5 +1,5 @@
""" """
Renaming tests. This means search for usages. Renaming tests. This means search for references.
I always leave a little bit of space to add room for additions, because the I always leave a little bit of space to add room for additions, because the
results always contain position informations. results always contain position informations.
""" """

View File

@@ -1,6 +1,6 @@
""" """
Test coverage for renaming is mostly being done by testing Test coverage for renaming is mostly being done by testing
`Script.usages`. `Script.find_references`.
""" """
# --- simple # --- simple

View File

@@ -1,4 +1,4 @@
""" """
An import tree, for testing usages. An import tree, for testing references.
""" """

View File

@@ -162,8 +162,8 @@ def test_goto_definition_not_multiple(Script):
assert len(Script(s).infer()) == 1 assert len(Script(s).infer()) == 1
def test_usage_description(Script): def test_reference_description(Script):
descs = [u.description for u in Script("foo = ''; foo").usages()] descs = [u.description for u in Script("foo = ''; foo").find_references()]
assert set(descs) == {"foo = ''", 'foo'} assert set(descs) == {"foo = ''", 'foo'}

View File

@@ -51,8 +51,8 @@ def test_basedefinition_type(Script, names):
script = Script(source, path=None) script = Script(source, path=None)
definitions += script.infer(len(lines), len('variable')) definitions += script.infer(len(lines), len('variable'))
script2 = Script(source, 4, len('class C'), None) script2 = Script(source, path=None)
definitions += script2.usages() definitions += script2.find_references(4, len('class C'))
source_param = "def f(a): return a" source_param = "def f(a): return a"
script_param = Script(source_param, path=None) script_param = Script(source_param, path=None)

View File

@@ -1,11 +1,12 @@
def test_import_usage(Script): def test_import_references(Script):
s = Script("from .. import foo", line=1, column=18, path="foo.py") s = Script("from .. import foo", path="foo.py")
assert [usage.line for usage in s.usages()] == [1] assert [usage.line for usage in s.find_references(line=1, column=18)] == [1]
def test_exclude_builtin_modules(Script): def test_exclude_builtin_modules(Script):
def get(include): def get(include):
return [(d.line, d.column) for d in Script(source, column=8).usages(include_builtins=include)] references = Script(source).find_references(column=8, include_builtins=include)
return [(d.line, d.column) for d in references]
source = '''import sys\nprint(sys.path)''' source = '''import sys\nprint(sys.path)'''
places = get(include=True) places = get(include=True)
assert len(places) > 2 # Includes stubs assert len(places) > 2 # Includes stubs