def enter_with_auto_indent(self): line_number = self.textCursor().blockNumber() line_text = self.document().findBlockByLineNumber(line_number).text() indent_level = line_text.count(" " * util.indent_width(self.lang)) if self.lang == "python3": if line_text.endswith(":"): indent_level += 1 elif line_text.endswith("{"): indent_level += 1 self.insertPlainText("\n" + " " * util.indent_width(self.lang) * indent_level)
def enter_with_auto_indent(self): line_text = self.get_current_line_test() indent_level = line_text.count(" " * util.indent_width(self.lang)) if self.lang == "python3": if line_text.endswith(":"): indent_level += 1 elif line_text.endswith("{"): indent_level += 1 self.insertPlainText("\n" + " " * util.indent_width(self.lang) * indent_level)
def keyPressEvent(self, event: QtGui.QKeyEvent): tc = self.textCursor() if event.key() == Qt.Key_Return and self.completer.popup().isVisible(): self.insertCompletion() return if event.key() == Qt.Key_Return: self.enter_with_auto_indent() return if event.key() == Qt.Key_Backspace and self.should_remove_indent(): for i in range(util.indent_width(self.lang)): tc.movePosition(QtGui.QTextCursor.Left) tc.deleteChar() return if event.key() == Qt.Key_BraceRight and self.lang != "python3": # Decrease indent level if ( tc.position() > 0 and self.document().characterAt(tc.position() - 1) != "{" ): tc.movePosition(QtGui.QTextCursor.StartOfLine) for i in range(util.indent_width(self.lang)): if self.document().characterAt(tc.position()) == " ": tc.deleteChar() if event.key() == Qt.Key_F5: line_num = tc.blockNumber() + 1 self.toggleBreak(line_num) return # comment out or uncomment if ( event.modifiers() == QtCore.Qt.ControlModifier and event.key() == Qt.Key_Slash ): tc = self.textCursor() if tc.selection().isEmpty(): selected_line_num = 1 else: selected_line_num = tc.selection().toPlainText().count("\n") + 1 for i in range(selected_line_num): tc.movePosition(QtGui.QTextCursor.StartOfLine) if self.document().characterAt(tc.position()) == " ": tc.movePosition( QtGui.QTextCursor.NextWord, QtGui.QTextCursor.MoveAnchor, 1 ) if ( self.document().characterAt(tc.position()) == "/" and self.document().characterAt(tc.position() + 1) == "/" and self.document().characterAt(tc.position() + 2) == " " ): tc.deleteChar() tc.deleteChar() tc.deleteChar() else: tc.insertText("// ") tc.movePosition(QtGui.QTextCursor.Down, QtGui.QTextCursor.MoveAnchor, 1) return if event.key() == Qt.Key_Tab and event.modifiers() == QtCore.Qt.ControlModifier: tc = self.textCursor() if tc.selection().isEmpty(): selected_line_num = 1 else: selected_line_num = tc.selection().toPlainText().count("\n") + 1 for i in range(selected_line_num): tc.movePosition(QtGui.QTextCursor.StartOfLine) if self.document().characterAt(tc.position()) == " ": tc.deleteChar() tc.deleteChar() tc.deleteChar() tc.deleteChar() tc.movePosition( QtGui.QTextCursor.Down, QtGui.QTextCursor.MoveAnchor, 1 ) return if event.key() == Qt.Key_Tab: tc = self.textCursor() if tc.selection().isEmpty(): selected_line_num = 1 else: selected_line_num = tc.selection().toPlainText().count("\n") + 1 for i in range(selected_line_num): tc.movePosition(QtGui.QTextCursor.StartOfLine) tc.insertText(" " * util.indent_width(self.lang)) tc.movePosition(QtGui.QTextCursor.Down, QtGui.QTextCursor.MoveAnchor, 1) return if event.key() == Qt.Key_K and event.modifiers() == QtCore.Qt.ControlModifier: self.remove_whole_line() return if ( event.modifiers() == QtCore.Qt.ControlModifier and event.key() == QtCore.Qt.Key_B ): self.go_to_declaration() return if event.key() == QtCore.Qt.Key_F2: self.go_to_first_error() return super().keyPressEvent(event) if event.key() == Qt.Key_Home: if self.textCursor().selectedText(): return if self.document().characterAt(self.textCursor().position()) == " ": cursor = self.textCursor() cursor.movePosition( QtGui.QTextCursor.NextWord, QtGui.QTextCursor.MoveAnchor, 1 ) self.setTextCursor(cursor) self.start_complete_process(event, tc)
def should_remove_indent(self) -> bool: tc = self.textCursor() for i in range(util.indent_width(self.lang)): if self.document().characterAt(tc.position() - i - 1) != " ": return False return True