Editor: check highlighter for comment chars before auto fold

The KSyntaxHighlighter Definitions provide single and multiline comment
markers. So check against those when trying to automatically fold the
first license header instead of assuming that '#' and "/*" are comment
markers.

Task-number: QTCREATORBUG-25882
Change-Id: I3a84cfc45445caad3b40feb996cfb781a5fb3190
Reviewed-by: Christian Stenger <christian.stenger@qt.io>
This commit is contained in:
David Schulz
2021-06-22 12:57:31 +02:00
parent 1a402984da
commit 693d54a069

View File

@@ -1445,7 +1445,22 @@ void TextEditorWidgetPrivate::foldLicenseHeader()
QString text = block.text(); QString text = block.text();
if (TextDocumentLayout::canFold(block) && block.next().isVisible()) { if (TextDocumentLayout::canFold(block) && block.next().isVisible()) {
const QString trimmedText = text.trimmed(); const QString trimmedText = text.trimmed();
if (trimmedText.startsWith("/*") || trimmedText.startsWith('#')) { QStringList commentMarker;
if (auto highlighter = qobject_cast<Highlighter *>(
q->textDocument()->syntaxHighlighter())) {
const Highlighter::Definition def = highlighter->definition();
for (const QString &marker :
{def.singleLineCommentMarker(), def.multiLineCommentMarker().first}) {
if (!marker.isEmpty())
commentMarker << marker;
}
} else {
commentMarker = QStringList({"/*", "#"});
}
if (Utils::anyOf(commentMarker, [&](const QString &marker) {
return trimmedText.startsWith(marker);
})) {
TextDocumentLayout::doFoldOrUnfold(block, false); TextDocumentLayout::doFoldOrUnfold(block, false);
moveCursorVisible(); moveCursorVisible();
documentLayout->requestUpdate(); documentLayout->requestUpdate();