Fix toggle and select issue for markdown formatting (#1807)

This commit is contained in:
Veronica Berglyd Olsen
2024-04-09 16:40:38 +02:00
parent 7999251353
commit a8034b2fb5
+16 -15
View File
@@ -1470,7 +1470,7 @@ class GuiDocEditor(QPlainTextEdit):
# Internal Functions : Text Manipulation # Internal Functions : Text Manipulation
## ##
def _toggleFormat(self, fLen: int, fChar: str) -> bool: def _toggleFormat(self, fLen: int, fChar: str) -> None:
"""Toggle the formatting of a specific type for a piece of text. """Toggle the formatting of a specific type for a piece of text.
If more than one block is selected, the formatting is applied to If more than one block is selected, the formatting is applied to
the first block. the first block.
@@ -1488,12 +1488,12 @@ class GuiDocEditor(QPlainTextEdit):
posS = cursor.selectionStart() posS = cursor.selectionStart()
posE = cursor.selectionEnd() posE = cursor.selectionEnd()
if self._qDocument.characterAt(posO - 1) == fChar: if posS == posE and self._qDocument.characterAt(posO - 1) == fChar:
logger.warning("Format repetition, cancelling action") logger.warning("Format repetition, cancelling action")
cursor.clearSelection() cursor.clearSelection()
cursor.setPosition(posO) cursor.setPosition(posO)
self.setTextCursor(cursor) self.setTextCursor(cursor)
return False return
blockS = self._qDocument.findBlock(posS) blockS = self._qDocument.findBlock(posS)
blockE = self._qDocument.findBlock(posE) blockE = self._qDocument.findBlock(posE)
@@ -1519,7 +1519,6 @@ class GuiDocEditor(QPlainTextEdit):
break break
if fLen == min(numA, numB): if fLen == min(numA, numB):
cursor.clearSelection()
cursor.beginEditBlock() cursor.beginEditBlock()
cursor.setPosition(posS) cursor.setPosition(posS)
for i in range(fLen): for i in range(fLen):
@@ -1528,17 +1527,19 @@ class GuiDocEditor(QPlainTextEdit):
for i in range(fLen): for i in range(fLen):
cursor.deletePreviousChar() cursor.deletePreviousChar()
cursor.endEditBlock() cursor.endEditBlock()
cursor.clearSelection()
cursor.setPosition(posO - fLen) if select != _SelectAction.KEEP_SELECTION:
self.setTextCursor(cursor) cursor.clearSelection()
cursor.setPosition(posO - fLen)
self.setTextCursor(cursor)
else: else:
self._wrapSelection(fChar*fLen, pos=posO, select=select) self._wrapSelection(fChar*fLen, pos=posO, select=select)
return True return
def _wrapSelection(self, before: str, after: str | None = None, pos: int | None = None, def _wrapSelection(self, before: str, after: str | None = None, pos: int | None = None,
select: _SelectAction = _SelectAction.NO_DECISION) -> bool: select: _SelectAction = _SelectAction.NO_DECISION) -> None:
"""Wrap the selected text in whatever is in tBefore and tAfter. """Wrap the selected text in whatever is in tBefore and tAfter.
If there is no selection, the autoSelect setting decides the If there is no selection, the autoSelect setting decides the
action. AutoSelect will select the word under the cursor before action. AutoSelect will select the word under the cursor before
@@ -1578,21 +1579,21 @@ class GuiDocEditor(QPlainTextEdit):
if select == _SelectAction.MOVE_AFTER: if select == _SelectAction.MOVE_AFTER:
cursor.setPosition(posE + len(before + after)) cursor.setPosition(posE + len(before + after))
elif select == _SelectAction.KEEP_SELECTION: elif select == _SelectAction.KEEP_SELECTION:
cursor.setPosition(posE + len(before), QtMoveAnchor) cursor.setPosition(posS + len(before), QtMoveAnchor)
cursor.setPosition(posS + len(before), QtKeepAnchor) cursor.setPosition(posE + len(before), QtKeepAnchor)
elif select == _SelectAction.KEEP_POSITION: elif select == _SelectAction.KEEP_POSITION:
cursor.setPosition(posO + len(before)) cursor.setPosition(posO + len(before))
self.setTextCursor(cursor) self.setTextCursor(cursor)
return True return
def _replaceQuotes(self, sQuote: str, oQuote: str, cQuote: str) -> bool: def _replaceQuotes(self, sQuote: str, oQuote: str, cQuote: str) -> None:
"""Replace all straight quotes in the selected text.""" """Replace all straight quotes in the selected text."""
cursor = self.textCursor() cursor = self.textCursor()
if not cursor.hasSelection(): if not cursor.hasSelection():
SHARED.error(self.tr("Please select some text before calling replace quotes.")) SHARED.error(self.tr("Please select some text before calling replace quotes."))
return False return
posS = cursor.selectionStart() posS = cursor.selectionStart()
posE = cursor.selectionEnd() posE = cursor.selectionEnd()
@@ -1632,7 +1633,7 @@ class GuiDocEditor(QPlainTextEdit):
self._allowAutoReplace(True) self._allowAutoReplace(True)
return True return
def _processBlockFormat( def _processBlockFormat(
self, action: nwDocAction, text: str, toggle: bool = True self, action: nwDocAction, text: str, toggle: bool = True