Пример #1
0
    def on_keydown(self, key, modifiers, text):
        selection = self.selection
        overlay = self.overlay
        shift = 'shift' in modifiers
        ctrl  = 'ctrl' in modifiers
        if ctrl and key == 's':
            fileformat_flat.save_file(filename, mutable, document)
        if ctrl and key == 'a':
            self.selection = mutable.extend(selection)
            overlay.dirty = True
        if isinstance(selection, mutable.Selection):
            if key == 'left' and selection.head > 0:
                selection.head -= 1
            elif key == 'right' and selection.head < selection.last:
                selection.head += 1
            if key in ('left', 'right'):
                selection.tail = selection.tail if shift else selection.head
                overlay.dirty = True
            if isinstance(selection.container, mutable.String):
                if not ctrl and len(text) > 0 and text.isalnum() or text in ' ':
                    selection.splice(text)
                    selection.start = selection.stop
            if key in ('backspace', 'delete'):
                if selection.start == selection.stop:
                    selection.start -= (key == 'backspace')
                    selection.stop  += (key == 'delete')
                selection.splice(u'')
                selection.stop = selection.start
        else:
            parent = selection.parent
            index  = parent.index(selection)
            if key == 'left' and index > 0:
                self.selection = parent[index-1]
                overlay.dirty = True
            if key == 'right' and index+1 < len(parent):
                self.selection = parent[index+1]
                overlay.dirty = True

        if text == '/':
            target = mutable.String("")
            data = mutable.Struct(mutable.StructType(u'variable:name'), [target])
            if self.insert_object(data):
                self.selection = mutable.Selection(target, 0)

        if text == '(':
            target = mutable.Struct(mutable.StructType(u'null'), [])
            data = mutable.Struct(mutable.StructType(u'call:callee:arguments'), [target, mutable.List([])])
            if self.insert_object(data):
                self.selection = target
Пример #2
0
    def on_keydown(self, key, modifiers, text):
        selection = self.selection
        overlay = self.overlay
        shift = 'shift' in modifiers
        ctrl  = 'ctrl' in modifiers
        alt   = 'alt' in modifiers
        if ctrl and key == 's':
            document = mutable.get_document(mutable.get_object(selection))
            fileformat_flat.save_file(document.filename, mutable, document)
            return
        if ctrl and key == 'a':
            self.selection = mutable.extend(selection)
            overlay.dirty = True
            return
        if ctrl and key == 'left':
            self.selection = walk_prev_selection(selection)
            overlay.dirty = True
            return
        if ctrl and key == 'right':
            self.selection = walk_next_selection(selection)
            overlay.dirty = True
            return
        if isinstance(selection, mutable.Selection):
            if key == 'left':
                if selection.head > 0:
                    selection.head -= 1
                else:
                    self.selection = prev_selection(mutable.get_object(selection))
                    overlay.dirty = True
                    return
            elif key == 'right':
                if selection.head < selection.last:
                    selection.head += 1
                else:
                    self.selection = next_selection(mutable.get_object(selection))
                    overlay.dirty = True
                    return
            elif key == 'home':
                selection.head = 0
            elif key == 'end':
                selection.head = selection.last
            if key in ('left', 'right', 'home', 'end'):
                selection.tail = selection.tail if shift else selection.head
                overlay.dirty = True
#            if isinstance(selection.container, mutable.String):
#                if not ctrl and len(text) > 0 and text.isalnum() or text in ' ':
#                    selection.splice(text)
#                    selection.start = selection.stop
            if key == 'backspace' or key == 'delete':
                if selection.start == selection.stop:
                    selection.start -= (key == 'backspace')
                    selection.stop  += (key == 'delete')
                selection.splice(u'')
                selection.stop = selection.start
                return
        else:
            parent = selection.parent
            index  = parent.index(selection)
            if key == 'left':
                self.selection = prev_selection(selection)
                overlay.dirty = True
            if key == 'right':
                self.selection = next_selection(selection)
                overlay.dirty = True

        if text == '=':
            target = mkstruct(u"null")
            block = mkstruct(u"assign:slot:value", target, mkstruct(u"null"))
            return self.insert_inline(target, block, expressions)
        if alt and key == 'r':
            target = mkstruct(u"null")
            block = mkstruct(u"return:value", target)
            return self.insert_inline_deep(target, block, expressions)
        if text == '(':
            target = mkstruct(u"null")
            block = mkstruct(u"call:callee:arguments", target, mutable.List([]))
            return self.insert_inline(target, block, expressions)
        if key == 'space':
            self.selection = next_selection(mutable.get_object(selection))
            overlay.dirty = True
        elif len(text) > 0 and not self.insert_text(text):
            if text.isalpha():
                target = mutable.String(u"")
                if self.insert_object(mkstruct(u'variable:name', target)):
                    self.selection = mutable.Selection(target, 0)
                    self.insert_text(text)
            elif text.isdigit():
                target = mutable.String(u"")
                if self.insert_object(mkstruct(u'number:value', target)):
                    self.selection = mutable.Selection(target, 0)
                    self.insert_text(text)