From 343e206b489caba11875ed1aa04b3ecdb0d643f5 Mon Sep 17 00:00:00 2001 From: Artem Sokolovskii Date: Thu, 8 Aug 2024 17:05:40 +0200 Subject: [PATCH] Syntaxhighlighter: Fix folding for function definition The issue was: when the user removes the function definition and insert it back folding isn't changed. Now it triggers rehighlighting of the next line. Change-Id: I5dcc67ed2e09a20d54e3a38c9605a4a24ad79177 Reviewed-by: David Schulz --- src/plugins/cppeditor/cpphighlighter.cpp | 17 ++++++++++++++++- src/plugins/fakevim/fakevim_test.cpp | 1 - 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/plugins/cppeditor/cpphighlighter.cpp b/src/plugins/cppeditor/cpphighlighter.cpp index 79853794480..66fb796dedb 100644 --- a/src/plugins/cppeditor/cpphighlighter.cpp +++ b/src/plugins/cppeditor/cpphighlighter.cpp @@ -264,6 +264,21 @@ void CppHighlighter::highlightBlock(const QString &text) } } + // rehighlight the next block if it contains a folding marker since we move the folding + // marker in some cases and we need to rehighlight the next block to update this floding indent + int rehighlightNextBlock = 0; + if (const QTextBlock nextBlock = currentBlock().next(); nextBlock.isValid()) { + if (const auto nextData = TextDocumentLayout::textUserData(nextBlock)) { + if (const auto foldingCheckData = TextDocumentLayout::textUserData(nextBlock.next())) { + if (foldingCheckData->foldingIndent() > nextData->foldingIndent()) { + static const int rehighlightNextBlockMask = 1 << 24; + if (!(currentBlockState() & rehighlightNextBlockMask)) + rehighlightNextBlock = rehighlightNextBlockMask; + } + } + } + } + // mark the trailing white spaces const int lastTokenEnd = tokens.last().utf16charsEnd(); if (text.length() > lastTokenEnd) @@ -284,7 +299,7 @@ void CppHighlighter::highlightBlock(const QString &text) TextDocumentLayout::setParentheses(currentBlock(), parentheses); TextDocumentLayout::setFoldingIndent(currentBlock(), foldingIndent); - setCurrentBlockState((braceDepth << 8) | tokenize.state()); + setCurrentBlockState(rehighlightNextBlock | (braceDepth << 8) | tokenize.state()); qCDebug(highlighterLog) << "storing brace depth" << braceDepth << "and folding indent" << foldingIndent; TextDocumentLayout::setExpectedRawStringSuffix(currentBlock(), diff --git a/src/plugins/fakevim/fakevim_test.cpp b/src/plugins/fakevim/fakevim_test.cpp index ac1f0a5797c..ed98c8230af 100644 --- a/src/plugins/fakevim/fakevim_test.cpp +++ b/src/plugins/fakevim/fakevim_test.cpp @@ -3011,7 +3011,6 @@ void FakeVimTester::test_vim_code_folding() "}" N ""); - NOT_IMPLEMENTED // Opening folds recursively isn't supported (previous position in fold isn't restored). }