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 2bfa16daa1.

Change-Id: I1c4efa5033ef4152e2d77dc2d99edbceac4e13c1
Reviewed-by: David Schulz <david.schulz@qt.io>
This commit is contained in:
Christian Kandeler
2024-12-11 15:50:38 +01:00
parent 25d0b21e8c
commit 65d9635ef7
3 changed files with 36 additions and 14 deletions

View File

@@ -79,6 +79,8 @@ CppEditorDocument::CppEditorDocument()
{ {
setId(CppEditor::Constants::CPPEDITOR_ID); setId(CppEditor::Constants::CPPEDITOR_ID);
resetSyntaxHighlighter([] { return new CppHighlighter(); }); resetSyntaxHighlighter([] { return new CppHighlighter(); });
connect(syntaxHighlighter(), &SyntaxHighlighter::finished,
this, &CppEditorDocument::applyIfdefedOutBlocks);
ICodeStylePreferencesFactory *factory ICodeStylePreferencesFactory *factory
= TextEditorSettings::codeStyleFactory(Constants::CPP_SETTINGS_ID); = TextEditorSettings::codeStyleFactory(Constants::CPP_SETTINGS_ID);
@@ -310,15 +312,15 @@ void CppEditorDocument::setExtraPreprocessorDirectives(const QByteArray &directi
void CppEditorDocument::setIfdefedOutBlocks(const QList<TextEditor::BlockRange> &blocks) void CppEditorDocument::setIfdefedOutBlocks(const QList<TextEditor::BlockRange> &blocks)
{ {
if (syntaxHighlighter() && !syntaxHighlighter()->syntaxHighlighterUpToDate()) { m_ifdefedOutBlocks = blocks;
connect(syntaxHighlighter(), applyIfdefedOutBlocks();
&SyntaxHighlighter::finished,
this,
[this, blocks] { setIfdefedOutBlocks(blocks); },
Qt::SingleShotConnection);
return;
} }
void CppEditorDocument::applyIfdefedOutBlocks()
{
if (!syntaxHighlighter() || !syntaxHighlighter()->syntaxHighlighterUpToDate())
return;
auto documentLayout = qobject_cast<TextDocumentLayout*>(document()->documentLayout()); auto documentLayout = qobject_cast<TextDocumentLayout*>(document()->documentLayout());
QTC_ASSERT(documentLayout, return); QTC_ASSERT(documentLayout, return);
@@ -328,8 +330,8 @@ void CppEditorDocument::setIfdefedOutBlocks(const QList<TextEditor::BlockRange>
int previousBraceDepth = 0; int previousBraceDepth = 0;
while (block.isValid()) { while (block.isValid()) {
bool resetToPrevious = false; bool resetToPrevious = false;
if (rangeNumber < blocks.size()) { if (rangeNumber < m_ifdefedOutBlocks.size()) {
const BlockRange &range = blocks.at(rangeNumber); const BlockRange &range = m_ifdefedOutBlocks.at(rangeNumber);
if (block.position() >= range.first() if (block.position() >= range.first()
&& ((block.position() + block.length() - 1) <= range.last() || !range.last())) { && ((block.position() + block.length() - 1) <= range.last() || !range.last())) {
TextDocumentLayout::setIfdefedOut(block); TextDocumentLayout::setIfdefedOut(block);
@@ -366,6 +368,10 @@ void CppEditorDocument::setIfdefedOutBlocks(const QList<TextEditor::BlockRange>
if (needUpdate) if (needUpdate)
documentLayout->requestUpdate(); documentLayout->requestUpdate();
#ifdef WITH_TESTS
emit ifdefedOutBlocksApplied();
#endif
} }
void CppEditorDocument::setPreferredParseContext(const QString &parseContextId) void CppEditorDocument::setPreferredParseContext(const QString &parseContextId)

View File

@@ -52,6 +52,10 @@ public:
bool usesClangd() const; bool usesClangd() const;
#ifdef WITH_TESTS
QList<TextEditor::BlockRange> ifdefedOutBlocks() const { return m_ifdefedOutBlocks; }
#endif
signals: signals:
void codeWarningsUpdated(unsigned contentsRevision, void codeWarningsUpdated(unsigned contentsRevision,
const QList<QTextEdit::ExtraSelection> selections, const QList<QTextEdit::ExtraSelection> selections,
@@ -65,6 +69,10 @@ signals:
void preprocessorSettingsChanged(bool customSettings); void preprocessorSettingsChanged(bool customSettings);
#ifdef WITH_TESTS
void ifdefedOutBlocksApplied();
#endif
protected: protected:
void applyFontSettings() override; void applyFontSettings() override;
Utils::Result saveImpl(const Utils::FilePath &filePath, bool autoSave) override; Utils::Result saveImpl(const Utils::FilePath &filePath, bool autoSave) override;
@@ -93,6 +101,7 @@ private:
void releaseResources(); void releaseResources();
void showHideInfoBarAboutMultipleParseContexts(bool show); void showHideInfoBarAboutMultipleParseContexts(bool show);
void applyIfdefedOutBlocks();
void initializeTimer(); void initializeTimer();
@@ -116,6 +125,7 @@ private:
ParseContextModel m_parseContextModel; ParseContextModel m_parseContextModel;
OutlineModel m_overviewModel; OutlineModel m_overviewModel;
QList<TextEditor::BlockRange> m_ifdefedOutBlocks;
}; };
} // namespace Internal } // namespace Internal

View File

@@ -4,6 +4,7 @@
#include "cpphighlighter.h" #include "cpphighlighter.h"
#include "cppdoxygen.h" #include "cppdoxygen.h"
#include "cppeditordocument.h"
#include "cppeditorlogging.h" #include "cppeditorlogging.h"
#include "cpptoolsreuse.h" #include "cpptoolsreuse.h"
@@ -13,7 +14,6 @@
#include <utils/textutils.h> #include <utils/textutils.h>
#include <cplusplus/SimpleLexer.h> #include <cplusplus/SimpleLexer.h>
#include <cplusplus/Lexer.h>
#include <QFile> #include <QFile>
#include <QTextCharFormat> #include <QTextCharFormat>
@@ -904,10 +904,16 @@ int main() { // 1,0
QCOMPARE(actual, expected); QCOMPARE(actual, expected);
} }
}; };
connect(testDocument.m_editorWidget, &CppEditorWidget::ifdefedOutBlocksChanged,
if (testDocument.m_editorWidget->cppEditorDocument()->ifdefedOutBlocks().isEmpty()) {
connect(testDocument.m_editorWidget->cppEditorDocument(),
&CppEditorDocument::ifdefedOutBlocksApplied,
this, check); this, check);
t.start(5000); t.start(5000);
QCOMPARE(loop.exec(), 0); QCOMPARE(loop.exec(), 0);
} else {
check();
}
} }
void cleanup() void cleanup()