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);
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<TextEditor::BlockRange> &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<TextDocumentLayout*>(document()->documentLayout());
QTC_ASSERT(documentLayout, return);
@@ -328,8 +330,8 @@ void CppEditorDocument::setIfdefedOutBlocks(const QList<TextEditor::BlockRange>
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<TextEditor::BlockRange>
if (needUpdate)
documentLayout->requestUpdate();
#ifdef WITH_TESTS
emit ifdefedOutBlocksApplied();
#endif
}
void CppEditorDocument::setPreferredParseContext(const QString &parseContextId)

View File

@@ -52,6 +52,10 @@ public:
bool usesClangd() const;
#ifdef WITH_TESTS
QList<TextEditor::BlockRange> ifdefedOutBlocks() const { return m_ifdefedOutBlocks; }
#endif
signals:
void codeWarningsUpdated(unsigned contentsRevision,
const QList<QTextEdit::ExtraSelection> 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<TextEditor::BlockRange> m_ifdefedOutBlocks;
};
} // namespace Internal

View File

@@ -4,6 +4,7 @@
#include "cpphighlighter.h"
#include "cppdoxygen.h"
#include "cppeditordocument.h"
#include "cppeditorlogging.h"
#include "cpptoolsreuse.h"
@@ -13,7 +14,6 @@
#include <utils/textutils.h>
#include <cplusplus/SimpleLexer.h>
#include <cplusplus/Lexer.h>
#include <QFile>
#include <QTextCharFormat>
@@ -904,10 +904,16 @@ int main() { // 1,0
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);
t.start(5000);
QCOMPARE(loop.exec(), 0);
} else {
check();
}
}
void cleanup()