Goto on a function/attribute in a class now goes to the definition in its super class, fixes #1175

This commit is contained in:
Dave Halter
2019-12-24 12:49:23 +01:00
parent a21f443756
commit 9578e4252b
3 changed files with 45 additions and 1 deletions

View File

@@ -8,6 +8,8 @@ Changelog
- **Add** ``Script.get_context`` to get information where you currently are. - **Add** ``Script.get_context`` to get information where you currently are.
- Goto on a function/attribute in a class now goes to the definition in its
super class.
- Big **Script API Changes**: - Big **Script API Changes**:
- The line and column parameters of ``jedi.Script`` are now deprecated - The line and column parameters of ``jedi.Script`` are now deprecated
- ``completions`` deprecated, use ``complete`` instead - ``completions`` deprecated, use ``complete`` instead

View File

@@ -309,6 +309,21 @@ class Script(object):
# executed by `foo()`, if we the cursor is after `)`. # executed by `foo()`, if we the cursor is after `)`.
return self.infer(line, column, only_stubs=only_stubs, prefer_stubs=prefer_stubs) return self.infer(line, column, only_stubs=only_stubs, prefer_stubs=prefer_stubs)
name = self._get_module_context().create_name(tree_name) name = self._get_module_context().create_name(tree_name)
# Make it possible to goto the super class function/attribute
# definitions, when they are overwritten.
names = []
if name.tree_name.is_definition() and name.parent_context.is_class():
class_node = name.parent_context.tree_node
class_value = self._get_module_context().create_value(class_node)
mro = class_value.py__mro__()
next(mro) # Ignore the first entry, because it's the class itself.
for cls in mro:
names = cls.goto(tree_name.value)
if names:
break
if not names:
names = list(name.goto()) names = list(name.goto())
if follow_imports: if follow_imports:

View File

@@ -0,0 +1,27 @@
class Super(object):
attribute = 3
def func(self):
return 1
class Inner():
pass
class Sub(Super):
#? 13 Sub.attribute
def attribute(self):
pass
#! 8 ['attribute = 3']
def attribute(self):
pass
#! 4 ['def func']
func = 3
#! 12 ['def func']
class func(): ...
#! 8 ['class Inner']
def Inner(self): ...