From 693d54a069d4f040e9d914ac205ddcff537599f1 Mon Sep 17 00:00:00 2001 From: David Schulz Date: Tue, 22 Jun 2021 12:57:31 +0200 Subject: [PATCH] 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 --- src/plugins/texteditor/texteditor.cpp | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/plugins/texteditor/texteditor.cpp b/src/plugins/texteditor/texteditor.cpp index d3061d97e1f..db7eb38b350 100644 --- a/src/plugins/texteditor/texteditor.cpp +++ b/src/plugins/texteditor/texteditor.cpp @@ -1445,7 +1445,22 @@ void TextEditorWidgetPrivate::foldLicenseHeader() QString text = block.text(); if (TextDocumentLayout::canFold(block) && block.next().isVisible()) { const QString trimmedText = text.trimmed(); - if (trimmedText.startsWith("/*") || trimmedText.startsWith('#')) { + QStringList commentMarker; + if (auto highlighter = qobject_cast( + 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); moveCursorVisible(); documentLayout->requestUpdate();