Preserve case repalce now works

This commit is contained in:
Veronica K. B. Olsen
2020-06-17 17:15:30 +02:00
parent c3c0bd7abe
commit 0ddb7ad1de
2 changed files with 33 additions and 4 deletions
+21
View File
@@ -172,3 +172,24 @@ def splitVersionNumber(vString):
vInt = vMajor*10000 + vMinor*100 + vPatch vInt = vMajor*10000 + vMinor*100 + vPatch
return [vMajor, vMinor, vPatch, vInt] return [vMajor, vMinor, vPatch, vInt]
def transferCase(theSource, theTarget):
"""Transfers the case of the source word to the target word. This
will consider all upper or lower, and first char capitalisation.
"""
theResult = theTarget
if not isinstance(theSource, str) or not isinstance(theTarget, str):
return theResult
if len(theTarget) < 1 or len(theSource) < 1:
return theResult
if theSource[0] == theSource[0].upper():
theResult = theTarget[0].upper() + theTarget[1:]
if theSource == theSource.upper():
theResult = theTarget.upper()
elif theSource == theSource.lower():
theResult = theTarget.lower()
return theResult
+12 -4
View File
@@ -48,6 +48,7 @@ from nw.core import NWDoc
from nw.gui.dochighlight import GuiDocHighlighter from nw.gui.dochighlight import GuiDocHighlighter
from nw.core import NWSpellSimple, countWords from nw.core import NWSpellSimple, countWords
from nw.constants import nwUnicode, nwDocAction from nw.constants import nwUnicode, nwDocAction
from nw.common import transferCase
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@@ -1185,16 +1186,23 @@ class GuiDocEditor(QTextEdit):
self._beginSearch() self._beginSearch()
return return
if not theCursor.hasSelection():
return
theCursor = self.textCursor() theCursor = self.textCursor()
searchFor = self.docSearch.getSearchText() searchFor = self.docSearch.getSearchText()
replWith = self.docSearch.getReplaceText() replWith = self.docSearch.getReplaceText()
selText = theCursor.selectedText() selText = theCursor.selectedText()
if not self.docSearch.isCaseSense: if self.docSearch.doPreserve:
searchFor = searchFor.lower() replWith = transferCase(selText, replWith)
selText = selText.lower()
if theCursor.hasSelection() and selText == searchFor: if not self.docSearch.isCaseSense:
isMatch = searchFor.lower() == selText.lower()
else:
isMatch = searchFor == selText
if isMatch:
theCursor.beginEditBlock() theCursor.beginEditBlock()
theCursor.removeSelectedText() theCursor.removeSelectedText()
theCursor.insertText(replWith) theCursor.insertText(replWith)