From 65d9635ef74fe188f9af6bef5fa65c244bb36b54 Mon Sep 17 00:00:00 2001 From: Christian Kandeler Date: Wed, 11 Dec 2024 15:50:38 +0100 Subject: [PATCH] CppEditor: Try harder to prevent code folding in ifdefed-out code The syntax highlighter can re-run after the semantic highlighter, overwriting its changes. Amends 2bfa16daa16bd578eb83561f76e14ad55e38678d. Change-Id: I1c4efa5033ef4152e2d77dc2d99edbceac4e13c1 Reviewed-by: David Schulz --- src/plugins/cppeditor/cppeditordocument.cpp | 24 +++++++++++++-------- src/plugins/cppeditor/cppeditordocument.h | 10 +++++++++ src/plugins/cppeditor/cpphighlighter.cpp | 16 +++++++++----- 3 files changed, 36 insertions(+), 14 deletions(-) diff --git a/src/plugins/cppeditor/cppeditordocument.cpp b/src/plugins/cppeditor/cppeditordocument.cpp index 297e1d81462..e05ee871497 100644 --- a/src/plugins/cppeditor/cppeditordocument.cpp +++ b/src/plugins/cppeditor/cppeditordocument.cpp @@ -79,6 +79,8 @@ CppEditorDocument::CppEditorDocument() { setId(CppEditor::Constants::CPPEDITOR_ID); resetSyntaxHighlighter([] { return new CppHighlighter(); }); + connect(syntaxHighlighter(), &SyntaxHighlighter::finished, + this, &CppEditorDocument::applyIfdefedOutBlocks); ICodeStylePreferencesFactory *factory = TextEditorSettings::codeStyleFactory(Constants::CPP_SETTINGS_ID); @@ -310,14 +312,14 @@ void CppEditorDocument::setExtraPreprocessorDirectives(const QByteArray &directi void CppEditorDocument::setIfdefedOutBlocks(const QList &blocks) { - if (syntaxHighlighter() && !syntaxHighlighter()->syntaxHighlighterUpToDate()) { - connect(syntaxHighlighter(), - &SyntaxHighlighter::finished, - this, - [this, blocks] { setIfdefedOutBlocks(blocks); }, - Qt::SingleShotConnection); + m_ifdefedOutBlocks = blocks; + applyIfdefedOutBlocks(); +} + +void CppEditorDocument::applyIfdefedOutBlocks() +{ + if (!syntaxHighlighter() || !syntaxHighlighter()->syntaxHighlighterUpToDate()) return; - } auto documentLayout = qobject_cast(document()->documentLayout()); QTC_ASSERT(documentLayout, return); @@ -328,8 +330,8 @@ void CppEditorDocument::setIfdefedOutBlocks(const QList int previousBraceDepth = 0; while (block.isValid()) { bool resetToPrevious = false; - if (rangeNumber < blocks.size()) { - const BlockRange &range = blocks.at(rangeNumber); + if (rangeNumber < m_ifdefedOutBlocks.size()) { + const BlockRange &range = m_ifdefedOutBlocks.at(rangeNumber); if (block.position() >= range.first() && ((block.position() + block.length() - 1) <= range.last() || !range.last())) { TextDocumentLayout::setIfdefedOut(block); @@ -366,6 +368,10 @@ void CppEditorDocument::setIfdefedOutBlocks(const QList if (needUpdate) documentLayout->requestUpdate(); + +#ifdef WITH_TESTS + emit ifdefedOutBlocksApplied(); +#endif } void CppEditorDocument::setPreferredParseContext(const QString &parseContextId) diff --git a/src/plugins/cppeditor/cppeditordocument.h b/src/plugins/cppeditor/cppeditordocument.h index 02946c60278..951805039ad 100644 --- a/src/plugins/cppeditor/cppeditordocument.h +++ b/src/plugins/cppeditor/cppeditordocument.h @@ -52,6 +52,10 @@ public: bool usesClangd() const; +#ifdef WITH_TESTS + QList ifdefedOutBlocks() const { return m_ifdefedOutBlocks; } +#endif + signals: void codeWarningsUpdated(unsigned contentsRevision, const QList selections, @@ -65,6 +69,10 @@ signals: void preprocessorSettingsChanged(bool customSettings); +#ifdef WITH_TESTS + void ifdefedOutBlocksApplied(); +#endif + protected: void applyFontSettings() override; Utils::Result saveImpl(const Utils::FilePath &filePath, bool autoSave) override; @@ -93,6 +101,7 @@ private: void releaseResources(); void showHideInfoBarAboutMultipleParseContexts(bool show); + void applyIfdefedOutBlocks(); void initializeTimer(); @@ -116,6 +125,7 @@ private: ParseContextModel m_parseContextModel; OutlineModel m_overviewModel; + QList m_ifdefedOutBlocks; }; } // namespace Internal diff --git a/src/plugins/cppeditor/cpphighlighter.cpp b/src/plugins/cppeditor/cpphighlighter.cpp index dedd644f64b..994a979e7c1 100644 --- a/src/plugins/cppeditor/cpphighlighter.cpp +++ b/src/plugins/cppeditor/cpphighlighter.cpp @@ -4,6 +4,7 @@ #include "cpphighlighter.h" #include "cppdoxygen.h" +#include "cppeditordocument.h" #include "cppeditorlogging.h" #include "cpptoolsreuse.h" @@ -13,7 +14,6 @@ #include #include -#include #include #include @@ -904,10 +904,16 @@ int main() { // 1,0 QCOMPARE(actual, expected); } }; - connect(testDocument.m_editorWidget, &CppEditorWidget::ifdefedOutBlocksChanged, - this, check); - t.start(5000); - QCOMPARE(loop.exec(), 0); + + if (testDocument.m_editorWidget->cppEditorDocument()->ifdefedOutBlocks().isEmpty()) { + connect(testDocument.m_editorWidget->cppEditorDocument(), + &CppEditorDocument::ifdefedOutBlocksApplied, + this, check); + t.start(5000); + QCOMPARE(loop.exec(), 0); + } else { + check(); + } } void cleanup()