From 7ff72449616cba6afdd8f12078aab02bb65873fc Mon Sep 17 00:00:00 2001 From: Loren Eteval Date: Sun, 15 Oct 2023 13:41:57 +0800 Subject: [PATCH] Update Widget.py Signed-off-by: Loren Eteval --- Furious/Widget/Widget.py | 49 +++++++++++++++++++++------------------- 1 file changed, 26 insertions(+), 23 deletions(-) diff --git a/Furious/Widget/Widget.py b/Furious/Widget/Widget.py index 3599788..cb105e6 100644 --- a/Furious/Widget/Widget.py +++ b/Furious/Widget/Widget.py @@ -408,6 +408,30 @@ class ZoomablePlainTextEdit(QPlainTextEdit): return indent + def getPrevAndNextChar(self, cursor): + plainText = self.toPlainText() + + cursor.movePosition(QTextCursor.MoveOperation.Left) + + try: + prevChar = plainText[cursor.position()] + except Exception: + # Any non-exit exceptions + + prevChar = '' + + # Move the cursor to the next character position + cursor.movePosition(QTextCursor.MoveOperation.Right) + + try: + nextChar = plainText[cursor.position()] + except Exception: + # Any non-exit exceptions + + nextChar = '' + + return prevChar + nextChar + def smartIndent(self, event): cursor = self.textCursor() indent = self.getIndent(cursor.block().text()) @@ -443,31 +467,10 @@ class ZoomablePlainTextEdit(QPlainTextEdit): self.setTextCursor(cursor) def smartBackspace(self, event): - plainText = self.toPlainText() - cursor = self.textCursor() - cursor.movePosition(QTextCursor.MoveOperation.Left) + chPair = self.getPrevAndNextChar(cursor) - try: - prevChar = self.toPlainText()[cursor.position()] - except Exception: - # Any non-exit exceptions - - prevChar = '' - - # Move the cursor to the next character position - cursor.movePosition(QTextCursor.MoveOperation.Right) - - try: - nextChar = self.toPlainText()[cursor.position()] - except Exception: - # Any non-exit exceptions - - nextChar = '' - - pair = prevChar + nextChar - - if pair == '""' or pair == '{}' or pair == '[]': + if chPair == '""' or chPair == '{}' or chPair == '[]': cursor.deleteChar() cursor.deletePreviousChar() else: