Ejemplo n.º 1
0
def finisher(object):
    if type(object) in callabletypes:
        return "("
    elif type(object) in sequencetypes:
        return "["
    elif type(object) in mappingtypes:
        return "{"
    elif members(object):
        return "."
    return " "
Ejemplo n.º 2
0
    def cb_complete(self, event):
        """Attempt to complete the identifier currently being typed."""
        if self.compmenus:
            if self.cursor() == self.compindex:
                # Second attempt to complete: add finishing char and continue.
                self.text.insert("insert", self.compfinish)
                self.compindex = None
                self.unpostmenus()
            return "break"

        command, context, ident, start, end = self.precontext()

        # Get the list of possible choices.
        if context:
            try:
                object = eval(context[:-1], self.dict, self.globals)
                keys = members(object)
            except:
                object = None
                keys = []
        else:
            class Lookup:
                def __init__(self, dicts):
                    self.dicts = dicts

                def __getattr__(self, key):
                    for dict in self.dicts:
                        if dict.has_key(key): return dict[key]
                    return None
            object = Lookup([self.dict, __builtin__.__dict__])
            keys = self.dict.keys() + self.globals.keys() + dir(__builtin__)

        keys = matchingkeys(keys, ident)
        if not ident:
            public = []
            for key in keys:
                if key[:1] != "_": public.append(key)
            keys = public
        skip = len(ident)

        # Produce the completion.
        if len(keys) == 1:
            # Complete with the single possible choice.
            if self.cursor() == self.compindex:
                # Second attempt to complete: add finisher and continue.
                self.text.insert("insert", self.compfinish)
                self.compindex = None
            else:
                self.text.delete("insert", end)
                self.text.insert("insert", keys[0][skip:])
                try: self.compfinish = finisher(getattr(object, keys[0]))
                except: self.compfinish = " "
                if self.compfinish == " ":
                    # Object has no members; stop here.
                    self.text.insert("insert", " ")
                else:
                    self.compindex = self.cursor()
        elif len(keys) > 1:
            # Present a menu.
            prefix = commonprefix(keys)
            keys.sort()
            if len(prefix) > skip:
                self.text.delete("insert", end)
                self.text.insert("insert", keys[0][skip:len(prefix)])
                skip = len(prefix)

            if len(keys[0]) == skip:
                # Common prefix is a valid choice; next try can finish.
                self.compindex = self.cursor()
                try: self.compfinish = finisher(getattr(object, keys[0]))
                except: self.compfinish = " "

            self.postmenus(keys, skip, end, object)

        return "break"