Example #1
0
    def handle_key_press(self, ev, editor):
        key = ev.key()

        if key in (Qt.Key_Enter, Qt.Key_Return) and no_modifiers(ev, Qt.ControlModifier, Qt.AltModifier):
            ls = get_leading_whitespace_on_block(editor)
            cursor, text = get_text_before_cursor(editor)
            if text.rstrip().endswith('{'):
                ls += ' ' * editor.tw
            editor.textCursor().insertText('\n' + ls)
            return True

        if key == Qt.Key_BraceRight:
            ls = get_leading_whitespace_on_block(editor)
            pls = get_leading_whitespace_on_block(editor, previous=True)
            cursor, text = get_text_before_cursor(editor)
            if not text.rstrip() and ls >= pls and len(text) > 1:
                text = expand_tabs(text, editor.tw)[:-editor.tw]
                cursor.insertText(text + '}')
                editor.setTextCursor(cursor)
                return True

        if key == Qt.Key_Home and smart_home(editor, ev):
            return True

        if key == Qt.Key_Tab and smart_tab(editor, ev):
            return True

        if key == Qt.Key_Backspace and smart_backspace(editor, ev):
            return True

        return False
Example #2
0
    def handle_key_press(self, ev, editor):
        key = ev.key()

        if key in (Qt.Key.Key_Enter, Qt.Key.Key_Return) and no_modifiers(ev, Qt.KeyboardModifier.ControlModifier, Qt.KeyboardModifier.AltModifier):
            ls = get_leading_whitespace_on_block(editor)
            cursor, text = get_text_before_cursor(editor)
            if text.rstrip().endswith('{'):
                ls += ' ' * editor.tw
            editor.textCursor().insertText('\n' + ls)
            return True

        if key == Qt.Key.Key_BraceRight:
            ls = get_leading_whitespace_on_block(editor)
            pls = get_leading_whitespace_on_block(editor, previous=True)
            cursor, text = get_text_before_cursor(editor)
            if not text.rstrip() and ls >= pls and len(text) > 1:
                text = expand_tabs(text, editor.tw)[:-editor.tw]
                cursor.insertText(text + '}')
                editor.setTextCursor(cursor)
                return True

        if key == Qt.Key.Key_Home and smart_home(editor, ev):
            return True

        if key == Qt.Key.Key_Tab:
            mods = ev.modifiers()
            if not mods & Qt.KeyboardModifier.ControlModifier and smart_tab(editor, ev):
                return True

        if key == Qt.Key.Key_Backspace and smart_backspace(editor, ev):
            return True

        return False
Example #3
0
    def handle_key_press(self, ev, editor):
        key = ev.key()

        if key == Qt.Key_Tab and smart_tab(editor, ev):
            return True

        elif key == Qt.Key_Backspace and smart_backspace(editor, ev):
            return True

        elif key in (Qt.Key_Enter, Qt.Key_Return):
            ls = get_leading_whitespace_on_block(editor)
            cursor = editor.textCursor()
            line = cursor.block().text()
            if line.rstrip().endswith(':'):
                ls += ' ' * tw
            elif self.escape_scope_pat.match(line) is not None:
                ls = ls[:-tw]
            cursor.insertText('\n' + ls)
            editor.setTextCursor(cursor)
            return True

        elif key == Qt.Key_Colon:
            cursor, text = get_text_before_cursor(editor)
            if self.dedent_pat.search(text) is not None:
                ls = get_leading_whitespace_on_block(editor)
                pls = get_leading_whitespace_on_block(editor, previous=True)
                if ls and ls >= pls:
                    ls = ls[:-tw]
                    text = ls + text.lstrip() + ':'
                    cursor.insertText(text)
                    editor.setTextCursor(cursor)
                    return True

        if key == Qt.Key_Home and smart_home(editor, ev):
            return True
Example #4
0
    def handle_key_press(self, ev, editor):
        ev_text = ev.text()
        key = ev.key()
        is_xml = editor.syntax == 'xml'

        if tprefs['replace_entities_as_typed'] and (key == Qt.Key_Semicolon or ';' in ev_text):
            self.replace_possible_entity(editor)
            return True

        if key in (Qt.Key_Enter, Qt.Key_Return) and no_modifiers(ev, Qt.ControlModifier, Qt.AltModifier):
            ls = get_leading_whitespace_on_block(editor)
            if ls == ' ':
                ls = ''  # Do not consider a single leading space as indentation
            if is_xml:
                count = 0
                for m in self.tag_pat.finditer(get_text_before_cursor(editor)[1]):
                    text = m.group()
                    if self.closing_pat.search(text) is not None:
                        count -= 1
                    elif self.self_closing_pat.search(text) is None:
                        count += 1
                if self.closing_tag_pat.match(get_text_after_cursor(editor)[1].lstrip()):
                    count -= 1
                if count > 0:
                    ls += editor.tw * ' '
            editor.textCursor().insertText('\n' + ls)
            return True

        if key == Qt.Key_Slash:
            cursor, text = get_text_before_cursor(editor)
            if not text.rstrip().endswith('<'):
                return False
            text = expand_tabs(text.rstrip()[:-1], editor.tw)
            pls = get_leading_whitespace_on_block(editor, previous=True)
            if is_xml and not text.lstrip() and len(text) > 1 and len(text) >= len(pls):
                # Auto-dedent
                text = text[:-editor.tw] + '</'
                cursor.insertText(text)
                editor.setTextCursor(cursor)
                self.auto_close_tag(editor)
                return True
            if self.auto_close_tag(editor):
                return True

        if key == Qt.Key_Home and smart_home(editor, ev):
            return True

        if key == Qt.Key_Tab and smart_tab(editor, ev):
            return True

        if key == Qt.Key_Backspace and smart_backspace(editor, ev):
            return True

        if key in (Qt.Key_BraceLeft, Qt.Key_BraceRight):
            mods = ev.modifiers()
            if int(mods & Qt.ControlModifier):
                if self.jump_to_enclosing_tag(editor, key == Qt.Key_BraceLeft):
                    return True

        return False
Example #5
0
    def handle_key_press(self, ev, editor):
        key = ev.key()

        if key == Qt.Key_Tab and smart_tab(editor, ev):
            return True

        elif key == Qt.Key_Backspace and smart_backspace(editor, ev):
            return True

        elif key in (Qt.Key_Enter, Qt.Key_Return):
            ls = get_leading_whitespace_on_block(editor)
            cursor = editor.textCursor()
            line = cursor.block().text()
            if line.rstrip().endswith(':'):
                ls += ' ' * tw
            elif self.escape_scope_pat.match(line) is not None:
                ls = ls[:-tw]
            cursor.insertText('\n' + ls)
            editor.setTextCursor(cursor)
            return True

        elif key == Qt.Key_Colon:
            cursor, text = get_text_before_cursor(editor)
            if self.dedent_pat.search(text) is not None:
                ls = get_leading_whitespace_on_block(editor)
                pls = get_leading_whitespace_on_block(editor, previous=True)
                if ls and ls >= pls:
                    ls = ls[:-tw]
                    text = ls + text.lstrip() + ':'
                    cursor.insertText(text)
                    editor.setTextCursor(cursor)
                    return True

        if key == Qt.Key_Home and smart_home(editor, ev):
            return True
Example #6
0
    def handle_key_press(self, ev, editor):
        ev_text = ev.text()
        key = ev.key()
        is_xml = editor.syntax == 'xml'

        if tprefs['replace_entities_as_typed'] and (key == Qt.Key_Semicolon or ';' in ev_text):
            self.replace_possible_entity(editor)
            return True

        if key in (Qt.Key_Enter, Qt.Key_Return) and no_modifiers(ev, Qt.ControlModifier, Qt.AltModifier):
            ls = get_leading_whitespace_on_block(editor)
            if ls == ' ':
                ls = ''  # Do not consider a single leading space as indentation
            if is_xml:
                count = 0
                for m in self.tag_pat.finditer(get_text_before_cursor(editor)[1]):
                    text = m.group()
                    if self.closing_pat.search(text) is not None:
                        count -= 1
                    elif self.self_closing_pat.search(text) is None:
                        count += 1
                if self.closing_tag_pat.match(get_text_after_cursor(editor)[1].lstrip()):
                    count -= 1
                if count > 0:
                    ls += editor.tw * ' '
            editor.textCursor().insertText('\n' + ls)
            return True

        if key == Qt.Key_Slash:
            cursor, text = get_text_before_cursor(editor)
            if not text.rstrip().endswith('<'):
                return False
            text = expand_tabs(text.rstrip()[:-1], editor.tw)
            pls = get_leading_whitespace_on_block(editor, previous=True)
            if is_xml and not text.lstrip() and len(text) > 1 and len(text) >= len(pls):
                # Auto-dedent
                text = text[:-editor.tw] + '</'
                cursor.insertText(text)
                editor.setTextCursor(cursor)
                self.auto_close_tag(editor)
                return True
            if self.auto_close_tag(editor):
                return True

        if key == Qt.Key_Home and smart_home(editor, ev):
            return True

        if key == Qt.Key_Tab and smart_tab(editor, ev):
            return True

        if key == Qt.Key_Backspace and smart_backspace(editor, ev):
            return True

        if key in (Qt.Key_BraceLeft, Qt.Key_BraceRight):
            mods = ev.modifiers()
            if int(mods & Qt.ControlModifier):
                if self.jump_to_enclosing_tag(editor, key == Qt.Key_BraceLeft):
                    return True

        return False