Let project search start with selected text (#1789)
This commit is contained in:
@@ -382,8 +382,7 @@ class DocSearch:
|
||||
escaped += f"\\{c}"
|
||||
search = escaped
|
||||
if self._words:
|
||||
search = search if search.startswith("\\b") else f"\\b{search}"
|
||||
search = search if search.endswith("\\b") else f"{search}\\b"
|
||||
search = f"(?:^|\\b){search}(?:$|\\b)"
|
||||
return search
|
||||
|
||||
# END Class DocSearch
|
||||
|
||||
@@ -596,6 +596,15 @@ class GuiDocEditor(QPlainTextEdit):
|
||||
text = text.replace(nwUnicode.U_PSEP, "\n") # Paragraph separators
|
||||
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:
|
||||
"""Find the cursor position in the document. If the editor has a
|
||||
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)
|
||||
return
|
||||
|
||||
def setCursorSelection(self, selStart: int, selLength: int) -> None:
|
||||
def setCursorSelection(self, start: int, length: int) -> None:
|
||||
"""Make a text selection."""
|
||||
cursor = self.textCursor()
|
||||
cursor.setPosition(selStart, QTextCursor.MoveMode.MoveAnchor)
|
||||
cursor.setPosition(selStart + selLength, QTextCursor.MoveMode.KeepAnchor)
|
||||
self.setTextCursor(cursor)
|
||||
if start >= 0 and length > 0:
|
||||
cursor = self.textCursor()
|
||||
cursor.setPosition(start, QTextCursor.MoveMode.MoveAnchor)
|
||||
cursor.setPosition(start + length, QTextCursor.MoveMode.KeepAnchor)
|
||||
self.setTextCursor(cursor)
|
||||
return
|
||||
|
||||
##
|
||||
@@ -1280,11 +1290,7 @@ class GuiDocEditor(QPlainTextEdit):
|
||||
|
||||
def beginSearch(self) -> None:
|
||||
"""Set the selected text as the search text."""
|
||||
cursor = self.textCursor()
|
||||
if cursor.hasSelection():
|
||||
self.docSearch.setSearchText(cursor.selectedText())
|
||||
else:
|
||||
self.docSearch.setSearchText(None)
|
||||
self.docSearch.setSearchText(self.getSelectedText() or None)
|
||||
resS, _ = self.findAllOccurences()
|
||||
self.docSearch.setResultCount(None, len(resS))
|
||||
return
|
||||
@@ -2226,7 +2232,7 @@ class BackgroundWordCounter(QRunnable):
|
||||
"""
|
||||
self._isRunning = True
|
||||
if self._forSelection:
|
||||
text = self._docEditor.textCursor().selectedText()
|
||||
text = self._docEditor.getSelectedText()
|
||||
else:
|
||||
text = self._docEditor.getText()
|
||||
|
||||
|
||||
@@ -192,10 +192,13 @@ class GuiProjectSearch(QWidget):
|
||||
)
|
||||
return
|
||||
|
||||
def beginSearch(self) -> None:
|
||||
def beginSearch(self, text: str = "") -> None:
|
||||
"""Focus the search box and select its text, if any."""
|
||||
self.searchText.setFocus()
|
||||
self.searchText.selectAll()
|
||||
if text:
|
||||
self.searchText.setText(text.partition("\n")[0])
|
||||
self.searchText.selectAll()
|
||||
return
|
||||
|
||||
def closeProjectTasks(self) -> None:
|
||||
|
||||
@@ -1168,7 +1168,9 @@ class GuiMain(QMainWindow):
|
||||
elif view == nwView.SEARCH:
|
||||
self.mainStack.setCurrentWidget(self.splitMain)
|
||||
self.projStack.setCurrentWidget(self.projSearch)
|
||||
self.projSearch.beginSearch()
|
||||
self.projSearch.beginSearch(
|
||||
self.docEditor.getSelectedText() if self.docEditor.anyFocus() else ""
|
||||
)
|
||||
elif view == nwView.OUTLINE:
|
||||
self.mainStack.setCurrentWidget(self.outlineView)
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user