Skip to content

Commit 0f01429

Browse files
pengfeixxdeepin-bot[bot]
authored andcommitted
fix(editor): prevent freeze when replacing newline characters
Add position advancement check in updateKeywordSelectionsInView while loop to prevent infinite loop when keyword contains newline. 在 updateKeywordSelectionsInView 的 while 循环中添加光标位置推进检查, 防止搜索关键词包含换行符时因光标未前进而导致死循环。 Log: 修复替换换行符时编辑器卡死的问题 PMS: BUG-332113 Influence: 修复后使用替换功能替换换行符不再导致编辑器卡死,替换功能恢复正常。
1 parent 3d50789 commit 0f01429

1 file changed

Lines changed: 11 additions & 1 deletion

File tree

src/editor/dtextedit.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2446,8 +2446,18 @@ bool TextEdit::updateKeywordSelectionsInView(QString keyword, QTextCharFormat ch
24462446
return false;
24472447
}
24482448

2449+
int lastMatchPos = -1;
24492450
while (!cursor.isNull()) {
24502451
extra.cursor = cursor;
2452+
int currentMatchPos = std::min(extra.cursor.position(), extra.cursor.anchor());
2453+
2454+
// 防止光标位置未前进导致死循环
2455+
if (currentMatchPos <= lastMatchPos) {
2456+
qDebug() << "Updating keyword selections in view, cursor not advancing, break";
2457+
break;
2458+
}
2459+
lastMatchPos = currentMatchPos;
2460+
24512461
// 调整为不区分大小写
24522462
Qt::CaseSensitivity option = defaultCaseSensitive;
24532463
/* 查找字符时,查找到完全相等的时候才高亮,如查找小写f时,大写的F不高亮 */
@@ -2465,7 +2475,7 @@ bool TextEdit::updateKeywordSelectionsInView(QString keyword, QTextCharFormat ch
24652475
cursor = document()->find(keyword, cursor, flags);
24662476
}
24672477

2468-
if (cursor.position() > endPos) {
2478+
if (!cursor.isNull() && cursor.position() > endPos) {
24692479
qDebug() << "Updating keyword selections in view with position greater than end pos, break";
24702480
break;
24712481
}

0 commit comments

Comments
 (0)