Skip to content

Commit 1c0cae2

Browse files
fix: Add find highlight selection functionality
- Implemented setFindHighlightSelection method in TextEdit to set the current cursor as the highlight selection for find operations. - Updated Window class to utilize the new method when the current selection matches the search text, enhancing the search experience. log: Add find highlight selection functionality bug: https://pms.uniontech.com/bug-view-331021.html
1 parent dba598a commit 1c0cae2

3 files changed

Lines changed: 21 additions & 0 deletions

File tree

src/editor/dtextedit.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2237,6 +2237,13 @@ void TextEdit::clearFindMatchSelections()
22372237
m_findMatchSelections.clear();
22382238
}
22392239

2240+
void TextEdit::setFindHighlightSelection(const QTextCursor &cursor)
2241+
{
2242+
qDebug() << "Setting find highlight selection";
2243+
m_findHighlightSelection.cursor = cursor;
2244+
qDebug() << "Find highlight selection set to position:" << cursor.position() << "with selection:" << cursor.hasSelection();
2245+
}
2246+
22402247
void TextEdit::updateCursorKeywordSelection(QString keyword, bool findNext)
22412248
{
22422249
qDebug() << "Updating cursor keyword selection";

src/editor/dtextedit.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ class TextEdit : public DPlainTextEdit
204204
bool highlightKeyword(const QString &keyword, int position, Qt::CaseSensitivity caseFlag = Qt::CaseInsensitive);
205205
bool highlightKeywordInView(const QString &keyword, Qt::CaseSensitivity caseFlag = Qt::CaseInsensitive);
206206
void clearFindMatchSelections();
207+
void setFindHighlightSelection(const QTextCursor &cursor);
207208
void updateCursorKeywordSelection(QString keyword, bool findNext);
208209
void updateHighlightLineSelection();
209210
bool updateKeywordSelections(QString keyword, QTextCharFormat charFormat, QList<QTextEdit::ExtraSelection> &listSelection);

src/widgets/window.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3310,6 +3310,19 @@ void Window::slotSwitchToReplaceBar()
33103310
column = cursor.columnNumber();
33113311
scrollOffset = wrapper->textEditor()->verticalScrollBar()->value();
33123312
qDebug() << "Current position - file:" << currentFile << "row:" << row << "column:" << column;
3313+
if (!searchText.isEmpty()) {
3314+
// 检查当前光标是否有选中文本,且选中文本与搜索文本匹配
3315+
QTextCursor currentCursor = wrapper->textEditor()->textCursor();
3316+
if (currentCursor.hasSelection() && currentCursor.selectedText() == searchText) {
3317+
qDebug() << "Current selection matches search text, using it as search pointer";
3318+
// 直接将当前选中文本设置为搜索指针
3319+
wrapper->textEditor()->setFindHighlightSelection(currentCursor);
3320+
} else {
3321+
qDebug() << "No matching selection, setting search pointer from current position";
3322+
// 没有匹配的选中文本,从当前位置搜索
3323+
wrapper->textEditor()->updateCursorKeywordSelection(searchText, true);
3324+
}
3325+
}
33133326
}
33143327

33153328
// 隐藏查找栏

0 commit comments

Comments
 (0)