Merged find next and prev functions, and added optrions for case sensitivity and whole words

This commit is contained in:
Veronica K. B. Olsen
2020-06-16 21:35:57 +02:00
parent 8755434f76
commit ad8463543e
+31 -30
View File
@@ -34,7 +34,7 @@ import nw
from time import time from time import time
from PyQt5.QtCore import Qt, QSize, QThread, QTimer, pyqtSlot from PyQt5.QtCore import Qt, QSize, QThread, QTimer, pyqtSlot, QRegExp
from PyQt5.QtGui import ( from PyQt5.QtGui import (
QTextCursor, QTextOption, QKeySequence, QFont, QColor, QPalette, QTextCursor, QTextOption, QKeySequence, QFont, QColor, QPalette,
QTextDocument, QCursor QTextDocument, QCursor
@@ -527,7 +527,7 @@ class GuiDocEditor(QTextEdit):
elif theAction == nwDocAction.GO_NEXT: elif theAction == nwDocAction.GO_NEXT:
self._findNext() self._findNext()
elif theAction == nwDocAction.GO_PREV: elif theAction == nwDocAction.GO_PREV:
self._findPrev() self._findNext(isBackward=True)
elif theAction == nwDocAction.REPL_NEXT: elif theAction == nwDocAction.REPL_NEXT:
self._replaceNext() self._replaceNext()
elif theAction == nwDocAction.BLOCK_H1: elif theAction == nwDocAction.BLOCK_H1:
@@ -1147,39 +1147,31 @@ class GuiDocEditor(QTextEdit):
self.docSearch.setReplaceText("") self.docSearch.setReplaceText("")
return return
def _findNext(self): def _findNext(self, isBackward=False):
"""Searches for the next occurrence of the search bar text in """Searches for the next or previous occurrence of the search
the document. Wraps back to the top if not found. bar text in the document. Wraps around if not found.
""" """
if not self.docSearch.isVisible(): if not self.docSearch.isVisible():
self._beginSearch() self._beginSearch()
return return
searchFor = self.docSearch.getSearchText() findOpt = QTextDocument.FindFlag(0)
wasFound = self.find(searchFor) if isBackward:
if not wasFound: findOpt |= QTextDocument.FindBackward
theCursor = self.textCursor() if self.docSearch.isCaseSense:
theCursor.movePosition(QTextCursor.Start) findOpt |= QTextDocument.FindCaseSensitively
self.setTextCursor(theCursor) if self.docSearch.isWholeWord:
self.find(searchFor) findOpt |= QTextDocument.FindWholeWords
return
def _findPrev(self):
"""Searches for the previous occurrence of the search bar text
in the document. Wraps back to the end if not found.
"""
if not self.docSearch.isVisible():
self._beginSearch()
return
searchFor = self.docSearch.getSearchText() searchFor = self.docSearch.getSearchText()
wasFound = self.find(searchFor, QTextDocument.FindBackward) wasFound = self.find(searchFor, findOpt)
if not wasFound: if not wasFound:
theCursor = self.textCursor() theCursor = self.textCursor()
theCursor.movePosition(QTextCursor.End) theCursor.movePosition(
QTextCursor.End if isBackward else QTextCursor.Start
)
self.setTextCursor(theCursor) self.setTextCursor(theCursor)
self.find(searchFor, QTextDocument.FindBackward) self.find(searchFor, findOpt)
return return
@@ -1195,18 +1187,24 @@ class GuiDocEditor(QTextEdit):
theCursor = self.textCursor() theCursor = self.textCursor()
searchFor = self.docSearch.getSearchText() searchFor = self.docSearch.getSearchText()
replWith = self.docSearch.getReplaceText() replWith = self.docSearch.getReplaceText()
if theCursor.hasSelection() and theCursor.selectedText() == searchFor: selText = theCursor.selectedText()
xPos = theCursor.selectionStart()
if not self.docSearch.isCaseSense:
searchFor = searchFor.lower()
selText = selText.lower()
if theCursor.hasSelection() and selText == searchFor:
theCursor.beginEditBlock() theCursor.beginEditBlock()
theCursor.removeSelectedText() theCursor.removeSelectedText()
theCursor.insertText(replWith) theCursor.insertText(replWith)
theCursor.endEditBlock() theCursor.endEditBlock()
theCursor.setPosition(xPos) theCursor.setPosition(theCursor.selectionEnd())
self.setTextCursor(theCursor) self.setTextCursor(theCursor)
logger.verbose("Replaced occurrence of '%s' with '%s' on line %d" % ( logger.verbose("Replaced occurrence of '%s' with '%s' on line %d" % (
searchFor, replWith, theCursor.blockNumber() searchFor, replWith, theCursor.blockNumber()
)) ))
if searchFor != "":
if searchFor:
self._findNext() self._findNext()
return return
@@ -1273,7 +1271,10 @@ class GuiDocEditSearch(QWidget):
self.theProject = docEditor.theProject self.theProject = docEditor.theProject
self.theTheme = docEditor.theTheme self.theTheme = docEditor.theTheme
self.repVisible = False self.repVisible = False
self.isCaseSense = False
self.isWholeWord = False
self.isRegEx = True
mPx = self.mainConf.pxInt(6) mPx = self.mainConf.pxInt(6)
fPx = int(0.9*self.theTheme.fontPixelSize) fPx = int(0.9*self.theTheme.fontPixelSize)