removed a lot of the goto usages, used goto_assignments instead. Did the same also for a lot of definition uses

This commit is contained in:
David Halter
2013-05-03 21:26:11 +04:30
parent edd0a08351
commit 7c8fee1257
10 changed files with 41 additions and 40 deletions

View File

@@ -37,7 +37,7 @@ class NotFoundError(Exception):
class Script(object): class Script(object):
""" """
A Script is the base for a completion, goto or whatever you want to do with A Script is the base for completions, goto or whatever you want to do with
|jedi|. |jedi|.
:param source: The source code of the current file, separated by newlines. :param source: The source code of the current file, separated by newlines.
@@ -264,13 +264,13 @@ class Script(object):
@api_classes._clear_caches_after_call @api_classes._clear_caches_after_call
def goto_definitions(self): def goto_definitions(self):
""" """
Return the definitions of a the path under the cursor. This is not a Return the definitions of a the path under the cursor. goto function!
goto function! This follows complicated paths and returns the end, not This follows complicated paths and returns the end, not the first
the first definition. The big difference between :meth:`goto` and definition. The big difference between :meth:`goto_assignments` and
:meth:`definition` is that :meth:`goto` doesn't follow imports and :meth:`goto_definitions` is that :meth:`goto_assignments` doesn't
statements. Multiple objects may be returned, because Python itself is follow imports and statements. Multiple objects may be returned,
a dynamic language, which means depending on an option you can have two because Python itself is a dynamic language, which means depending on
different versions of a function. an option you can have two different versions of a function.
:rtype: list of :class:`api_classes.Definition` :rtype: list of :class:`api_classes.Definition`
""" """
@@ -328,10 +328,10 @@ class Script(object):
@api_classes._clear_caches_after_call @api_classes._clear_caches_after_call
def goto_assignments(self): def goto_assignments(self):
""" """
Return the first definition found by goto. Imports and statements Return the first definition found. Imports and statements aren't
aren't followed. Multiple objects may be returned, because Python followed. Multiple objects may be returned, because Python itself is a
itself is a dynamic language, which means depending on an option you dynamic language, which means depending on an option you can have two
can have two different versions of a function. different versions of a function.
:rtype: list of :class:`api_classes.Definition` :rtype: list of :class:`api_classes.Definition`
""" """
@@ -340,7 +340,8 @@ class Script(object):
def _goto(self, add_import_name=False): def _goto(self, add_import_name=False):
""" """
Used for goto and usages. Used for goto_assignments and usages.
:param add_import_name: TODO add description :param add_import_name: TODO add description
""" """
def follow_inexistent_imports(defs): def follow_inexistent_imports(defs):

View File

@@ -415,8 +415,8 @@ class Completion(BaseDefinition):
class Definition(BaseDefinition): class Definition(BaseDefinition):
""" """
*Definition* objects are returned from :meth:`api.Script.goto` or *Definition* objects are returned from :meth:`api.Script.goto_assignments`
:meth:`api.Script.definition`. or :meth:`api.Script.goto_definitions`.
""" """
def __init__(self, definition): def __init__(self, definition):
super(Definition, self).__init__(definition, definition.start_pos) super(Definition, self).__init__(definition, definition.start_pos)

View File

@@ -359,7 +359,7 @@ class SubModule(Scope, Module):
@property @property
def name(self): def name(self):
""" This is used for the goto function. """ """ This is used for the goto functions. """
if self._name is not None: if self._name is not None:
return self._name return self._name
if self.path is None: if self.path is None:

View File

@@ -32,9 +32,9 @@ class TestBase(unittest.TestCase):
script = self.get_script(src, pos, path) script = self.get_script(src, pos, path)
return script.completions() return script.completions()
def goto(self, src, pos=None): def goto_assignments(self, src, pos=None):
script = self.get_script(src, pos) script = self.get_script(src, pos)
return script.goto() return script.goto_assignments()
def function_definition(self, src, pos=None): def function_definition(self, src, pos=None):
script = self.get_script(src, pos) script = self.get_script(src, pos)

View File

@@ -1,4 +1,4 @@
# goto command tests are a different in syntax # goto_assignments command tests are different in syntax
definition = 3 definition = 3
#! 0 ['a = definition'] #! 0 ['a = definition']

View File

@@ -1,5 +1,5 @@
""" """
Renaming tests. This means search for related names. Renaming tests. This means search for usages.
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

@@ -10,8 +10,7 @@ el.description
scopes, path, dot, like = \ scopes, path, dot, like = \
functions.prepare_goto(source, row, column, api._prepare_goto(source, row, column, source_path, True)
source_path, True)
# has problems with that (sometimes) very deep nesting. # has problems with that (sometimes) very deep nesting.
#? set() #? set()

View File

@@ -10,9 +10,9 @@ tests.
There are different kind of tests: There are different kind of tests:
- completions / definitions ``#?`` - completions / goto_definitions ``#?``
- goto: ``#!`` - goto_assignments: ``#!``
- related names: ``#<`` - usages: ``#<``
How to run tests? How to run tests?
+++++++++++++++++ +++++++++++++++++
@@ -63,8 +63,8 @@ For example::
Because it follows ``a.rea`` and a is an ``int``, which has a ``real`` Because it follows ``a.rea`` and a is an ``int``, which has a ``real``
property. property.
Definition Goto Definitions
++++++++++ +++++++++++++++
Definition tests use the same symbols like completion tests. This is Definition tests use the same symbols like completion tests. This is
possible because the completion tests are defined with a list:: possible because the completion tests are defined with a list::
@@ -72,8 +72,8 @@ possible because the completion tests are defined with a list::
#? int() #? int()
ab = 3; ab ab = 3; ab
Goto Goto Assignments
++++ ++++++++++++++++
Tests look like this:: Tests look like this::
@@ -87,7 +87,7 @@ the test (otherwise it's just end of line)::
#! 2 ['abc=1'] #! 2 ['abc=1']
abc abc
Related Names Usages
+++++++++++++ +++++++++++++
Tests look like this:: Tests look like this::
@@ -141,8 +141,8 @@ class IntegrationTestCase(object):
def run(self, compare_cb): def run(self, compare_cb):
testers = { testers = {
TEST_COMPLETIONS: self.run_completion, TEST_COMPLETIONS: self.run_completion,
TEST_DEFINITIONS: self.run_definition, TEST_DEFINITIONS: self.run_goto_definitions,
TEST_ASSIGNMENTS: self.run_goto, TEST_ASSIGNMENTS: self.run_goto_assignments,
TEST_USAGES: self.run_usages, TEST_USAGES: self.run_usages,
} }
return testers[self.test_type](compare_cb) return testers[self.test_type](compare_cb)
@@ -154,11 +154,11 @@ class IntegrationTestCase(object):
comp_str = set([c.name for c in completions]) comp_str = set([c.name for c in completions])
return compare_cb(self, comp_str, set(literal_eval(self.correct))) return compare_cb(self, comp_str, set(literal_eval(self.correct)))
def run_definition(self, compare_cb): def run_goto_definitions(self, compare_cb):
def definition(correct, correct_start, path): def definition(correct, correct_start, path):
def defs(line_nr, indent): def defs(line_nr, indent):
s = jedi.Script(self.source, line_nr, indent, path) s = jedi.Script(self.source, line_nr, indent, path)
return set(s.definition()) return set(s.goto_definitions())
should_be = set() should_be = set()
number = 0 number = 0
@@ -183,12 +183,12 @@ class IntegrationTestCase(object):
script = self.script() script = self.script()
should_str = definition(self.correct, self.start, script.source_path) should_str = definition(self.correct, self.start, script.source_path)
result = script.definition() result = script.goto_definitions()
is_str = set(r.desc_with_module for r in result) is_str = set(r.desc_with_module for r in result)
return compare_cb(self, is_str, should_str) return compare_cb(self, is_str, should_str)
def run_goto(self, compare_cb): def run_goto_assignments(self, compare_cb):
result = self.script().goto() result = self.script().goto_assignments()
comp_str = str(sorted(str(r.description) for r in result)) comp_str = str(sorted(str(r.description) for r in result))
return compare_cb(self, comp_str, self.correct) return compare_cb(self, comp_str, self.correct)
@@ -242,7 +242,7 @@ def collect_file_tests(lines, lines_to_execute):
else: else:
try: try:
r = re.search(r'(?:^|(?<=\s))#([?!<])\s*([^\n]+)', line) r = re.search(r'(?:^|(?<=\s))#([?!<])\s*([^\n]+)', line)
# test_type is ? for completion and ! for goto # test_type is ? for completion and ! for goto_assignments
test_type = r.group(1) test_type = r.group(1)
correct = r.group(2) correct = r.group(2)
start = r.start() start = r.start()

View File

@@ -42,7 +42,7 @@ def make_definitions():
source_param = "def f(a): return a" source_param = "def f(a): return a"
script_param = api.Script(source_param, 1, len(source_param), None) script_param = api.Script(source_param, 1, len(source_param), None)
definitions += script_param.goto() definitions += script_param.goto_assignments()
return definitions return definitions

View File

@@ -336,7 +336,8 @@ class TestRegression(TestBase):
assert [d.doc for d in defs] assert [d.doc for d in defs]
def test_goto_following_on_imports(self): def test_goto_following_on_imports(self):
g = self.goto("import multiprocessing.dummy; multiprocessing.dummy") s = "import multiprocessing.dummy; multiprocessing.dummy"
g = self.goto_assignments(s)
assert len(g) == 1 assert len(g) == 1
assert g[0].start_pos != (0, 0) assert g[0].start_pos != (0, 0)