mirror of
https://github.com/davidhalter/jedi.git
synced 2025-12-09 23:34:45 +08:00
Fix some API classes issues. Among them call signature generation and Definition.parent() issues.
This commit is contained in:
@@ -156,9 +156,13 @@ class BaseDefinition(object):
|
|||||||
|
|
||||||
if isinstance(stripped, compiled.CompiledObject):
|
if isinstance(stripped, compiled.CompiledObject):
|
||||||
return stripped.type()
|
return stripped.type()
|
||||||
if isinstance(stripped, iterable.Array):
|
elif isinstance(stripped, iterable.Array):
|
||||||
return 'instance'
|
return 'instance'
|
||||||
|
elif isinstance(stripped, pr.Import):
|
||||||
|
return 'import'
|
||||||
|
|
||||||
string = type(stripped).__name__.lower().replace('wrapper', '')
|
string = type(stripped).__name__.lower().replace('wrapper', '')
|
||||||
|
print(stripped, string)
|
||||||
if string == 'exprstmt':
|
if string == 'exprstmt':
|
||||||
return 'statement'
|
return 'statement'
|
||||||
else:
|
else:
|
||||||
@@ -379,8 +383,8 @@ class BaseDefinition(object):
|
|||||||
|
|
||||||
def parent(self):
|
def parent(self):
|
||||||
scope = self._definition.get_parent_scope()
|
scope = self._definition.get_parent_scope()
|
||||||
non_flow = scope.get_parent_until(pr.Flow, reverse=True)
|
scope = er.wrap(self._evaluator, scope)
|
||||||
return Definition(self._evaluator, non_flow.name)
|
return Definition(self._evaluator, scope.name)
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return "<%s %s>" % (type(self).__name__, self.description)
|
return "<%s %s>" % (type(self).__name__, self.description)
|
||||||
|
|||||||
@@ -414,6 +414,10 @@ class Class(use_metaclass(CachedMetaClass, Wrapper)):
|
|||||||
def py__getattribute__(self, name):
|
def py__getattribute__(self, name):
|
||||||
return self._evaluator.find_types(self, name)
|
return self._evaluator.find_types(self, name)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def params(self):
|
||||||
|
return self.get_subscope_by_name('__init__').params
|
||||||
|
|
||||||
def scope_names_generator(self, position=None, add_class_vars=True):
|
def scope_names_generator(self, position=None, add_class_vars=True):
|
||||||
def in_iterable(name, iterable):
|
def in_iterable(name, iterable):
|
||||||
""" checks if the name is in the variable 'iterable'. """
|
""" checks if the name is in the variable 'iterable'. """
|
||||||
|
|||||||
@@ -43,9 +43,10 @@ import re
|
|||||||
from inspect import cleandoc
|
from inspect import cleandoc
|
||||||
from collections import defaultdict
|
from collections import defaultdict
|
||||||
from itertools import chain
|
from itertools import chain
|
||||||
|
import textwrap
|
||||||
|
|
||||||
from jedi._compatibility import (next, Python3Method, encoding, is_py3,
|
from jedi._compatibility import (next, Python3Method, encoding, is_py3,
|
||||||
literal_eval, use_metaclass)
|
literal_eval, use_metaclass, unicode)
|
||||||
from jedi import debug
|
from jedi import debug
|
||||||
from jedi import cache
|
from jedi import cache
|
||||||
|
|
||||||
@@ -779,9 +780,9 @@ class Class(ClassOrFunc):
|
|||||||
if self._doc_token is not None:
|
if self._doc_token is not None:
|
||||||
docstr = self.raw_doc
|
docstr = self.raw_doc
|
||||||
for sub in self.subscopes:
|
for sub in self.subscopes:
|
||||||
if unicode(sub.name) == '__init__':
|
if str(sub.name) == '__init__':
|
||||||
return '%s\n\n%s' % (
|
return '%s\n\n%s' % (
|
||||||
sub.get_call_signature(funcname=self.name), docstr)
|
sub.get_call_signature(func_name=self.name), docstr)
|
||||||
return docstr
|
return docstr
|
||||||
|
|
||||||
def scope_names_generator(self, position=None):
|
def scope_names_generator(self, position=None):
|
||||||
@@ -862,32 +863,20 @@ class Function(ClassOrFunc):
|
|||||||
def scope_names_generator(self, position=None):
|
def scope_names_generator(self, position=None):
|
||||||
yield self, filter_after_position(self.get_defined_names(), position)
|
yield self, filter_after_position(self.get_defined_names(), position)
|
||||||
|
|
||||||
def get_call_signature(self, width=72, funcname=None):
|
def get_call_signature(self, width=72, func_name=None):
|
||||||
"""
|
"""
|
||||||
Generate call signature of this function.
|
Generate call signature of this function.
|
||||||
|
|
||||||
:param width: Fold lines if a line is longer than this value.
|
:param width: Fold lines if a line is longer than this value.
|
||||||
:type width: int
|
:type width: int
|
||||||
:arg funcname: Override function name when given.
|
:arg func_name: Override function name when given.
|
||||||
:type funcname: str
|
:type func_name: str
|
||||||
|
|
||||||
:rtype: str
|
:rtype: str
|
||||||
"""
|
"""
|
||||||
l = unicode(funcname or self.name) + '('
|
func_name = func_name or self.children[1]
|
||||||
lines = []
|
code = unicode(func_name) + self.children[2].get_code()
|
||||||
for (i, p) in enumerate(self.params):
|
return '\n'.join(textwrap.wrap(code, width))
|
||||||
code = p.get_code(False)
|
|
||||||
if i != len(self.params) - 1:
|
|
||||||
code += ', '
|
|
||||||
if len(l + code) > width:
|
|
||||||
lines.append(l[:-1] if l[-1] == ' ' else l)
|
|
||||||
l = code
|
|
||||||
else:
|
|
||||||
l += code
|
|
||||||
if l:
|
|
||||||
lines.append(l)
|
|
||||||
lines[-1] += ')'
|
|
||||||
return '\n'.join(lines)
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def doc(self):
|
def doc(self):
|
||||||
|
|||||||
@@ -105,7 +105,7 @@ def test_class_call_signature():
|
|||||||
pass
|
pass
|
||||||
Foo""").goto_definitions()
|
Foo""").goto_definitions()
|
||||||
doc = defs[0].doc
|
doc = defs[0].doc
|
||||||
assert "Foo(self, x, y = 1, z = 'a')" in str(doc)
|
assert "Foo(self, x, y=1, z='a')" in str(doc)
|
||||||
|
|
||||||
|
|
||||||
def test_position_none_if_builtin():
|
def test_position_none_if_builtin():
|
||||||
@@ -212,7 +212,7 @@ class TestParent(TestCase):
|
|||||||
def bar(): pass
|
def bar(): pass
|
||||||
Foo().bar''')).completions()[0].parent()
|
Foo().bar''')).completions()[0].parent()
|
||||||
assert parent.name == 'Foo'
|
assert parent.name == 'Foo'
|
||||||
assert parent.type == 'class'
|
assert parent.type == 'instance'
|
||||||
|
|
||||||
parent = Script('str.join').completions()[0].parent()
|
parent = Script('str.join').completions()[0].parent()
|
||||||
assert parent.name == 'str'
|
assert parent.name == 'str'
|
||||||
|
|||||||
Reference in New Issue
Block a user