From c154bdad0e8f8226e34b95e57476326aff4aaa72 Mon Sep 17 00:00:00 2001 From: David Halter Date: Mon, 6 Aug 2012 17:47:43 +0200 Subject: [PATCH] array indexing works now also with variables --- evaluate.py | 24 +++++++++++++++--------- helpers.py | 7 +++++++ mixin/builtins.py | 9 +++++++++ parsing.py | 3 ++- test/completion/arrays.py | 23 +++++++++++++++++++++++ 5 files changed, 56 insertions(+), 10 deletions(-) diff --git a/evaluate.py b/evaluate.py index bc8994a7..b2392788 100644 --- a/evaluate.py +++ b/evaluate.py @@ -727,17 +727,21 @@ class Array(object): def __init__(self, array): self._array = array - def get_index_types(self, index=None): - values = self._array.values - if index is not None: - if [x for x in index if ':' in x]: + def get_index_types(self, index_call_list=None): + """ Tries to get the only element (key) of a TODO doc""" + # array slicing + if index_call_list is not None: + if index_call_list and [x for x in index_call_list if ':' in x]: return [self] - else: + + index_possibilities = list(follow_call_list(index_call_list)) + if len(index_possibilities) == 1: # This is indexing only one element, with a fixed index number, # otherwise it just ignores the index (e.g. [1+1]). try: - # Multiple elements in the array. - i = index.get_only_subelement().name + # Multiple elements in the array are not wanted. var_args + # and get_only_subelement can raise AttributeErrors. + i = index_possibilities[0].var_args.get_only_subelement() except AttributeError: pass else: @@ -745,9 +749,10 @@ class Array(object): return self.get_exact_index_types(i) except (IndexError, KeyError): pass - return self.follow_values(values) + return self.follow_values(self._array.values) def get_exact_index_types(self, index): + """ Here the index is an int. Raises IndexError/KeyError """ if self._array.type == parsing.Array.DICT: old_index = index index = None @@ -1155,7 +1160,8 @@ def follow_call(call): debug.warning('unknown type:', current.type, current) scopes = [] # Make instances of those number/string objects. - scopes = [Instance(s) for s in scopes] + arr = helpers.generate_param_array([current.name]) + scopes = [Instance(s, arr) for s in scopes] else: # This is the first global lookup. scopes = get_scopes_for_name(scope, current, position=position, diff --git a/helpers.py b/helpers.py index ac2dda49..e8074c93 100644 --- a/helpers.py +++ b/helpers.py @@ -77,3 +77,10 @@ def fast_parent_copy(obj): copied_list[i] = list_rec(el) return copied_list return recursion(obj) + +def generate_param_array(args_tuple, parent_stmt=None): + """ This generates an array, that can be used as a param """ + values = [] + for arg in args_tuple: + values.append([arg]) + return parsing.Array(parsing.Array.TUPLE, parent_stmt, values=values) diff --git a/mixin/builtins.py b/mixin/builtins.py index 1b8a46c8..7ab24602 100644 --- a/mixin/builtins.py +++ b/mixin/builtins.py @@ -99,3 +99,12 @@ def iter(collection, sentinel=None): yield collection() else: yield next(collection) + +# basic types +class int(): + def __init__(self, x, base=None): + self.x = x + +class str(): + def __init__(self, obj): + self.obj = obj diff --git a/parsing.py b/parsing.py index 70d34c69..bbd811af 100644 --- a/parsing.py +++ b/parsing.py @@ -830,7 +830,8 @@ class Array(Call): DICT = 'dict' SET = 'set' - def __init__(self, arr_type, parent_stmt=None, parent=None, values=None): + def __init__(self, arr_type=NOARRAY, parent_stmt=None, parent=None, + values=None): super(Array, self).__init__(None, arr_type, parent_stmt, parent) self.values = values if values else [] diff --git a/test/completion/arrays.py b/test/completion/arrays.py index 477f5dfe..43eb7704 100644 --- a/test/completion/arrays.py +++ b/test/completion/arrays.py @@ -139,6 +139,29 @@ dic2[r'asdf'] #? int() str() dic2['just_something'] +# ----------------- +# with variable as index +# ----------------- +a = (1, "") +index = 1 +#? str() +a[index] + +# these should just ouput the whole array +index = int +#? int() str() +a[index] +index = int() +#? int() str() +a[index] + +# dicts +index = 'asdf' + +dic2 = {'asdf': 3, 'b': 'str'} +#? int() +dic2[index] + # ----------------- # __getitem__ # -----------------