mirror of
https://github.com/davidhalter/jedi.git
synced 2025-12-09 23:34:45 +08:00
Script.get_in_function_call -> Script.function_definition
This commit is contained in:
18
jedi/api.py
18
jedi/api.py
@@ -11,6 +11,7 @@ __all__ = ['Script', 'NotFoundError', 'set_debug_function', '_quick_complete']
|
|||||||
|
|
||||||
import re
|
import re
|
||||||
import os
|
import os
|
||||||
|
import warnings
|
||||||
|
|
||||||
import parsing
|
import parsing
|
||||||
import parsing_representation as pr
|
import parsing_representation as pr
|
||||||
@@ -122,7 +123,7 @@ class Script(object):
|
|||||||
completions.append((c, s))
|
completions.append((c, s))
|
||||||
|
|
||||||
if not dot: # named params have no dots
|
if not dot: # named params have no dots
|
||||||
call_def = self.get_in_function_call()
|
call_def = self.function_definition()
|
||||||
if call_def:
|
if call_def:
|
||||||
if not call_def.module.is_builtin():
|
if not call_def.module.is_builtin():
|
||||||
for p in call_def.params:
|
for p in call_def.params:
|
||||||
@@ -336,6 +337,15 @@ class Script(object):
|
|||||||
return sorted(set(names), key=lambda x: (x.module_path, x.start_pos))
|
return sorted(set(names), key=lambda x: (x.module_path, x.start_pos))
|
||||||
|
|
||||||
def get_in_function_call(self):
|
def get_in_function_call(self):
|
||||||
|
"""
|
||||||
|
.. deprecated:: 0.5.0
|
||||||
|
Use :attr:`.function_definition` instead.
|
||||||
|
.. todo:: Remove!
|
||||||
|
"""
|
||||||
|
warnings.warn("Use line instead.", DeprecationWarning)
|
||||||
|
return self.function_definition()
|
||||||
|
|
||||||
|
def function_definition(self):
|
||||||
"""
|
"""
|
||||||
Return the function object of the call you're currently in.
|
Return the function object of the call you're currently in.
|
||||||
|
|
||||||
@@ -391,7 +401,7 @@ class Script(object):
|
|||||||
|
|
||||||
debug.speed('func_call start')
|
debug.speed('func_call start')
|
||||||
call = None
|
call = None
|
||||||
if settings.use_get_in_function_call_cache:
|
if settings.use_function_definition_cache:
|
||||||
try:
|
try:
|
||||||
call, index = check_cache()
|
call, index = check_cache()
|
||||||
except NotFoundError:
|
except NotFoundError:
|
||||||
@@ -405,9 +415,9 @@ class Script(object):
|
|||||||
return None
|
return None
|
||||||
debug.speed('func_call parsed')
|
debug.speed('func_call parsed')
|
||||||
|
|
||||||
with common.scale_speed_settings(settings.scale_get_in_function_call):
|
with common.scale_speed_settings(settings.scale_function_definition):
|
||||||
_callable = lambda: evaluate.follow_call(call)
|
_callable = lambda: evaluate.follow_call(call)
|
||||||
origins = cache.cache_get_in_function_call(_callable, user_stmt)
|
origins = cache.cache_function_definition(_callable, user_stmt)
|
||||||
debug.speed('func_call followed')
|
debug.speed('func_call followed')
|
||||||
|
|
||||||
if len(origins) == 0:
|
if len(origins) == 0:
|
||||||
|
|||||||
@@ -336,9 +336,11 @@ class RelatedName(BaseDefinition):
|
|||||||
|
|
||||||
|
|
||||||
class CallDef(object):
|
class CallDef(object):
|
||||||
""" `CallDef` objects is the return value of `Script.get_in_function_call`.
|
"""
|
||||||
|
`CallDef` objects is the return value of `Script.function_definition`.
|
||||||
It knows what functions you are currently in. e.g. `isinstance(` would
|
It knows what functions you are currently in. e.g. `isinstance(` would
|
||||||
return the `isinstance` function. without `(` it would return nothing."""
|
return the `isinstance` function. without `(` it would return nothing.
|
||||||
|
"""
|
||||||
def __init__(self, executable, index, call):
|
def __init__(self, executable, index, call):
|
||||||
self.executable = executable
|
self.executable = executable
|
||||||
self.index = index
|
self.index = index
|
||||||
|
|||||||
@@ -135,8 +135,8 @@ def time_cache(time_add_setting):
|
|||||||
return _temp
|
return _temp
|
||||||
|
|
||||||
|
|
||||||
@time_cache("get_in_function_call_validity")
|
@time_cache("function_definition_validity")
|
||||||
def cache_get_in_function_call(stmt):
|
def cache_function_definition(stmt):
|
||||||
module_path = stmt.get_parent_until().path
|
module_path = stmt.get_parent_until().path
|
||||||
return None if module_path is None else (module_path, stmt.start_pos)
|
return None if module_path is None else (module_path, stmt.start_pos)
|
||||||
|
|
||||||
|
|||||||
@@ -34,7 +34,7 @@ Parser
|
|||||||
|
|
||||||
.. autodata:: fast_parser
|
.. autodata:: fast_parser
|
||||||
.. autodata:: fast_parser_always_reparse
|
.. autodata:: fast_parser_always_reparse
|
||||||
.. autodata:: use_get_in_function_call_cache
|
.. autodata:: use_function_definition_cache
|
||||||
|
|
||||||
|
|
||||||
Dynamic stuff
|
Dynamic stuff
|
||||||
@@ -66,14 +66,14 @@ definitely worse in some cases. But a completion should also be fast.
|
|||||||
.. autodata:: max_function_recursion_level
|
.. autodata:: max_function_recursion_level
|
||||||
.. autodata:: max_executions_without_builtins
|
.. autodata:: max_executions_without_builtins
|
||||||
.. autodata:: max_executions
|
.. autodata:: max_executions
|
||||||
.. autodata:: scale_get_in_function_call
|
.. autodata:: scale_function_definition
|
||||||
|
|
||||||
|
|
||||||
Caching
|
Caching
|
||||||
~~~~~~~
|
~~~~~~~
|
||||||
|
|
||||||
.. autodata:: star_import_cache_validity
|
.. autodata:: star_import_cache_validity
|
||||||
.. autodata:: get_in_function_call_validity
|
.. autodata:: function_definition_validity
|
||||||
|
|
||||||
|
|
||||||
Various
|
Various
|
||||||
@@ -156,9 +156,9 @@ This is just a debugging option. Always reparsing means that the fast parser
|
|||||||
is basically useless. So don't use it.
|
is basically useless. So don't use it.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
use_get_in_function_call_cache = True
|
use_function_definition_cache = True
|
||||||
"""
|
"""
|
||||||
Use the cache (full cache) to generate get_in_function_call's. This may fail
|
Use the cache (full cache) to generate function_definition's. This may fail
|
||||||
with multiline docstrings (likely) and other complicated changes (unlikely).
|
with multiline docstrings (likely) and other complicated changes (unlikely).
|
||||||
The goal is to move away from it by making the rest faster.
|
The goal is to move away from it by making the rest faster.
|
||||||
"""
|
"""
|
||||||
@@ -225,9 +225,9 @@ max_executions = 250
|
|||||||
A maximum amount of time, the completion may use.
|
A maximum amount of time, the completion may use.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
scale_get_in_function_call = 0.1
|
scale_function_definition = 0.1
|
||||||
"""
|
"""
|
||||||
Because get_in_function_call is normally used on every single key hit, it has
|
Because function_definition is normally used on every single key hit, it has
|
||||||
to be faster than a normal completion. This is the factor that is used to
|
to be faster than a normal completion. This is the factor that is used to
|
||||||
scale `max_executions` and `max_until_execution_unique`:
|
scale `max_executions` and `max_until_execution_unique`:
|
||||||
"""
|
"""
|
||||||
@@ -253,7 +253,7 @@ might be slow, therefore we do a star import caching, that lasts a certain
|
|||||||
time span (in seconds).
|
time span (in seconds).
|
||||||
"""
|
"""
|
||||||
|
|
||||||
get_in_function_call_validity = 3.0
|
function_definition_validity = 3.0
|
||||||
"""
|
"""
|
||||||
Finding function calls might be slow (0.1-0.5s). This is not acceptible for
|
Finding function calls might be slow (0.1-0.5s). This is not acceptible for
|
||||||
normal writing. Therefore cache it for a short time.
|
normal writing. Therefore cache it for a short time.
|
||||||
|
|||||||
@@ -65,9 +65,9 @@ class TestBase(unittest.TestCase):
|
|||||||
script = self.get_script(src, pos)
|
script = self.get_script(src, pos)
|
||||||
return script.goto()
|
return script.goto()
|
||||||
|
|
||||||
def get_in_function_call(self, src, pos=None):
|
def function_definition(self, src, pos=None):
|
||||||
script = self.get_script(src, pos)
|
script = self.get_script(src, pos)
|
||||||
return script.get_in_function_call()
|
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' % \
|
||||||
|
|||||||
@@ -35,11 +35,11 @@ class TestRegression(TestBase):
|
|||||||
self.assertEqual(length, 1)
|
self.assertEqual(length, 1)
|
||||||
|
|
||||||
def test_part_parser(self):
|
def test_part_parser(self):
|
||||||
""" test the get_in_function_call speedups """
|
""" test the function_definition speedups """
|
||||||
s = '\n' * 100 + 'abs('
|
s = '\n' * 100 + 'abs('
|
||||||
pos = 101, 4
|
pos = 101, 4
|
||||||
self.get_in_function_call(s, pos)
|
self.function_definition(s, pos)
|
||||||
assert self.get_in_function_call(s, pos)
|
assert self.function_definition(s, pos)
|
||||||
|
|
||||||
def test_get_definition_cursor(self):
|
def test_get_definition_cursor(self):
|
||||||
|
|
||||||
@@ -123,7 +123,7 @@ class TestRegression(TestBase):
|
|||||||
assert self.complete("from datetime import")[0].word == 'import'
|
assert self.complete("from datetime import")[0].word == 'import'
|
||||||
assert self.complete("from datetime import ")
|
assert self.complete("from datetime import ")
|
||||||
|
|
||||||
def test_get_in_function_call(self):
|
def test_function_definition(self):
|
||||||
def check(call_def, name, index):
|
def check(call_def, name, index):
|
||||||
return call_def and call_def.call_name == name \
|
return call_def and call_def.call_name == name \
|
||||||
and call_def.index == index
|
and call_def.index == index
|
||||||
@@ -139,54 +139,54 @@ class TestRegression(TestBase):
|
|||||||
s7 = "str().upper().center("
|
s7 = "str().upper().center("
|
||||||
s8 = "str(int[zip("
|
s8 = "str(int[zip("
|
||||||
|
|
||||||
assert check(self.get_in_function_call(s, (1, 4)), 'abs', 0)
|
assert check(self.function_definition(s, (1, 4)), 'abs', 0)
|
||||||
assert check(self.get_in_function_call(s, (1, 6)), 'abs', 1)
|
assert check(self.function_definition(s, (1, 6)), 'abs', 1)
|
||||||
assert check(self.get_in_function_call(s, (1, 7)), 'abs', 1)
|
assert check(self.function_definition(s, (1, 7)), 'abs', 1)
|
||||||
assert check(self.get_in_function_call(s, (1, 8)), 'abs', 1)
|
assert check(self.function_definition(s, (1, 8)), 'abs', 1)
|
||||||
assert check(self.get_in_function_call(s, (1, 11)), 'str', 0)
|
assert check(self.function_definition(s, (1, 11)), 'str', 0)
|
||||||
|
|
||||||
assert check(self.get_in_function_call(s2, (1, 4)), 'abs', 0)
|
assert check(self.function_definition(s2, (1, 4)), 'abs', 0)
|
||||||
assert self.get_in_function_call(s2, (1, 5)) is None
|
assert self.function_definition(s2, (1, 5)) is None
|
||||||
assert self.get_in_function_call(s2) is None
|
assert self.function_definition(s2) is None
|
||||||
|
|
||||||
assert self.get_in_function_call(s3, (1, 5)) is None
|
assert self.function_definition(s3, (1, 5)) is None
|
||||||
assert self.get_in_function_call(s3) is None
|
assert self.function_definition(s3) is None
|
||||||
|
|
||||||
assert self.get_in_function_call(s4, (1, 3)) is None
|
assert self.function_definition(s4, (1, 3)) is None
|
||||||
assert check(self.get_in_function_call(s4, (1, 4)), 'abs', 0)
|
assert check(self.function_definition(s4, (1, 4)), 'abs', 0)
|
||||||
assert check(self.get_in_function_call(s4, (1, 8)), 'zip', 0)
|
assert check(self.function_definition(s4, (1, 8)), 'zip', 0)
|
||||||
assert check(self.get_in_function_call(s4, (1, 9)), 'abs', 0)
|
assert check(self.function_definition(s4, (1, 9)), 'abs', 0)
|
||||||
#assert check(self.get_in_function_call(s4, (1, 10)), 'abs', 1)
|
#assert check(self.function_definition(s4, (1, 10)), 'abs', 1)
|
||||||
|
|
||||||
assert check(self.get_in_function_call(s5, (1, 4)), 'abs', 0)
|
assert check(self.function_definition(s5, (1, 4)), 'abs', 0)
|
||||||
assert check(self.get_in_function_call(s5, (1, 6)), 'abs', 1)
|
assert check(self.function_definition(s5, (1, 6)), 'abs', 1)
|
||||||
|
|
||||||
assert check(self.get_in_function_call(s6), 'center', 0)
|
assert check(self.function_definition(s6), 'center', 0)
|
||||||
assert check(self.get_in_function_call(s6, (1, 4)), 'str', 0)
|
assert check(self.function_definition(s6, (1, 4)), 'str', 0)
|
||||||
|
|
||||||
assert check(self.get_in_function_call(s7), 'center', 0)
|
assert check(self.function_definition(s7), 'center', 0)
|
||||||
assert check(self.get_in_function_call(s8), 'zip', 0)
|
assert check(self.function_definition(s8), 'zip', 0)
|
||||||
assert check(self.get_in_function_call(s8, (1, 8)), 'str', 0)
|
assert check(self.function_definition(s8, (1, 8)), 'str', 0)
|
||||||
|
|
||||||
s = "import time; abc = time; abc.sleep("
|
s = "import time; abc = time; abc.sleep("
|
||||||
assert check(self.get_in_function_call(s), 'sleep', 0)
|
assert check(self.function_definition(s), 'sleep', 0)
|
||||||
|
|
||||||
# jedi-vim #9
|
# jedi-vim #9
|
||||||
s = "with open("
|
s = "with open("
|
||||||
assert check(self.get_in_function_call(s), 'open', 0)
|
assert check(self.function_definition(s), 'open', 0)
|
||||||
|
|
||||||
# jedi-vim #11
|
# jedi-vim #11
|
||||||
s1 = "for sorted("
|
s1 = "for sorted("
|
||||||
assert check(self.get_in_function_call(s1), 'sorted', 0)
|
assert check(self.function_definition(s1), 'sorted', 0)
|
||||||
s2 = "for s in sorted("
|
s2 = "for s in sorted("
|
||||||
assert check(self.get_in_function_call(s2), 'sorted', 0)
|
assert check(self.function_definition(s2), 'sorted', 0)
|
||||||
|
|
||||||
# jedi #57
|
# jedi #57
|
||||||
s = "def func(alpha, beta): pass\n" \
|
s = "def func(alpha, beta): pass\n" \
|
||||||
"func(alpha='101',"
|
"func(alpha='101',"
|
||||||
assert check(self.get_in_function_call(s, (2, 13)), 'func', 0)
|
assert check(self.function_definition(s, (2, 13)), 'func', 0)
|
||||||
|
|
||||||
def test_get_in_function_call_complex(self):
|
def test_function_definition_complex(self):
|
||||||
def check(call_def, name, index):
|
def check(call_def, name, index):
|
||||||
return call_def and call_def.call_name == name \
|
return call_def and call_def.call_name == name \
|
||||||
and call_def.index == index
|
and call_def.index == index
|
||||||
@@ -201,17 +201,17 @@ class TestRegression(TestBase):
|
|||||||
if 1:
|
if 1:
|
||||||
pass
|
pass
|
||||||
"""
|
"""
|
||||||
assert check(self.get_in_function_call(s, (6, 24)), 'abc', 0)
|
assert check(self.function_definition(s, (6, 24)), 'abc', 0)
|
||||||
s = """
|
s = """
|
||||||
import re
|
import re
|
||||||
def huhu(it):
|
def huhu(it):
|
||||||
re.compile(
|
re.compile(
|
||||||
return it * 2
|
return it * 2
|
||||||
"""
|
"""
|
||||||
assert check(self.get_in_function_call(s, (4, 31)), 'compile', 0)
|
assert check(self.function_definition(s, (4, 31)), 'compile', 0)
|
||||||
# jedi-vim #70
|
# jedi-vim #70
|
||||||
s = """def foo("""
|
s = """def foo("""
|
||||||
assert self.get_in_function_call(s) is None
|
assert self.function_definition(s) is None
|
||||||
|
|
||||||
def test_add_dynamic_mods(self):
|
def test_add_dynamic_mods(self):
|
||||||
api.settings.additional_dynamic_modules = ['dynamic.py']
|
api.settings.additional_dynamic_modules = ['dynamic.py']
|
||||||
@@ -389,7 +389,7 @@ class TestSpeed(TestBase):
|
|||||||
def test_scipy_speed(self):
|
def test_scipy_speed(self):
|
||||||
s = 'import scipy.weave; scipy.weave.inline('
|
s = 'import scipy.weave; scipy.weave.inline('
|
||||||
script = jedi.Script(s, 1, len(s), '')
|
script = jedi.Script(s, 1, len(s), '')
|
||||||
script.get_in_function_call()
|
script.function_definition()
|
||||||
#print(jedi.imports.imports_processed)
|
#print(jedi.imports.imports_processed)
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|||||||
Reference in New Issue
Block a user