Skip to content

Commit be4f295

Browse files
pengfeixxdeepin-bot[bot]
authored andcommitted
fix(editor): fix color mark lost when pressing Enter in middle of marked text
When inserting text inside a color mark, updateMark() splits the mark into two segments, leaving newly inserted characters in the gap between them, causing alternating highlight loss (e.g. "1,3" highlighted but "2,4" not). Remove the split logic since Qt auto-adjusts QTextCursor ranges to cover the new text. 在颜色标记中间按回车后,updateMark()将标记分割为两段,导致 新输入的字符落在间隙中,出现奇偶交替丢失高亮的问题。 移除分割逻辑,由Qt自动调整cursor范围即可正确覆盖。 Log: 修复颜色标记中间回车后新输入文本高亮丢失的问题 PMS: BUG-345731 Influence: 修复后,在颜色标记中间按回车换行继续输入文本时,新输入的文本将正确保持颜色高亮。
1 parent 7d6af5d commit be4f295

1 file changed

Lines changed: 3 additions & 66 deletions

File tree

src/editor/dtextedit.cpp

Lines changed: 3 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -6603,72 +6603,9 @@ void TextEdit::updateMark(int from, int charsRemoved, int charsAdded)
66036603
//如果字符添加在标记中
66046604
if (nCurrentPos > nStartPos && nCurrentPos < nEndPos) {
66056605
qDebug() << "updateMark, charsAdded > 0, nCurrentPos > nStartPos && nCurrentPos < nEndPos";
6606-
m_wordMarkSelections.removeAt(i);
6607-
selection.format.setBackground(strColor);
6608-
selection.cursor = textCursor();
6609-
6610-
QTextEdit::ExtraSelection preSelection;
6611-
6612-
//如果是输入法输入
6613-
if (m_bIsInputMethod) {
6614-
qDebug() << "updateMark, charsAdded > 0, m_bIsInputMethod";
6615-
//添加第一段标记
6616-
selection.cursor.setPosition(nStartPos, QTextCursor::MoveAnchor);
6617-
selection.cursor.setPosition(nCurrentPos - m_qstrCommitString.size(), QTextCursor::KeepAnchor);
6618-
m_wordMarkSelections.insert(i, QPair<QTextEdit::ExtraSelection, qint64>
6619-
(selection, timeStamp));
6620-
6621-
preSelection.cursor = selection.cursor;
6622-
preSelection.format = selection.format;
6623-
6624-
//添加第二段标记
6625-
selection.cursor.setPosition(nCurrentPos, QTextCursor::MoveAnchor);
6626-
selection.cursor.setPosition(nEndPos, QTextCursor::KeepAnchor);
6627-
m_wordMarkSelections.insert(i + 1, QPair<QTextEdit::ExtraSelection, qint64>
6628-
(selection, timeStamp));
6629-
6630-
m_bIsInputMethod = false;
6631-
} else {
6632-
qDebug() << "updateMark, charsAdded > 0, !m_bIsInputMethod";
6633-
//添加第一段标记
6634-
selection.cursor.setPosition(nStartPos, QTextCursor::MoveAnchor);
6635-
selection.cursor.setPosition(from, QTextCursor::KeepAnchor);
6636-
m_wordMarkSelections.insert(i, QPair<QTextEdit::ExtraSelection, qint64>
6637-
(selection, timeStamp));
6638-
6639-
preSelection.cursor = selection.cursor;
6640-
preSelection.format = selection.format;
6641-
6642-
//添加第二段标记
6643-
selection.cursor.setPosition(nCurrentPos, QTextCursor::MoveAnchor);
6644-
selection.cursor.setPosition(nEndPos, QTextCursor::KeepAnchor);
6645-
m_wordMarkSelections.insert(i + 1, QPair<QTextEdit::ExtraSelection, qint64>
6646-
(selection, timeStamp));
6647-
}
6648-
6649-
bool bIsFind = false;
6650-
qDebug() << "updateMark, charsAdded > 0, bIsFind";
6651-
//在记录标记的表中替换(按标记动作记录)
6652-
for (int j = 0; j < m_mapWordMarkSelections.count(); j++) {
6653-
auto list = m_mapWordMarkSelections.value(j);
6654-
for (int k = 0; k < list.count(); k++) {
6655-
if (list.value(k).cursor == wordMarkSelections.value(i).first.cursor
6656-
&& list.value(k).format == wordMarkSelections.value(i).first.format) {
6657-
list.removeAt(k);
6658-
listSelections = list;
6659-
listSelections.insert(k, preSelection);
6660-
listSelections.insert(k + 1, selection);
6661-
bIsFind = true;
6662-
break;
6663-
}
6664-
}
6665-
6666-
if (bIsFind) {
6667-
m_mapWordMarkSelections.remove(j);
6668-
m_mapWordMarkSelections.insert(j, listSelections);
6669-
break;
6670-
}
6671-
}
6606+
//Qt已自动调整cursor范围以包含新插入的文本,保持标记为整体不做分割,
6607+
//避免分割后新输入文本落在两段标记间隙中导致颜色丢失
6608+
//BUG-345731
66726609
break;
66736610

66746611
} else if (nCurrentPos == nEndPos) { //如果字符添加在标记后

0 commit comments

Comments
 (0)