Use selected text for project search (#1790)

This commit is contained in:
Veronica Berglyd Olsen
2024-04-02 22:59:43 +02:00
committed by GitHub
6 changed files with 27 additions and 20 deletions
+1 -2
View File
@@ -382,8 +382,7 @@ class DocSearch:
escaped += f"\\{c}" escaped += f"\\{c}"
search = escaped search = escaped
if self._words: if self._words:
search = search if search.startswith("\\b") else f"\\b{search}" search = f"(?:^|\\b){search}(?:$|\\b)"
search = search if search.endswith("\\b") else f"{search}\\b"
return search return search
# END Class DocSearch # END Class DocSearch
+17 -11
View File
@@ -596,6 +596,15 @@ class GuiDocEditor(QPlainTextEdit):
text = text.replace(nwUnicode.U_PSEP, "\n") # Paragraph separators text = text.replace(nwUnicode.U_PSEP, "\n") # Paragraph separators
return text return text
def getSelectedText(self) -> str:
"""Get currently selected text."""
if (cursor := self.textCursor()).hasSelection():
text = cursor.selectedText()
text = text.replace(nwUnicode.U_LSEP, "\n") # Line separators
text = text.replace(nwUnicode.U_PSEP, "\n") # Paragraph separators
return text
return ""
def getCursorPosition(self) -> int: def getCursorPosition(self) -> int:
"""Find the cursor position in the document. If the editor has a """Find the cursor position in the document. If the editor has a
selection, return the position of the end of the selection. selection, return the position of the end of the selection.
@@ -642,12 +651,13 @@ class GuiDocEditor(QPlainTextEdit):
logger.debug("Cursor moved to line %d", line) logger.debug("Cursor moved to line %d", line)
return return
def setCursorSelection(self, selStart: int, selLength: int) -> None: def setCursorSelection(self, start: int, length: int) -> None:
"""Make a text selection.""" """Make a text selection."""
cursor = self.textCursor() if start >= 0 and length > 0:
cursor.setPosition(selStart, QTextCursor.MoveMode.MoveAnchor) cursor = self.textCursor()
cursor.setPosition(selStart + selLength, QTextCursor.MoveMode.KeepAnchor) cursor.setPosition(start, QTextCursor.MoveMode.MoveAnchor)
self.setTextCursor(cursor) cursor.setPosition(start + length, QTextCursor.MoveMode.KeepAnchor)
self.setTextCursor(cursor)
return return
## ##
@@ -1280,11 +1290,7 @@ class GuiDocEditor(QPlainTextEdit):
def beginSearch(self) -> None: def beginSearch(self) -> None:
"""Set the selected text as the search text.""" """Set the selected text as the search text."""
cursor = self.textCursor() self.docSearch.setSearchText(self.getSelectedText() or None)
if cursor.hasSelection():
self.docSearch.setSearchText(cursor.selectedText())
else:
self.docSearch.setSearchText(None)
resS, _ = self.findAllOccurences() resS, _ = self.findAllOccurences()
self.docSearch.setResultCount(None, len(resS)) self.docSearch.setResultCount(None, len(resS))
return return
@@ -2226,7 +2232,7 @@ class BackgroundWordCounter(QRunnable):
""" """
self._isRunning = True self._isRunning = True
if self._forSelection: if self._forSelection:
text = self._docEditor.textCursor().selectedText() text = self._docEditor.getSelectedText()
else: else:
text = self._docEditor.getText() text = self._docEditor.getText()
+4 -1
View File
@@ -192,10 +192,13 @@ class GuiProjectSearch(QWidget):
) )
return return
def beginSearch(self) -> None: def beginSearch(self, text: str = "") -> None:
"""Focus the search box and select its text, if any.""" """Focus the search box and select its text, if any."""
self.searchText.setFocus() self.searchText.setFocus()
self.searchText.selectAll() self.searchText.selectAll()
if text:
self.searchText.setText(text.partition("\n")[0])
self.searchText.selectAll()
return return
def closeProjectTasks(self) -> None: def closeProjectTasks(self) -> None:
+3 -1
View File
@@ -1168,7 +1168,9 @@ class GuiMain(QMainWindow):
elif view == nwView.SEARCH: elif view == nwView.SEARCH:
self.mainStack.setCurrentWidget(self.splitMain) self.mainStack.setCurrentWidget(self.splitMain)
self.projStack.setCurrentWidget(self.projSearch) self.projStack.setCurrentWidget(self.projSearch)
self.projSearch.beginSearch() self.projSearch.beginSearch(
self.docEditor.getSelectedText() if self.docEditor.anyFocus() else ""
)
elif view == nwView.OUTLINE: elif view == nwView.OUTLINE:
self.mainStack.setCurrentWidget(self.outlineView) self.mainStack.setCurrentWidget(self.outlineView)
return return
+1 -4
View File
@@ -439,10 +439,7 @@ def testCoreTools_DocSearch(monkeypatch, mockGUI, fncPath, mockRnd, ipsumText):
# Whole Words # Whole Words
search.setWholeWords(True) search.setWholeWords(True)
search.setUserRegEx(True) search.setUserRegEx(True)
assert search._buildPattern("Hi") == r"\bHi\b" assert search._buildPattern("Hi") == r"(?:^|\b)Hi(?:$|\b)"
assert search._buildPattern(r"\bHi") == r"\bHi\b"
assert search._buildPattern(r"Hi\b") == r"\bHi\b"
assert search._buildPattern(r"\bHi\b") == r"\bHi\b"
search.setWholeWords(False) search.setWholeWords(False)
search.setUserRegEx(False) search.setUserRegEx(False)
+1 -1
View File
@@ -117,7 +117,7 @@ def testGuiDocSearch_Main(qtbot, monkeypatch, nwGUI, prjLipsum):
# RegEx # RegEx
search.toggleRegEx.setChecked(True) search.toggleRegEx.setChecked(True)
search.searchText.setText("(dolor|dolorem)") search.beginSearch("(dolor|dolorem)")
search.searchAction.activate(QAction.ActionEvent.Trigger) search.searchAction.activate(QAction.ActionEvent.Trigger)
assert search.searchResult.topLevelItemCount() == 10 assert search.searchResult.topLevelItemCount() == 10
assert totalCount() == 34 assert totalCount() == 34