From ad4803ccc872c491d31f9ff022777de63f3f5214 Mon Sep 17 00:00:00 2001 From: David Schulz Date: Mon, 21 Jun 2021 08:46:58 +0200 Subject: [PATCH] Editor: fix folding for Markdown Fixes: QTCREATORBUG-25882 Change-Id: I46048a191f4dd9bb2f7c3029f8555066bb8451ab Reviewed-by: Christian Stenger --- src/plugins/texteditor/highlighter.cpp | 27 ++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/src/plugins/texteditor/highlighter.cpp b/src/plugins/texteditor/highlighter.cpp index cea1b2f307e..84243a516d1 100644 --- a/src/plugins/texteditor/highlighter.cpp +++ b/src/plugins/texteditor/highlighter.cpp @@ -26,29 +26,32 @@ #include "highlighter.h" #include "highlightersettings.h" -#include "textdocumentlayout.h" #include "tabsettings.h" -#include "texteditorsettings.h" +#include "textdocumentlayout.h" #include "texteditor.h" +#include "texteditorsettings.h" #include #include #include #include -#include #include +#include #include -#include #include +#include #include #include #include +#include #include using namespace TextEditor; +static Q_LOGGING_CATEGORY(highlighterLog, "qtc.editor.highlighter", QtWarningMsg) + static const char kDefinitionForMimeType[] = "definitionForMimeType"; static const char kDefinitionForExtension[] = "definitionForExtension"; static const char kDefinitionForFilePath[] = "definitionForFilePath"; @@ -370,14 +373,22 @@ void Highlighter::applyFolding(int offset, const bool fromStart = TabSettings::firstNonSpace(text) == offset; const bool toEnd = (offset + length) == (text.length() - TabSettings::trailingWhitespaces(text)); if (region.type() == KSyntaxHighlighting::FoldingRegion::Begin) { - TextDocumentLayout::changeBraceDepth(block, 1); - // if there is only a folding begin in the line move the current block into the fold - if (fromStart && toEnd) { + const int newBraceDepth = TextDocumentLayout::braceDepth(block) + 1; + TextDocumentLayout::setBraceDepth(block, newBraceDepth); + qCDebug(highlighterLog) << "Found folding start from '" << offset << "' to '" << length + << "' resulting in the bracedepth '" << newBraceDepth << "' in :"; + qCDebug(highlighterLog) << text; + // if there is only a folding begin marker in the line move the current block into the fold + if (fromStart && toEnd && length <= 1) { data->setFoldingIndent(TextDocumentLayout::braceDepth(block)); data->setFoldingStartIncluded(true); } } else if (region.type() == KSyntaxHighlighting::FoldingRegion::End) { - TextDocumentLayout::changeBraceDepth(block, -1); + const int newBraceDepth = qMax(0, TextDocumentLayout::braceDepth(block) - 1); + qCDebug(highlighterLog) << "Found folding end from '" << offset << "' to '" << length + << "' resulting in the bracedepth '" << newBraceDepth << "' in :"; + qCDebug(highlighterLog) << text; + TextDocumentLayout::setBraceDepth(block, newBraceDepth); // if the folding end is at the end of the line move the current block into the fold if (toEnd) data->setFoldingEndIncluded(true);