1
0
forked from VimPlug/jedi

Param helper class in the tree.

This commit is contained in:
Dave Halter
2014-10-22 20:07:42 +02:00
parent e9f4c60e49
commit 4f4aef7ac8
3 changed files with 49 additions and 27 deletions

View File

@@ -154,10 +154,10 @@ class Instance(use_metaclass(CachedMetaClass, Executed)):
continue continue
# Get the self name, if there's one. # Get the self name, if there's one.
self_name = self._get_func_self_name(sub) self_name = self._get_func_self_name(sub)
if not self_name: if self_name is None:
continue continue
if sub.name.get_code() == '__init__': if sub.name.value == '__init__':
# ``__init__`` is special because the params need are injected # ``__init__`` is special because the params need are injected
# this way. Therefore an execution is necessary. # this way. Therefore an execution is necessary.
if not sub.decorators: if not sub.decorators:
@@ -165,6 +165,7 @@ class Instance(use_metaclass(CachedMetaClass, Executed)):
# because to follow them and their self variables is too # because to follow them and their self variables is too
# complicated. # complicated.
sub = self._get_method_execution(sub) sub = self._get_method_execution(sub)
print(sub.get_names_dict().values())
for per_name_list in sub.get_names_dict().values(): for per_name_list in sub.get_names_dict().values():
for call in per_name_list: for call in per_name_list:
if unicode(call.name) == self_name \ if unicode(call.name) == self_name \

View File

@@ -57,7 +57,7 @@ class Parser(object):
# files processed even if they were not changed during refactoring. If # files processed even if they were not changed during refactoring. If
# and only if the refactor method's write parameter was True. # and only if the refactor method's write parameter was True.
self.used_names = {} self.used_names = {}
logger = logging.getLogger("RefactoringTool") logger = logging.getLogger("Jedi-Parser")
d = Driver(pytree.python_grammar, convert=self.convert, logger=logger) d = Driver(pytree.python_grammar, convert=self.convert, logger=logger)
self.module = d.parse_string(source).get_parent_until() self.module = d.parse_string(source).get_parent_until()

View File

@@ -408,12 +408,11 @@ class Scope(Simple, DocstringMixin):
:param start_pos: The position (line and column) of the scope. :param start_pos: The position (line and column) of the scope.
:type start_pos: tuple(int, int) :type start_pos: tuple(int, int)
""" """
__slots__ = ('subscopes', 'imports', '_doc_token', 'asserts', __slots__ = ('imports', '_doc_token', 'asserts',
'is_generator', '_names_dict') 'is_generator', '_names_dict')
def __init__(self, children): def __init__(self, children):
super(Scope, self).__init__(children) super(Scope, self).__init__(children)
self.subscopes = []
self.imports = [] self.imports = []
self._doc_token = None self._doc_token = None
self.asserts = [] self.asserts = []
@@ -426,6 +425,10 @@ class Scope(Simple, DocstringMixin):
# returns will be in "normal" modules. # returns will be in "normal" modules.
return self._search_in_scope(ReturnStmt) return self._search_in_scope(ReturnStmt)
@property
def subscopes(self):
return self._search_in_scope(Scope)
def _search_in_scope(self, typ): def _search_in_scope(self, typ):
def scan(children): def scan(children):
elements = [] elements = []
@@ -519,7 +522,7 @@ class Scope(Simple, DocstringMixin):
if include_imports: if include_imports:
checks += self.imports checks += self.imports
if self.isinstance(Function): if self.isinstance(Function):
checks += self.params + self.decorators checks += self.decorators
checks += [r for r in self.returns if r is not None] checks += [r for r in self.returns if r is not None]
if self.isinstance(Flow): if self.isinstance(Flow):
checks += self.inputs checks += self.inputs
@@ -717,7 +720,7 @@ class Function(Scope):
:param start_pos: The start position (line, column) the Function. :param start_pos: The start position (line, column) the Function.
:type start_pos: tuple(int, int) :type start_pos: tuple(int, int)
""" """
__slots__ = ('decorators', 'listeners') __slots__ = ('decorators', 'listeners', '_params')
def __init__(self, children): def __init__(self, children):
super(Function, self).__init__(children) super(Function, self).__init__(children)
@@ -729,11 +732,30 @@ class Function(Scope):
return self.children[1] # First token after `def` return self.children[1] # First token after `def`
@property @property
@cache.underscore_memoization
def params(self): def params(self):
third = self.children[3] # After def foo( node = self.children[2].children[1:-1] # After `def foo`
if isinstance(third, Operator): if not node:
return [] return []
return self.children[3].children if is_node(node[0], 'typedargslist'):
params = []
iterator = node[0].children
for n in iterator:
stars = 0
if n in ('*', '**'):
stars = len(n.value)
n = next(iterator, None)
op = next(iterator, None)
if op == '=':
default = op
next(iterator, None)
else:
default = None
params.append(Param(n, default, stars))
return params
else:
return [Param(node[0])]
def annotation(self): def annotation(self):
try: try:
@@ -1178,15 +1200,28 @@ class ArrayStmt(Statement):
""" """
class Param(ExprStmt): class Param(object):
""" """
The class which shows definitions of params of classes and functions. The class which shows definitions of params of classes and functions.
But this is not to define function calls. But this is not to define function calls.
A helper class for functions. Read only.
""" """
__slots__ = ('position_nr', 'is_generated', 'annotation_stmt', __slots__ = ('tfpdef', 'default', 'stars', 'position_nr', 'is_generated', 'annotation_stmt',
'parent_function') 'parent_function')
def __init__(self, *args, **kwargs): def __init__(self, tfpdef, default=None, stars=0):
self.tfpdef = tfpdef # tfpdef: see grammar.txt
self.default = default
self.stars = stars
def get_name(self):
if is_node(self.tfpdef, 'tfpdef'):
return self.tfpdef.children[0]
else:
return self.tfpdef
def __init__old(self):
kwargs.pop('names_are_set_vars', None) kwargs.pop('names_are_set_vars', None)
super(Param, self).__init__(*args, names_are_set_vars=True, **kwargs) super(Param, self).__init__(*args, names_are_set_vars=True, **kwargs)
@@ -1201,20 +1236,6 @@ class Param(ExprStmt):
annotation_stmt.parent = self.use_as_parent annotation_stmt.parent = self.use_as_parent
self.annotation_stmt = annotation_stmt self.annotation_stmt = annotation_stmt
def get_name(self):
""" get the name of the param """
n = self.get_defined_names()
if len(n) > 1:
debug.warning("Multiple param names (%s).", n)
return n[0]
@property
def stars(self):
exp = self.expression_list()
if exp and isinstance(exp[0], Operator):
return exp[0].string.count('*')
return 0
class StatementElement(Simple): class StatementElement(Simple):
__slots__ = ('next', 'previous') __slots__ = ('next', 'previous')