diff --git a/jedi/api/__init__.py b/jedi/api/__init__.py index 32cfd71d..704efab8 100644 --- a/jedi/api/__init__.py +++ b/jedi/api/__init__.py @@ -462,25 +462,17 @@ class Script(object): and unicode(name_part) == unicode(import_name[0].names[-1]): definitions.append(import_name[0]) else: + # The Evaluator.goto function checks for definitions, but since we + # use a reverse tokenizer, we have new name_part objects, so we + # have to check the user_stmt here for positions. + if isinstance(user_stmt, pr.Statement): + for name in user_stmt.get_defined_names(): + if name.start_pos <= self._pos <= name.end_pos \ + and len(name.names) == 1: + return [name] - def test_lhs(): - """ - Special rule for goto, left hand side of the statement returns - itself, if the name is ``foo``, but not ``foo.bar``. - """ - if isinstance(user_stmt, pr.Statement): - for name in user_stmt.get_defined_names(): - if name.start_pos <= self._pos <= name.end_pos \ - and len(name.names) == 1: - return name - return None - - lhs = test_lhs() - if lhs is None: - defs = self._evaluator.goto(user_stmt or stmt, call_path) - definitions = follow_inexistent_imports(defs) - else: - definitions = [lhs] + defs = self._evaluator.goto(stmt, call_path) + definitions = follow_inexistent_imports(defs) return definitions def usages(self, additional_module_paths=()): diff --git a/jedi/evaluate/__init__.py b/jedi/evaluate/__init__.py index 08ff973e..bdb31566 100644 --- a/jedi/evaluate/__init__.py +++ b/jedi/evaluate/__init__.py @@ -320,6 +320,15 @@ class Evaluator(object): return types def goto(self, stmt, call_path): + # Return the name defined in the call_path, if it's part of the + # statement name definitions. Only return, if it's one name and one + # name only. Otherwise it's a mixture between a definition and a + # reference. In this case it's just a definition. So we stay on it. + print stmt.get_defined_names() + if len(call_path) == 1 and isinstance(call_path[0], pr.NamePart) \ + and call_path[0] in [d.names[-1] for d in stmt.get_defined_names()]: + return [call_path[0]] + scope = stmt.get_parent_scope() pos = stmt.start_pos first_part, search_name_part = call_path[:-1], call_path[-1]