Skip to content

Commit d03911a

Browse files
fix(editor): improve syntax highlighting handling and cleanup
- Removed unnecessary calls to highlight() in updateMark and OnUpdateHighlighter methods to streamline performance. - Enhanced reloadFileHighlight to ensure proper re-highlighting of visible text blocks without skipping userData. - Adjusted OnUpdateHighlighter to prevent re-highlighting of blocks with userData, improving efficiency. Log: 优化语法高亮处理,提升编辑器性能和稳定性。
1 parent c2e7e4f commit d03911a

2 files changed

Lines changed: 18 additions & 18 deletions

File tree

src/editor/dtextedit.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6694,8 +6694,6 @@ void TextEdit::updateMark(int from, int charsRemoved, int charsAdded)
66946694

66956695
//渲染所有的指定字符格式
66966696
renderAllSelections();
6697-
6698-
highlight();
66996697
qDebug() << "updateMark, completed";
67006698
}
67016699

@@ -7094,7 +7092,6 @@ void TextEdit::slotCanRedoChanged(bool bCanRedo)
70947092
Window *wnd = dynamic_cast<Window *>(m_wrapper->window());
70957093
if (wnd)
70967094
wnd->updateModifyStatus(m_sFilePath, isModified);
7097-
this->m_wrapper->OnUpdateHighlighter();
70987095
}
70997096

71007097
void TextEdit::slotCanUndoChanged(bool bCanUndo)
@@ -7105,7 +7102,6 @@ void TextEdit::slotCanUndoChanged(bool bCanUndo)
71057102
Window *wnd = dynamic_cast<Window *>(m_wrapper->window());
71067103
if (wnd)
71077104
wnd->updateModifyStatus(m_sFilePath, isModified);
7108-
this->m_wrapper->OnUpdateHighlighter();
71097105
}
71107106

71117107
bool TextEdit::containsExtraSelection(QList<QTextEdit::ExtraSelection> listSelections, QTextEdit::ExtraSelection selection)

src/editor/editwrapper.cpp

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,7 @@ void EditWrapper::reloadFileHighlight(QString definitionName)
455455
}
456456
if (m_pSyntaxHighlighter) m_pSyntaxHighlighter->setDefinition(m_Definition);
457457
m_pTextEdit->setSyntaxDefinition(m_Definition);
458+
m_pSyntaxHighlighter->setEnableHighlight(true);
458459

459460
// 获取当前展示区域文本块
460461
QPoint startPoint = QPoint(0, 0);
@@ -485,8 +486,14 @@ void EditWrapper::reloadFileHighlight(QString definitionName)
485486
// 复位标识位
486487
m_bHighlighterAll = false;
487488

488-
// 重新高亮当前界面
489-
OnUpdateHighlighter();
489+
// 底部栏切换语法高亮类型(如 Bash → Python)后,须强制重刷可见区域;
490+
// 不可调用 OnUpdateHighlighter(),因其会跳过已有 userData 的文本块而导致不着色
491+
for (QTextBlock var = beginBlock; var.isValid(); var = var.next()) {
492+
m_pSyntaxHighlighter->rehighlightBlock(var);
493+
if (var == endBlock) {
494+
break;
495+
}
496+
}
490497
} else {
491498
qDebug() << "EditWrapper reloadFileHighlight, m_Definition.isValid() && !m_Definition.filePath().isEmpty() is false";
492499
// 不允许的高亮格式或无对应的高亮格式文件,例如“None”,移除高亮效果
@@ -1128,6 +1135,7 @@ void EditWrapper::OnThemeChangeSlot(QString theme)
11281135
m_pSyntaxHighlighter->setTheme(m_Repository.defaultTheme(KSyntaxHighlighting::Repository::LightTheme));
11291136
qDebug() << "EditWrapper OnThemeChangeSlot, QColor(backgroundColor).lightness() >= 128, setTheme(LightTheme)";
11301137
}
1138+
m_pSyntaxHighlighter->setEnableHighlight(true);
11311139
m_pSyntaxHighlighter->rehighlight();
11321140
qDebug() << "EditWrapper OnThemeChangeSlot, m_pSyntaxHighlighter->rehighlight()";
11331141
}
@@ -1226,9 +1234,7 @@ void EditWrapper::OnUpdateHighlighter()
12261234

12271235
auto rehighlightBlock = [this](const QTextBlock &block) {
12281236
qDebug() << "EditWrapper OnUpdateHighlighter, rehighlightBlock";
1229-
m_pSyntaxHighlighter->setEnableHighlight(true);
12301237
m_pSyntaxHighlighter->rehighlightBlock(block);
1231-
m_pSyntaxHighlighter->setEnableHighlight(false);
12321238
};
12331239

12341240
if (foundBlock.isValid() && foundBlock < beginBlock) {
@@ -1245,13 +1251,15 @@ void EditWrapper::OnUpdateHighlighter()
12451251
}
12461252
}
12471253

1248-
for (QTextBlock var = beginBlock; var != endBlock; var = var.next()) {
1249-
qDebug() << "EditWrapper OnUpdateHighlighter, rehighlightBlock";
1250-
rehighlightBlock(var);
1254+
for (QTextBlock var = beginBlock; var.isValid(); var = var.next()) {
1255+
if (!var.userData()) {
1256+
qDebug() << "EditWrapper OnUpdateHighlighter, rehighlightBlock";
1257+
rehighlightBlock(var);
1258+
}
1259+
if (var == endBlock) {
1260+
break;
1261+
}
12511262
}
1252-
1253-
qDebug() << "EditWrapper OnUpdateHighlighter, rehighlightBlock";
1254-
rehighlightBlock(endBlock);
12551263
}
12561264
qDebug() << "EditWrapper OnUpdateHighlighter, exit";
12571265
}
@@ -1282,14 +1290,10 @@ void EditWrapper::updateHighlighterAll()
12821290
}
12831291

12841292
for (QTextBlock var = beginBlock; var != endBlock; var = var.next()) {
1285-
m_pSyntaxHighlighter->setEnableHighlight(true);
12861293
m_pSyntaxHighlighter->rehighlightBlock(var);
1287-
m_pSyntaxHighlighter->setEnableHighlight(false);
12881294
}
12891295

1290-
m_pSyntaxHighlighter->setEnableHighlight(true);
12911296
m_pSyntaxHighlighter->rehighlightBlock(endBlock);
1292-
m_pSyntaxHighlighter->setEnableHighlight(false);
12931297

12941298
m_bHighlighterAll = true;
12951299
}

0 commit comments

Comments
 (0)