Source

codeaide / codeaide / modes / autotyping.py

Full commit
from codeaide.base import *

class AutoTyping(ModeBase):


    def count_paren(self, cursor, open_char, close_char):
        # XXX: Only counts on the current line. Is this a good idea?
        depth = 0
        line = unicode(cursor.block().text())
        col = cursor.columnNumber()
        for i, char in enumerate(line):
            if char == open_char:
                depth += 1
            elif char == close_char and \
              ((depth > 0) or (col <= i)):
                depth -= 1
        return depth


    def append_close_char(self, event, key, modifiers, text):
        """
        append close char and put cursor before close char
        """
        char = {
            Qt.Key_ParenLeft: u")",
            Qt.Key_BraceLeft: u"}",
            Qt.Key_BracketLeft:u"]",
            Qt.Key_Apostrophe: u"'",
            Qt.Key_QuoteDbl: u'"'
            }
        close_char = char[key]
        open_char = unicode(text)
        cursor = self.textedit.textCursor()
        if self.count_paren(cursor, text, close_char) <= -1:
            return False
        cursor.insertText(text)
        cursor.insertText(char[key])
        cursor.setPosition(cursor.position() - 1)
        self.textedit.setTextCursor(cursor)
        return True


    def close_char_pressed(self, event, key, modifiers, text):
        char = {
            Qt.Key_ParenRight: u"(",
            Qt.Key_BraceRight: u"{",
            Qt.Key_BracketRight:u"[",
            }
        open_char = char[key]
        close_char = unicode(text)
        cursor = self.textedit.textCursor()
        line = cursor.block().text()
        cursor.movePosition(QTextCursor.Right, QTextCursor.MoveAnchor)
        cursor.movePosition(QTextCursor.Left, QTextCursor.KeepAnchor, 2)
        seltext = unicode(cursor.selectedText())
        if seltext == open_char + close_char:
            if self.count_paren(cursor, open_char, close_char) < 1:
                event.ignore()
                return True
        return False


    def append_space(self, event, key, modifiers, text):
        """
        Normally you add a space character after commas, etc.
        """
        cursor = self.textedit.textCursor()
        cursor.insertText(text)
        cursor.insertText(" ")
        self.textedit.setTextCursor(cursor)
        return True


    def expand_after_space(self, event, key, modifieres, text):
        cursor = self.textedit.textCursor()
        line = unicode(cursor.block().text())
        col = cursor.columnNumber()
        match = re.compile("^\s*from\s+[A-Za-z_0-9]+\s*$").match
        head, tail = (line[:col], line[col:])
        if match(head):
            cursor.insertText(" import ")
        self.textedit.setTextCursor(cursor)
    


    keyboard_hooks = {
        Qt.Key_ParenLeft: append_close_char,
        Qt.Key_BraceLeft: append_close_char,
        Qt.Key_BracketLeft: append_close_char,
        Qt.Key_Apostrophe: append_close_char,
        Qt.Key_QuoteDbl: append_close_char,

        Qt.Key_ParenRight: close_char_pressed,
        Qt.Key_BraceRight: close_char_pressed,
        Qt.Key_BracketRight: close_char_pressed,

        Qt.Key_Comma: append_space,

        Qt.Key_Space: expand_after_space,
        }