def keyPressEvent(self, event):
key = event.key()
if key == Qt.Key_Return:
cursor = self.textCursor()
indentLvl = self.findLineIndentLevel()
newBlock = False
pos = cursor.position()
cursor.movePosition(QTextCursor.PreviousCharacter,
QTextCursor.KeepAnchor)
if cursor.selectedText() == self.openBlockDelimiter:
# We don't add a closing tag if there is text right
# below with the same indentation level because in
# that case the user might just be looking to add a
# new line
ok = cursor.movePosition(QTextCursor.Down)
if ok:
downIndentLvl = self.findLineIndentLevel(cursor)
cursor.select(QTextCursor.LineUnderCursor)
if (cursor.selectedText().strip(
) == '' or downIndentLvl <= indentLvl):
newBlock = True
cursor.movePosition(QTextCursor.Up)
else:
newBlock = True
indentLvl += 1
if newBlock:
cursor.select(QTextCursor.LineUnderCursor)
txt = cursor.selectedText().lstrip(" ").split(" ")
if len(txt) > 1:
if len(txt) < 3 and txt[-1][-1] == self.openBlockDelimiter:
feature = txt[-1][:-1]
else:
feature = txt[1]
else:
feature = None
cursor.setPosition(pos)
cursor.beginEditBlock()
cursor.insertText("\n")
newLineSpace = "".join(self._indent for _ in range(indentLvl))
cursor.insertText(newLineSpace)
if newBlock:
cursor.insertText("\n")
newLineSpace = "".join((newLineSpace[:-len(self._indent)],
"} ", feature, ";"))
cursor.insertText(newLineSpace)
cursor.movePosition(QTextCursor.Up)
cursor.movePosition(QTextCursor.EndOfLine)
self.setTextCursor(cursor)
cursor.endEditBlock()
else:
super(FeatureCodeEditor, self).keyPressEvent(event)
评论列表
文章目录