forked from VimPlug/jedi
api.Script.get_definition -> definition, to be consistent in the api naming. deprecated api.Script.get_definition
This commit is contained in:
13
jedi/api.py
13
jedi/api.py
@@ -170,7 +170,7 @@ class Script(object):
|
|||||||
x.word.lower()))
|
x.word.lower()))
|
||||||
|
|
||||||
def _prepare_goto(self, goto_path, is_like_search=False):
|
def _prepare_goto(self, goto_path, is_like_search=False):
|
||||||
""" Base for complete, goto and get_definition. Basically it returns
|
""" Base for complete, goto and definition. Basically it returns
|
||||||
the resolved scopes under cursor. """
|
the resolved scopes under cursor. """
|
||||||
debug.dbg('start: %s in %s' % (goto_path, self._parser.user_scope))
|
debug.dbg('start: %s in %s' % (goto_path, self._parser.user_scope))
|
||||||
|
|
||||||
@@ -201,11 +201,20 @@ class Script(object):
|
|||||||
return stmt
|
return stmt
|
||||||
|
|
||||||
def get_definition(self):
|
def get_definition(self):
|
||||||
|
"""
|
||||||
|
.. deprecated:: 0.5.0
|
||||||
|
Use :attr:`.function_definition` instead.
|
||||||
|
.. todo:: Remove!
|
||||||
|
"""
|
||||||
|
warnings.warn("Use line instead.", DeprecationWarning)
|
||||||
|
return self.definition()
|
||||||
|
|
||||||
|
def definition(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. This is not a
|
||||||
goto function! This follows complicated paths and returns the end, not
|
goto function! This follows complicated paths and returns the end, not
|
||||||
the first definition. The big difference between :meth:`goto` and
|
the first definition. The big difference between :meth:`goto` and
|
||||||
:meth:`get_definition` is that :meth:`goto` doesn't follow imports and
|
:meth:`definition` is that :meth:`goto` doesn't follow imports and
|
||||||
statements. Multiple objects may be returned, because Python itself is
|
statements. Multiple objects may be returned, because Python itself is
|
||||||
a dynamic language, which means depending on an option you can have two
|
a dynamic language, which means depending on an option you can have two
|
||||||
different versions of a function.
|
different versions of a function.
|
||||||
|
|||||||
@@ -263,7 +263,7 @@ 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` or
|
||||||
:meth:`api.Script.get_definition`.
|
:meth:`api.Script.definition`.
|
||||||
"""
|
"""
|
||||||
def __init__(self, definition):
|
def __init__(self, definition):
|
||||||
super(Definition, self).__init__(definition, definition.start_pos)
|
super(Definition, self).__init__(definition, definition.start_pos)
|
||||||
|
|||||||
@@ -32,6 +32,7 @@ sys.argv = sys.argv[:1] + args
|
|||||||
summary = []
|
summary = []
|
||||||
tests_fail = 0
|
tests_fail = 0
|
||||||
|
|
||||||
|
|
||||||
def get_test_list():
|
def get_test_list():
|
||||||
# get test list, that should be executed
|
# get test list, that should be executed
|
||||||
test_files = {}
|
test_files = {}
|
||||||
@@ -46,6 +47,7 @@ def get_test_list():
|
|||||||
last = arg
|
last = arg
|
||||||
return test_files
|
return test_files
|
||||||
|
|
||||||
|
|
||||||
class TestBase(unittest.TestCase):
|
class TestBase(unittest.TestCase):
|
||||||
def get_script(self, src, pos, path=None):
|
def get_script(self, src, pos, path=None):
|
||||||
if pos is None:
|
if pos is None:
|
||||||
@@ -53,9 +55,9 @@ class TestBase(unittest.TestCase):
|
|||||||
pos = len(lines), len(lines[-1])
|
pos = len(lines), len(lines[-1])
|
||||||
return jedi.Script(src, pos[0], pos[1], path)
|
return jedi.Script(src, pos[0], pos[1], path)
|
||||||
|
|
||||||
def get_def(self, src, pos=None):
|
def definition(self, src, pos=None):
|
||||||
script = self.get_script(src, pos)
|
script = self.get_script(src, pos)
|
||||||
return script.get_definition()
|
return script.definition()
|
||||||
|
|
||||||
def complete(self, src, pos=None, path=None):
|
def complete(self, src, pos=None, path=None):
|
||||||
script = self.get_script(src, pos, path)
|
script = self.get_script(src, pos, path)
|
||||||
@@ -69,9 +71,9 @@ class TestBase(unittest.TestCase):
|
|||||||
script = self.get_script(src, pos)
|
script = self.get_script(src, pos)
|
||||||
return script.function_definition()
|
return script.function_definition()
|
||||||
|
|
||||||
|
|
||||||
def print_summary():
|
def print_summary():
|
||||||
print('\nSummary: (%s fails of %s tests) in %.3fs' % \
|
print('\nSummary: (%s fails of %s tests) in %.3fs' % \
|
||||||
(tests_fail, test_sum, time.time() - t_start))
|
(tests_fail, test_sum, time.time() - t_start))
|
||||||
for s in summary:
|
for s in summary:
|
||||||
print(s)
|
print(s)
|
||||||
|
|
||||||
|
|||||||
@@ -41,7 +41,7 @@ class TestRegression(TestBase):
|
|||||||
self.function_definition(s, pos)
|
self.function_definition(s, pos)
|
||||||
assert self.function_definition(s, pos)
|
assert self.function_definition(s, pos)
|
||||||
|
|
||||||
def test_get_definition_cursor(self):
|
def test_definition_cursor(self):
|
||||||
|
|
||||||
s = ("class A():\n"
|
s = ("class A():\n"
|
||||||
" def _something(self):\n"
|
" def _something(self):\n"
|
||||||
@@ -60,7 +60,7 @@ class TestRegression(TestBase):
|
|||||||
diff_line = 4, 10
|
diff_line = 4, 10
|
||||||
should2 = 8, 10
|
should2 = 8, 10
|
||||||
|
|
||||||
get_def = lambda pos: [d.description for d in self.get_def(s, pos)]
|
get_def = lambda pos: [d.description for d in self.definition(s, pos)]
|
||||||
in_name = get_def(in_name)
|
in_name = get_def(in_name)
|
||||||
under_score = get_def(under_score)
|
under_score = get_def(under_score)
|
||||||
should1 = get_def(should1)
|
should1 = get_def(should1)
|
||||||
@@ -71,32 +71,31 @@ class TestRegression(TestBase):
|
|||||||
assert should1 == in_name
|
assert should1 == in_name
|
||||||
assert should1 == under_score
|
assert should1 == under_score
|
||||||
|
|
||||||
#print should2, diff_line
|
|
||||||
assert should2 == diff_line
|
assert should2 == diff_line
|
||||||
|
|
||||||
self.assertRaises(jedi.NotFoundError, get_def, cls)
|
self.assertRaises(jedi.NotFoundError, get_def, cls)
|
||||||
|
|
||||||
def test_keyword_doc(self):
|
def test_keyword_doc(self):
|
||||||
r = list(self.get_def("or", (1, 1)))
|
r = list(self.definition("or", (1, 1)))
|
||||||
assert len(r) == 1
|
assert len(r) == 1
|
||||||
if not is_py25:
|
if not is_py25:
|
||||||
assert len(r[0].doc) > 100
|
assert len(r[0].doc) > 100
|
||||||
|
|
||||||
r = list(self.get_def("asfdasfd", (1, 1)))
|
r = list(self.definition("asfdasfd", (1, 1)))
|
||||||
assert len(r) == 0
|
assert len(r) == 0
|
||||||
|
|
||||||
def test_operator_doc(self):
|
def test_operator_doc(self):
|
||||||
r = list(self.get_def("a == b", (1, 3)))
|
r = list(self.definition("a == b", (1, 3)))
|
||||||
assert len(r) == 1
|
assert len(r) == 1
|
||||||
if not is_py25:
|
if not is_py25:
|
||||||
assert len(r[0].doc) > 100
|
assert len(r[0].doc) > 100
|
||||||
|
|
||||||
def test_get_definition_at_zero(self):
|
def test_definition_at_zero(self):
|
||||||
assert self.get_def("a", (1, 1)) == []
|
assert self.definition("a", (1, 1)) == []
|
||||||
s = self.get_def("str", (1, 1))
|
s = self.definition("str", (1, 1))
|
||||||
assert len(s) == 1
|
assert len(s) == 1
|
||||||
assert list(s)[0].description == 'class str'
|
assert list(s)[0].description == 'class str'
|
||||||
assert self.get_def("", (1, 0)) == []
|
assert self.definition("", (1, 0)) == []
|
||||||
|
|
||||||
def test_complete_at_zero(self):
|
def test_complete_at_zero(self):
|
||||||
s = self.complete("str", (1, 3))
|
s = self.complete("str", (1, 3))
|
||||||
@@ -106,9 +105,9 @@ class TestRegression(TestBase):
|
|||||||
s = self.complete("", (1, 0))
|
s = self.complete("", (1, 0))
|
||||||
assert len(s) > 0
|
assert len(s) > 0
|
||||||
|
|
||||||
def test_get_definition_on_import(self):
|
def test_definition_on_import(self):
|
||||||
assert self.get_def("import sys_blabla", (1, 8)) == []
|
assert self.definition("import sys_blabla", (1, 8)) == []
|
||||||
assert len(self.get_def("import sys", (1, 8))) == 1
|
assert len(self.definition("import sys", (1, 8))) == 1
|
||||||
|
|
||||||
def test_complete_on_empty_import(self):
|
def test_complete_on_empty_import(self):
|
||||||
# should just list the files in the directory
|
# should just list the files in the directory
|
||||||
@@ -222,15 +221,15 @@ class TestRegression(TestBase):
|
|||||||
# .parser to load the module
|
# .parser to load the module
|
||||||
api.modules.Module(os.path.abspath('dynamic.py'), src2).parser
|
api.modules.Module(os.path.abspath('dynamic.py'), src2).parser
|
||||||
script = jedi.Script(src1, 1, len(src1), '../setup.py')
|
script = jedi.Script(src1, 1, len(src1), '../setup.py')
|
||||||
result = script.get_definition()
|
result = script.definition()
|
||||||
assert len(result) == 1
|
assert len(result) == 1
|
||||||
assert result[0].description == 'class int'
|
assert result[0].description == 'class int'
|
||||||
|
|
||||||
def test_named_import(self):
|
def test_named_import(self):
|
||||||
""" named import - jedi-vim issue #8 """
|
""" named import - jedi-vim issue #8 """
|
||||||
s = "import time as dt"
|
s = "import time as dt"
|
||||||
assert len(jedi.Script(s, 1, 15, '/').get_definition()) == 1
|
assert len(jedi.Script(s, 1, 15, '/').definition()) == 1
|
||||||
assert len(jedi.Script(s, 1, 10, '/').get_definition()) == 1
|
assert len(jedi.Script(s, 1, 10, '/').definition()) == 1
|
||||||
|
|
||||||
def test_unicode_script(self):
|
def test_unicode_script(self):
|
||||||
""" normally no unicode objects are being used. (<=2.7) """
|
""" normally no unicode objects are being used. (<=2.7) """
|
||||||
@@ -275,10 +274,10 @@ class TestRegression(TestBase):
|
|||||||
|
|
||||||
def test_keyword_definition_doc(self):
|
def test_keyword_definition_doc(self):
|
||||||
""" github jedi-vim issue #44 """
|
""" github jedi-vim issue #44 """
|
||||||
defs = self.get_def("print")
|
defs = self.definition("print")
|
||||||
assert [d.doc for d in defs]
|
assert [d.doc for d in defs]
|
||||||
|
|
||||||
defs = self.get_def("import")
|
defs = self.definition("import")
|
||||||
assert len(defs) == 1
|
assert len(defs) == 1
|
||||||
assert [d.doc for d in defs]
|
assert [d.doc for d in defs]
|
||||||
|
|
||||||
@@ -325,7 +324,7 @@ class TestFeature(TestBase):
|
|||||||
assert self.complete('import os; os.path.join')[0].full_name \
|
assert self.complete('import os; os.path.join')[0].full_name \
|
||||||
== 'os.path.join'
|
== 'os.path.join'
|
||||||
# issue #94
|
# issue #94
|
||||||
defs = self.get_def("""import os; os.path.join(""")
|
defs = self.definition("""import os; os.path.join(""")
|
||||||
assert defs[0].full_name is None
|
assert defs[0].full_name is None
|
||||||
|
|
||||||
def test_full_name_builtin(self):
|
def test_full_name_builtin(self):
|
||||||
@@ -336,7 +335,7 @@ class TestFeature(TestBase):
|
|||||||
import re
|
import re
|
||||||
any_re = re.compile('.*')
|
any_re = re.compile('.*')
|
||||||
any_re"""
|
any_re"""
|
||||||
self.assertEqual(self.get_def(s)[0].full_name, 're.RegexObject')
|
self.assertEqual(self.definition(s)[0].full_name, 're.RegexObject')
|
||||||
|
|
||||||
def test_quick_completion(self):
|
def test_quick_completion(self):
|
||||||
sources = [
|
sources = [
|
||||||
|
|||||||
10
test/run.py
10
test/run.py
@@ -36,7 +36,7 @@ def run_definition_test(script, should_str, line_nr):
|
|||||||
Runs tests for definitions.
|
Runs tests for definitions.
|
||||||
Return if the test was a fail or not, with 1 for fail and 0 for success.
|
Return if the test was a fail or not, with 1 for fail and 0 for success.
|
||||||
"""
|
"""
|
||||||
result = script.get_definition()
|
result = script.definition()
|
||||||
is_str = set(r.desc_with_module for r in result)
|
is_str = set(r.desc_with_module for r in result)
|
||||||
if is_str != should_str:
|
if is_str != should_str:
|
||||||
print('Solution @%s not right, received %s, wanted %s' \
|
print('Solution @%s not right, received %s, wanted %s' \
|
||||||
@@ -120,10 +120,10 @@ def run_test(source, f_name, lines_to_execute):
|
|||||||
>>> #? int()
|
>>> #? int()
|
||||||
>>> ab = 3; ab
|
>>> ab = 3; ab
|
||||||
"""
|
"""
|
||||||
def get_defs(correct, correct_start, path):
|
def definition(correct, correct_start, path):
|
||||||
def defs(line_nr, indent):
|
def defs(line_nr, indent):
|
||||||
script = jedi.Script(source, line_nr, indent, path)
|
script = jedi.Script(source, line_nr, indent, path)
|
||||||
return set(script.get_definition())
|
return set(script.definition())
|
||||||
|
|
||||||
should_be = set()
|
should_be = set()
|
||||||
number = 0
|
number = 0
|
||||||
@@ -166,7 +166,7 @@ def run_test(source, f_name, lines_to_execute):
|
|||||||
else:
|
else:
|
||||||
index = len(line) - 1 # -1 for the \n
|
index = len(line) - 1 # -1 for the \n
|
||||||
# if a list is wanted, use the completion test, otherwise the
|
# if a list is wanted, use the completion test, otherwise the
|
||||||
# get_definition test
|
# definition test
|
||||||
path = completion_test_dir + os.path.sep + f_name
|
path = completion_test_dir + os.path.sep + f_name
|
||||||
try:
|
try:
|
||||||
script = jedi.Script(source, line_nr, index, path)
|
script = jedi.Script(source, line_nr, index, path)
|
||||||
@@ -177,7 +177,7 @@ def run_test(source, f_name, lines_to_execute):
|
|||||||
elif correct.startswith('['):
|
elif correct.startswith('['):
|
||||||
fails += run_completion_test(script, correct, line_nr)
|
fails += run_completion_test(script, correct, line_nr)
|
||||||
else:
|
else:
|
||||||
should_str = get_defs(correct, start, path)
|
should_str = definition(correct, start, path)
|
||||||
fails += run_definition_test(script, should_str, line_nr)
|
fails += run_definition_test(script, should_str, line_nr)
|
||||||
except Exception:
|
except Exception:
|
||||||
print(traceback.format_exc())
|
print(traceback.format_exc())
|
||||||
|
|||||||
Reference in New Issue
Block a user