Script.goto_assignments now always needs a call_path. Otherwise it raises a NotFoundError.

This change makes Jedi's behavior more consistent.
This commit is contained in:
Dave Halter
2014-09-04 00:56:58 +02:00
parent fb10199f37
commit e872d9e073
3 changed files with 20 additions and 19 deletions

View File

@@ -429,6 +429,20 @@ class Script(object):
goto_path = self._user_context.get_path_under_cursor() goto_path = self._user_context.get_path_under_cursor()
context = self._user_context.get_context() context = self._user_context.get_context()
user_stmt = self._parser.user_stmt() user_stmt = self._parser.user_stmt()
stmt = self._get_under_cursor_stmt(goto_path)
expression_list = stmt.expression_list()
if len(expression_list) == 0:
return []
# The reverse tokenizer only generates parses call.
assert len(expression_list) == 1
call = expression_list[0]
if isinstance(call, pr.Call):
call_path = list(call.generate_call_path())
else:
# goto_assignments on Operator returns nothing.
return []
if next(context) in ('class', 'def'): if next(context) in ('class', 'def'):
# The cursor is on a class/function name. # The cursor is on a class/function name.
user_scope = self._parser.user_scope() user_scope = self._parser.user_scope()
@@ -448,7 +462,6 @@ class Script(object):
and unicode(name_part) == unicode(import_name[0].names[-1]): and unicode(name_part) == unicode(import_name[0].names[-1]):
definitions.append(import_name[0]) definitions.append(import_name[0])
else: else:
stmt = self._get_under_cursor_stmt(goto_path)
def test_lhs(): def test_lhs():
""" """
@@ -464,20 +477,7 @@ class Script(object):
lhs = test_lhs() lhs = test_lhs()
if lhs is None: if lhs is None:
expression_list = stmt.expression_list() defs = self._evaluator.goto(user_stmt or stmt, call_path)
if len(expression_list) == 0:
return []
# The reverse tokenizer only generates parses call.
assert len(expression_list) == 1
call = expression_list[0]
if isinstance(call, pr.Call):
call_path = list(call.generate_call_path())
else:
# goto_assignments on Operator returns nothing.
return []
defs = self._evaluator.goto(user_stmt or stmt,
call_path)
definitions = follow_inexistent_imports(defs) definitions = follow_inexistent_imports(defs)
else: else:
definitions = [lhs] definitions = [lhs]

View File

@@ -96,8 +96,7 @@ from import_tree.rename1 import abc
#< (0, 32), #< (0, 32),
from import_tree.rename1 import not_existing from import_tree.rename1 import not_existing
# shouldn't work # Shouldn't work (would raise a NotFoundError, because there's no name.)
#<
from not_existing import * from not_existing import *
# ----------------- # -----------------

View File

@@ -3,6 +3,7 @@ Test of keywords and ``jedi.keywords``
""" """
import jedi import jedi
from jedi import Script, common from jedi import Script, common
import pytest
def test_goto_assignments_keyword(): def test_goto_assignments_keyword():
@@ -19,8 +20,9 @@ def test_keyword():
defs = Script("print").goto_definitions() defs = Script("print").goto_definitions()
assert [d.doc for d in defs] assert [d.doc for d in defs]
defs = Script("import").goto_assignments() with pytest.raises(jedi.NotFoundError):
assert len(defs) == 0 Script("import").goto_assignments()
completions = Script("import", 1, 1).completions() completions = Script("import", 1, 1).completions()
assert len(completions) == 0 assert len(completions) == 0
with common.ignored(jedi.NotFoundError): # TODO shouldn't throw that. with common.ignored(jedi.NotFoundError): # TODO shouldn't throw that.