Added block comment/uncomment feature to CodeEditor.

This commit is contained in:
Jeremy Mack Wright 2018-12-24 17:46:50 -05:00
parent e8cf3241de
commit 70836f43d2

View File

@ -214,16 +214,40 @@ class CodeEditor(QPlainTextEdit):
if event.key() == Qt.Key_Backtab and self.textCursor().hasSelection(): if event.key() == Qt.Key_Backtab and self.textCursor().hasSelection():
customKey = True customKey = True
selStart=self.textCursor().selectionStart() selStart = self.textCursor().selectionStart()
selEnd=self.textCursor().selectionEnd() selEnd = self.textCursor().selectionEnd()
cur=self.textCursor() cur = self.textCursor()
endBlock=self.document().findBlock(selEnd) endBlock = self.document().findBlock(selEnd)
currBlock=self.document().findBlock(selStart) currBlock = self.document().findBlock(selStart)
while currBlock.position() <= endBlock.position(): while currBlock.position() <= endBlock.position():
cur.setPosition(currBlock.position()) cur.setPosition(currBlock.position())
if currBlock.text().left(1) == "\t": if currBlock.text().left(1) == "\t":
cur.deleteChar() cur.deleteChar()
currBlock=currBlock.next() currBlock=currBlock.next()
# Allow commenting and uncommenting of blocks of code
if event.key() == Qt.Key_Slash and event.modifiers() == Qt.ControlModifier:
customKey = True
selStart = self.textCursor().selectionStart()
selEnd = self.textCursor().selectionEnd()
cur = self.textCursor()
endBlock = self.document().findBlock(selEnd)
currBlock = self.document().findBlock(selStart)
while currBlock.position() <= endBlock.position():
cur.setPosition(currBlock.position())
if currBlock.text()[0] == "#":
cur.deleteChar()
# Make sure we remove extra spaces
while currBlock.text()[0] == " ":
cur.deleteChar()
else:
cur.insertText("# ")
currBlock = currBlock.next()
if not customKey: if not customKey:
QPlainTextEdit.keyPressEvent(self, event) QPlainTextEdit.keyPressEvent(self, event)