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
##
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.
If more than one block is selected, the formatting is applied to
the first block.
@@ -1488,12 +1488,12 @@ class GuiDocEditor(QPlainTextEdit):
posS = cursor.selectionStart()
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")
cursor.clearSelection()
cursor.setPosition(posO)
self.setTextCursor(cursor)
return False
return
blockS = self._qDocument.findBlock(posS)
blockE = self._qDocument.findBlock(posE)
@@ -1519,7 +1519,6 @@ class GuiDocEditor(QPlainTextEdit):
break
if fLen == min(numA, numB):
cursor.clearSelection()
cursor.beginEditBlock()
cursor.setPosition(posS)
for i in range(fLen):
@@ -1528,17 +1527,19 @@ class GuiDocEditor(QPlainTextEdit):
for i in range(fLen):
cursor.deletePreviousChar()
cursor.endEditBlock()
cursor.clearSelection()
cursor.setPosition(posO - fLen)
self.setTextCursor(cursor)
if select != _SelectAction.KEEP_SELECTION:
cursor.clearSelection()
cursor.setPosition(posO - fLen)
self.setTextCursor(cursor)
else:
self._wrapSelection(fChar*fLen, pos=posO, select=select)
return True
return
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.
If there is no selection, the autoSelect setting decides the
action. AutoSelect will select the word under the cursor before
@@ -1578,21 +1579,21 @@ class GuiDocEditor(QPlainTextEdit):
if select == _SelectAction.MOVE_AFTER:
cursor.setPosition(posE + len(before + after))
elif select == _SelectAction.KEEP_SELECTION:
cursor.setPosition(posE + len(before), QtMoveAnchor)
cursor.setPosition(posS + len(before), QtKeepAnchor)
cursor.setPosition(posS + len(before), QtMoveAnchor)
cursor.setPosition(posE + len(before), QtKeepAnchor)
elif select == _SelectAction.KEEP_POSITION:
cursor.setPosition(posO + len(before))
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."""
cursor = self.textCursor()
if not cursor.hasSelection():
SHARED.error(self.tr("Please select some text before calling replace quotes."))
return False
return
posS = cursor.selectionStart()
posE = cursor.selectionEnd()
@@ -1632,7 +1633,7 @@ class GuiDocEditor(QPlainTextEdit):
self._allowAutoReplace(True)
return True
return
def _processBlockFormat(
self, action: nwDocAction, text: str, toggle: bool = True