TextEditor: Move TextDocument::setIfdefedOutBlocks() to CppEditor

... where it belongs.

Change-Id: I4e7f344ed2d4af626965cf1f8a318de56a03a8bc
Reviewed-by: David Schulz <david.schulz@qt.io>
This commit is contained in:
Christian Kandeler
2024-06-18 17:47:31 +02:00
parent 0283b81392
commit f09a694cfc
13 changed files with 143 additions and 98 deletions

View File

@@ -9,18 +9,18 @@
#include "clangmodelmanagersupport.h" #include "clangmodelmanagersupport.h"
#include "tasktimers.h" #include "tasktimers.h"
#include <cppeditor/cppeditorwidget.h>
#include <cppeditor/semantichighlighter.h> #include <cppeditor/semantichighlighter.h>
#include <languageclient/languageclientmanager.h> #include <languageclient/languageclientmanager.h>
#include <languageclient/semantichighlightsupport.h> #include <languageclient/semantichighlightsupport.h>
#include <languageserverprotocol/lsptypes.h> #include <languageserverprotocol/lsptypes.h>
#include <texteditor/blockrange.h> #include <texteditor/blockrange.h>
#include <texteditor/texteditor.h>
#include <texteditor/textstyles.h> #include <texteditor/textstyles.h>
#include <QtConcurrent> #include <QtConcurrent>
#include <QTextDocument> #include <QTextDocument>
#include <new>
using namespace LanguageClient; using namespace LanguageClient;
using namespace LanguageServerProtocol; using namespace LanguageServerProtocol;
using namespace TextEditor; using namespace TextEditor;
@@ -256,6 +256,10 @@ void handleInactiveRegions(LanguageClient::Client *client, const JsonRpcMessage
params->uri().toFilePath(client->hostPathMapper())); params->uri().toFilePath(client->hostPathMapper()));
if (!doc) if (!doc)
return; return;
const auto editorWidget = CppEditor::CppEditorWidget::fromTextDocument(doc);
if (!editorWidget)
return;
const QList<Range> inactiveRegions = params->inactiveRegions(); const QList<Range> inactiveRegions = params->inactiveRegions();
QList<BlockRange> ifdefedOutBlocks; QList<BlockRange> ifdefedOutBlocks;
for (const Range &r : inactiveRegions) { for (const Range &r : inactiveRegions) {
@@ -263,7 +267,7 @@ void handleInactiveRegions(LanguageClient::Client *client, const JsonRpcMessage
const int endPos = r.end().toPositionInDocument(doc->document()) + 1; const int endPos = r.end().toPositionInDocument(doc->document()) + 1;
ifdefedOutBlocks.emplaceBack(startPos, endPos); ifdefedOutBlocks.emplaceBack(startPos, endPos);
} }
doc->setIfdefedOutBlocks(ifdefedOutBlocks); editorWidget->setIfdefedOutBlocks(ifdefedOutBlocks);
} }
QString inactiveRegionsMethodName() QString inactiveRegionsMethodName()

View File

@@ -835,7 +835,9 @@ void ClangdTestHighlighting::initTestCase()
{ {
ClangdTest::initTestCase(); ClangdTest::initTestCase();
connect(document("highlighting.cpp"), &TextDocument::ifdefedOutBlocksChanged, this, using CppEditor::CppEditorWidget;
connect(CppEditorWidget::fromTextDocument(document("highlighting.cpp")),
&CppEditorWidget::ifdefedOutBlocksChanged, this,
[this](const QList<BlockRange> &ranges) { m_ifdefedOutBlocks = ranges; }); [this](const QList<BlockRange> &ranges) { m_ifdefedOutBlocks = ranges; });
QTimer timer; QTimer timer;
timer.setSingleShot(true); timer.setSingleShot(true);

View File

@@ -38,6 +38,7 @@ add_qtc_plugin(CppEditor
cppeditortr.h cppeditortr.h
cppeditorconstants.h cppeditorconstants.h
cppeditordocument.cpp cppeditordocument.h cppeditordocument.cpp cppeditordocument.h
cppeditorlogging.cpp cppeditorlogging.h
cppeditoroutline.cpp cppeditoroutline.h cppeditoroutline.cpp cppeditoroutline.h
cppeditorplugin.cpp cppeditorplugin.cpp
cppeditorwidget.cpp cppeditorwidget.h cppeditorwidget.cpp cppeditorwidget.h

View File

@@ -84,6 +84,8 @@ QtcPlugin {
"cppdoxygen.cpp", "cppdoxygen.cpp",
"cppdoxygen.h", "cppdoxygen.h",
"cppdoxygen.kwgen", "cppdoxygen.kwgen",
"cppeditorlogging.cpp",
"cppeditorlogging.h",
"cppeditorwidget.cpp", "cppeditorwidget.cpp",
"cppeditorwidget.h", "cppeditorwidget.h",
"cppeditor.qrc", "cppeditor.qrc",

View File

@@ -6,6 +6,7 @@
#include "baseeditordocumentparser.h" #include "baseeditordocumentparser.h"
#include "cppcodeformatter.h" #include "cppcodeformatter.h"
#include "cppeditorconstants.h" #include "cppeditorconstants.h"
#include "cppeditorlogging.h"
#include "cppeditortr.h" #include "cppeditortr.h"
#include "cppmodelmanager.h" #include "cppmodelmanager.h"
#include "cppeditorconstants.h" #include "cppeditorconstants.h"
@@ -307,6 +308,64 @@ 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);
return;
}
auto documentLayout = qobject_cast<TextDocumentLayout*>(document()->documentLayout());
QTC_ASSERT(documentLayout, return);
QTextBlock block = document()->firstBlock();
bool needUpdate = false;
int rangeNumber = 0;
int braceDepthDelta = 0;
while (block.isValid()) {
bool cleared = false;
bool set = false;
if (rangeNumber < blocks.size()) {
const BlockRange &range = blocks.at(rangeNumber);
if (block.position() >= range.first()
&& ((block.position() + block.length() - 1) <= range.last() || !range.last()))
set = TextDocumentLayout::setIfdefedOut(block);
else
cleared = TextDocumentLayout::clearIfdefedOut(block);
if (block.contains(range.last()))
++rangeNumber;
} else {
cleared = TextDocumentLayout::clearIfdefedOut(block);
}
if (cleared || set) {
needUpdate = true;
int delta = TextDocumentLayout::braceDepthDelta(block);
if (cleared)
braceDepthDelta += delta;
else if (set)
braceDepthDelta -= delta;
}
if (braceDepthDelta) {
qCDebug(highlighterLog)
<< "changing brace depth and folding indent by" << braceDepthDelta << "for line"
<< (block.blockNumber() + 1) << "due to ifdefed out code";
TextDocumentLayout::changeBraceDepth(block,braceDepthDelta);
TextDocumentLayout::changeFoldingIndent(block, braceDepthDelta); // ### C++ only, refactor!
}
block = block.next();
}
if (needUpdate)
documentLayout->requestUpdate();
}
void CppEditorDocument::setPreferredParseContext(const QString &parseContextId) void CppEditorDocument::setPreferredParseContext(const QString &parseContextId)
{ {
const BaseEditorDocumentParser::Ptr parser = processor()->parser(); const BaseEditorDocumentParser::Ptr parser = processor()->parser();

View File

@@ -38,6 +38,9 @@ public:
void setPreferredParseContext(const QString &parseContextId); void setPreferredParseContext(const QString &parseContextId);
void setExtraPreprocessorDirectives(const QByteArray &directives); void setExtraPreprocessorDirectives(const QByteArray &directives);
// the blocks list must be sorted
void setIfdefedOutBlocks(const QList<TextEditor::BlockRange> &blocks);
void scheduleProcessDocument(); void scheduleProcessDocument();
ParseContextModel &parseContextModel(); ParseContextModel &parseContextModel();

View File

@@ -0,0 +1,10 @@
// Copyright (C) 2024 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
#include "cppeditorlogging.h"
namespace CppEditor::Internal {
Q_LOGGING_CATEGORY(highlighterLog, "qtc.cppeditor.syntaxhighlighter", QtWarningMsg)
} // namespace CppEditor::Internal

View File

@@ -0,0 +1,11 @@
// Copyright (C) 2024 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
#pragma once
#include <QLoggingCategory>
namespace CppEditor::Internal {
Q_DECLARE_LOGGING_CATEGORY(highlighterLog)
} // namespace CppEditor::Internal

View File

@@ -44,6 +44,7 @@
#include <texteditor/completionsettings.h> #include <texteditor/completionsettings.h>
#include <texteditor/fontsettings.h> #include <texteditor/fontsettings.h>
#include <texteditor/refactoroverlay.h> #include <texteditor/refactoroverlay.h>
#include <texteditor/syntaxhighlighter.h>
#include <texteditor/textdocument.h> #include <texteditor/textdocument.h>
#include <texteditor/textdocumentlayout.h> #include <texteditor/textdocumentlayout.h>
#include <texteditor/texteditorsettings.h> #include <texteditor/texteditorsettings.h>
@@ -419,6 +420,17 @@ CppEditorWidget::CppEditorWidget()
qRegisterMetaType<SemanticInfo>("SemanticInfo"); qRegisterMetaType<SemanticInfo>("SemanticInfo");
} }
CppEditorWidget *CppEditorWidget::fromTextDocument(TextEditor::TextDocument *doc)
{
const QVector<BaseTextEditor *> editors = BaseTextEditor::textEditorsForDocument(doc);
for (BaseTextEditor * const editor : editors) {
if (const auto editorWidget = qobject_cast<CppEditor::CppEditorWidget *>(
editor->editorWidget()))
return editorWidget;
}
return nullptr;
}
void CppEditorWidget::finalizeInitialization() void CppEditorWidget::finalizeInitialization()
{ {
d->m_cppEditorDocument = qobject_cast<CppEditorDocument *>(textDocument()); d->m_cppEditorDocument = qobject_cast<CppEditorDocument *>(textDocument());
@@ -594,7 +606,7 @@ void CppEditorWidget::onIfdefedOutBlocksUpdated(unsigned revision,
{ {
if (revision != documentRevision()) if (revision != documentRevision())
return; return;
textDocument()->setIfdefedOutBlocks(ifdefedOutBlocks); setIfdefedOutBlocks(ifdefedOutBlocks);
} }
void CppEditorWidget::findUsages() void CppEditorWidget::findUsages()
@@ -1484,6 +1496,14 @@ const QList<QTextEdit::ExtraSelection> CppEditorWidget::unselectLeadingWhitespac
return filtered; return filtered;
} }
void CppEditorWidget::setIfdefedOutBlocks(const QList<TextEditor::BlockRange> &blocks)
{
cppEditorDocument()->setIfdefedOutBlocks(blocks);
#ifdef WITH_TESTS
emit ifdefedOutBlocksChanged(blocks);
#endif
}
bool CppEditorWidget::isInTestMode() const { return d->inTestMode; } bool CppEditorWidget::isInTestMode() const { return d->inTestMode; }
#ifdef WITH_TESTS #ifdef WITH_TESTS

View File

@@ -14,6 +14,7 @@
#include <functional> #include <functional>
namespace TextEditor { namespace TextEditor {
class BlockRange;
class IAssistProposal; class IAssistProposal;
class IAssistProvider; class IAssistProvider;
} }
@@ -37,6 +38,8 @@ public:
CppEditorWidget(); CppEditorWidget();
~CppEditorWidget() override; ~CppEditorWidget() override;
static CppEditorWidget *fromTextDocument(TextEditor::TextDocument *doc);
Internal::CppEditorDocument *cppEditorDocument() const; Internal::CppEditorDocument *cppEditorDocument() const;
bool isSemanticInfoValidExceptLocalUses() const; bool isSemanticInfoValidExceptLocalUses() const;
@@ -83,6 +86,8 @@ public:
static const QList<QTextEdit::ExtraSelection> static const QList<QTextEdit::ExtraSelection>
unselectLeadingWhitespace(const QList<QTextEdit::ExtraSelection> &selections); unselectLeadingWhitespace(const QList<QTextEdit::ExtraSelection> &selections);
void setIfdefedOutBlocks(const QList<TextEditor::BlockRange> &blocks);
bool isInTestMode() const; bool isInTestMode() const;
void setProposals(const TextEditor::IAssistProposal *immediateProposal, void setProposals(const TextEditor::IAssistProposal *immediateProposal,
const TextEditor::IAssistProposal *finalProposal); const TextEditor::IAssistProposal *finalProposal);
@@ -91,6 +96,7 @@ public:
signals: signals:
void proposalsReady(const TextEditor::IAssistProposal *immediateProposal, void proposalsReady(const TextEditor::IAssistProposal *immediateProposal,
const TextEditor::IAssistProposal *finalProposal); const TextEditor::IAssistProposal *finalProposal);
void ifdefedOutBlocksChanged(const QList<TextEditor::BlockRange> &blocks);
#endif #endif
protected: protected:

View File

@@ -4,6 +4,7 @@
#include "cpphighlighter.h" #include "cpphighlighter.h"
#include "cppdoxygen.h" #include "cppdoxygen.h"
#include "cppeditorlogging.h"
#include "cpptoolsreuse.h" #include "cpptoolsreuse.h"
#include <extensionsystem/iplugin.h> #include <extensionsystem/iplugin.h>
@@ -15,7 +16,6 @@
#include <cplusplus/Lexer.h> #include <cplusplus/Lexer.h>
#include <QFile> #include <QFile>
#include <QLoggingCategory>
#include <QTextCharFormat> #include <QTextCharFormat>
#include <QTextDocument> #include <QTextDocument>
#include <QTextLayout> #include <QTextLayout>
@@ -28,8 +28,7 @@ using namespace TextEditor;
using namespace CPlusPlus; using namespace CPlusPlus;
namespace CppEditor { namespace CppEditor {
using namespace Internal;
static Q_LOGGING_CATEGORY(log, "qtc.cppeditor.syntaxhighlighter", QtWarningMsg)
CppHighlighter::CppHighlighter(QTextDocument *document) : CppHighlighter::CppHighlighter(QTextDocument *document) :
SyntaxHighlighter(document) SyntaxHighlighter(document)
@@ -39,16 +38,17 @@ CppHighlighter::CppHighlighter(QTextDocument *document) :
void CppHighlighter::highlightBlock(const QString &text) void CppHighlighter::highlightBlock(const QString &text)
{ {
qCDebug(log) << "highlighting line" << (currentBlock().blockNumber() + 1); qCDebug(highlighterLog) << "highlighting line" << (currentBlock().blockNumber() + 1);
const int previousBlockState_ = previousBlockState(); const int previousBlockState_ = previousBlockState();
int lexerState = 0, initialBraceDepth = 0; int lexerState = 0, initialBraceDepth = 0;
if (previousBlockState_ != -1) { if (previousBlockState_ != -1) {
lexerState = previousBlockState_ & 0xff; lexerState = previousBlockState_ & 0xff;
initialBraceDepth = previousBlockState_ >> 8; initialBraceDepth = previousBlockState_ >> 8;
qCDebug(log) << "initial brace depth carried over from previous block" << initialBraceDepth; qCDebug(highlighterLog) << "initial brace depth carried over from previous block"
<< initialBraceDepth;
} else { } else {
qCDebug(log) << "initial brace depth 0"; qCDebug(highlighterLog) << "initial brace depth 0";
} }
int braceDepth = initialBraceDepth; int braceDepth = initialBraceDepth;
@@ -69,9 +69,9 @@ void CppHighlighter::highlightBlock(const QString &text)
static const auto lexerStateWithoutNewLineExpectedBit = [](int state) { return state & ~0x80; }; static const auto lexerStateWithoutNewLineExpectedBit = [](int state) { return state & ~0x80; };
initialLexerState = lexerStateWithoutNewLineExpectedBit(initialLexerState); initialLexerState = lexerStateWithoutNewLineExpectedBit(initialLexerState);
int foldingIndent = initialBraceDepth; int foldingIndent = initialBraceDepth;
qCDebug(log) << "folding indent initialized to brace depth" << foldingIndent; qCDebug(highlighterLog) << "folding indent initialized to brace depth" << foldingIndent;
if (TextBlockUserData *userData = TextDocumentLayout::textUserData(currentBlock())) { if (TextBlockUserData *userData = TextDocumentLayout::textUserData(currentBlock())) {
qCDebug(log) << "resetting stored folding data for current block"; qCDebug(highlighterLog) << "resetting stored folding data for current block";
userData->setFoldingIndent(0); userData->setFoldingIndent(0);
userData->setFoldingStartIncluded(false); userData->setFoldingStartIncluded(false);
userData->setFoldingEndIncluded(false); userData->setFoldingEndIncluded(false);
@@ -90,7 +90,7 @@ void CppHighlighter::highlightBlock(const QString &text)
} }
TextDocumentLayout::setFoldingIndent(currentBlock(), foldingIndent); TextDocumentLayout::setFoldingIndent(currentBlock(), foldingIndent);
TextDocumentLayout::setExpectedRawStringSuffix(currentBlock(), inheritedRawStringSuffix); TextDocumentLayout::setExpectedRawStringSuffix(currentBlock(), inheritedRawStringSuffix);
qCDebug(log) << "no tokens, storing brace depth" << braceDepth << "and foldingIndent" qCDebug(highlighterLog) << "no tokens, storing brace depth" << braceDepth << "and foldingIndent"
<< foldingIndent; << foldingIndent;
return; return;
} }
@@ -133,7 +133,7 @@ void CppHighlighter::highlightBlock(const QString &text)
insertParen({Parenthesis::Opened, c, tk.utf16charsBegin()}); insertParen({Parenthesis::Opened, c, tk.utf16charsBegin()});
if (tk.is(T_LBRACE)) { if (tk.is(T_LBRACE)) {
++braceDepth; ++braceDepth;
qCDebug(log) << "encountered opening brace, increasing brace depth to" << braceDepth; qCDebug(highlighterLog) << "encountered opening brace, increasing brace depth to" << braceDepth;
// if a folding block opens at the beginning of a line, treat the line before // if a folding block opens at the beginning of a line, treat the line before
// as if it were inside the folding block except if it is a comment or the line does // as if it were inside the folding block except if it is a comment or the line does
@@ -147,7 +147,7 @@ void CppHighlighter::highlightBlock(const QString &text)
&& tk.utf16charsBegin() == firstNonSpace) { && tk.utf16charsBegin() == firstNonSpace) {
++foldingIndent; ++foldingIndent;
TextDocumentLayout::userData(currentBlock())->setFoldingStartIncluded(true); TextDocumentLayout::userData(currentBlock())->setFoldingStartIncluded(true);
qCDebug(log) qCDebug(highlighterLog)
<< "folding character is first on one line, increase folding indent to" << "folding character is first on one line, increase folding indent to"
<< foldingIndent << "and set foldingStartIncluded in stored data"; << foldingIndent << "and set foldingStartIncluded in stored data";
} }
@@ -157,16 +157,16 @@ void CppHighlighter::highlightBlock(const QString &text)
insertParen({Parenthesis::Closed, c, tk.utf16charsBegin()}); insertParen({Parenthesis::Closed, c, tk.utf16charsBegin()});
if (tk.is(T_RBRACE)) { if (tk.is(T_RBRACE)) {
--braceDepth; --braceDepth;
qCDebug(log) << "encountered closing brace, decreasing brace depth to" << braceDepth; qCDebug(highlighterLog) << "encountered closing brace, decreasing brace depth to" << braceDepth;
if (braceDepth < foldingIndent) { if (braceDepth < foldingIndent) {
// unless we are at the end of the block, we reduce the folding indent // unless we are at the end of the block, we reduce the folding indent
if (isLastToken || tokens.at(i + 1).is(T_SEMICOLON)) { if (isLastToken || tokens.at(i + 1).is(T_SEMICOLON)) {
qCDebug(log) << "token is last token in statement or line, setting " qCDebug(highlighterLog) << "token is last token in statement or line, setting "
"foldingEndIncluded in stored data"; "foldingEndIncluded in stored data";
TextDocumentLayout::userData(currentBlock())->setFoldingEndIncluded(true); TextDocumentLayout::userData(currentBlock())->setFoldingEndIncluded(true);
} else { } else {
foldingIndent = qMin(braceDepth, foldingIndent); foldingIndent = qMin(braceDepth, foldingIndent);
qCDebug(log) << "setting folding indent to minimum of current value and " qCDebug(highlighterLog) << "setting folding indent to minimum of current value and "
"brace depth, which is" "brace depth, which is"
<< foldingIndent; << foldingIndent;
} }
@@ -218,17 +218,17 @@ void CppHighlighter::highlightBlock(const QString &text)
if (initialLexerState && i == 0 && (tk.is(T_COMMENT) || tk.is(T_DOXY_COMMENT)) if (initialLexerState && i == 0 && (tk.is(T_COMMENT) || tk.is(T_DOXY_COMMENT))
&& (tokens.size() > 1 || !lexerState)) { && (tokens.size() > 1 || !lexerState)) {
--braceDepth; --braceDepth;
qCDebug(log) qCDebug(highlighterLog)
<< "encountered some comment-related condition, decreasing brace depth to" << "encountered some comment-related condition, decreasing brace depth to"
<< braceDepth; << braceDepth;
// unless we are at the end of the block, we reduce the folding indent // unless we are at the end of the block, we reduce the folding indent
if (isLastToken) { if (isLastToken) {
qCDebug(log) << "token is last token on line, setting " qCDebug(highlighterLog) << "token is last token on line, setting "
"foldingEndIncluded in stored data"; "foldingEndIncluded in stored data";
TextDocumentLayout::userData(currentBlock())->setFoldingEndIncluded(true); TextDocumentLayout::userData(currentBlock())->setFoldingEndIncluded(true);
} else { } else {
foldingIndent = qMin(braceDepth, foldingIndent); foldingIndent = qMin(braceDepth, foldingIndent);
qCDebug(log) << "setting folding indent to minimum of current value and " qCDebug(highlighterLog) << "setting folding indent to minimum of current value and "
"brace depth, which is" "brace depth, which is"
<< foldingIndent; << foldingIndent;
} }
@@ -272,7 +272,7 @@ void CppHighlighter::highlightBlock(const QString &text)
if (lastToken.is(T_COMMENT) || lastToken.is(T_DOXY_COMMENT)) { if (lastToken.is(T_COMMENT) || lastToken.is(T_DOXY_COMMENT)) {
insertParen({Parenthesis::Opened, QLatin1Char('+'), lastToken.utf16charsBegin()}); insertParen({Parenthesis::Opened, QLatin1Char('+'), lastToken.utf16charsBegin()});
++braceDepth; ++braceDepth;
qCDebug(log) qCDebug(highlighterLog)
<< "encountered some comment-related condition, increasing brace depth to" << "encountered some comment-related condition, increasing brace depth to"
<< braceDepth; << braceDepth;
} }
@@ -286,13 +286,13 @@ void CppHighlighter::highlightBlock(const QString &text)
userData && userData->ifdefedOut()) { userData && userData->ifdefedOut()) {
braceDepth = initialBraceDepth; braceDepth = initialBraceDepth;
foldingIndent = initialBraceDepth; foldingIndent = initialBraceDepth;
qCDebug(log) << "block is ifdefed out, resetting brace depth and folding indent to" qCDebug(highlighterLog) << "block is ifdefed out, resetting brace depth and folding indent to"
<< initialBraceDepth; << initialBraceDepth;
} }
TextDocumentLayout::setFoldingIndent(currentBlock(), foldingIndent); TextDocumentLayout::setFoldingIndent(currentBlock(), foldingIndent);
setCurrentBlockState((braceDepth << 8) | tokenize.state()); setCurrentBlockState((braceDepth << 8) | tokenize.state());
qCDebug(log) << "storing brace depth" << braceDepth << "and folding indent" << foldingIndent; qCDebug(highlighterLog) << "storing brace depth" << braceDepth << "and folding indent" << foldingIndent;
TextDocumentLayout::setExpectedRawStringSuffix(currentBlock(), TextDocumentLayout::setExpectedRawStringSuffix(currentBlock(),
tokenize.expectedRawStringSuffix()); tokenize.expectedRawStringSuffix());

View File

@@ -526,72 +526,6 @@ bool TextDocument::applyChangeSet(const ChangeSet &changeSet)
return PlainRefactoringFileFactory().file(filePath())->apply(changeSet); return PlainRefactoringFileFactory().file(filePath())->apply(changeSet);
} }
// the blocks list must be sorted
void TextDocument::setIfdefedOutBlocks(const QList<BlockRange> &blocks)
{
if (syntaxHighlighter() && !syntaxHighlighter()->syntaxHighlighterUpToDate()) {
connect(syntaxHighlighter(),
&SyntaxHighlighter::finished,
this,
[this, blocks] { setIfdefedOutBlocks(blocks); },
Qt::SingleShotConnection);
return;
}
QTextDocument *doc = document();
auto documentLayout = qobject_cast<TextDocumentLayout*>(doc->documentLayout());
QTC_ASSERT(documentLayout, return);
bool needUpdate = false;
QTextBlock block = doc->firstBlock();
int rangeNumber = 0;
int braceDepthDelta = 0;
while (block.isValid()) {
bool cleared = false;
bool set = false;
if (rangeNumber < blocks.size()) {
const BlockRange &range = blocks.at(rangeNumber);
if (block.position() >= range.first()
&& ((block.position() + block.length() - 1) <= range.last() || !range.last()))
set = TextDocumentLayout::setIfdefedOut(block);
else
cleared = TextDocumentLayout::clearIfdefedOut(block);
if (block.contains(range.last()))
++rangeNumber;
} else {
cleared = TextDocumentLayout::clearIfdefedOut(block);
}
if (cleared || set) {
needUpdate = true;
int delta = TextDocumentLayout::braceDepthDelta(block);
if (cleared)
braceDepthDelta += delta;
else if (set)
braceDepthDelta -= delta;
}
if (braceDepthDelta) {
qCDebug(Internal::foldingLog)
<< "changing brace depth and folding indent by" << braceDepthDelta << "for line"
<< (block.blockNumber() + 1) << "due to ifdefed out code";
TextDocumentLayout::changeBraceDepth(block,braceDepthDelta);
TextDocumentLayout::changeFoldingIndent(block, braceDepthDelta); // ### C++ only, refactor!
}
block = block.next();
}
if (needUpdate)
documentLayout->requestUpdate();
#ifdef WITH_TESTS
emit ifdefedOutBlocksChanged(blocks);
#endif
}
const ExtraEncodingSettings &TextDocument::extraEncodingSettings() const const ExtraEncodingSettings &TextDocument::extraEncodingSettings() const
{ {
return d->m_extraEncodingSettings; return d->m_extraEncodingSettings;

View File

@@ -84,9 +84,6 @@ public:
void autoFormat(const QTextCursor &cursor); void autoFormat(const QTextCursor &cursor);
bool applyChangeSet(const Utils::ChangeSet &changeSet); bool applyChangeSet(const Utils::ChangeSet &changeSet);
// the blocks list must be sorted
void setIfdefedOutBlocks(const QList<BlockRange> &blocks);
TextMarks marks() const; TextMarks marks() const;
bool addMark(TextMark *mark); bool addMark(TextMark *mark);
TextMarks marksAt(int line) const; TextMarks marksAt(int line) const;
@@ -162,10 +159,6 @@ signals:
void fontSettingsChanged(); void fontSettingsChanged();
void markRemoved(TextMark *mark); void markRemoved(TextMark *mark);
#ifdef WITH_TESTS
void ifdefedOutBlocksChanged(const QList<BlockRange> &blocks);
#endif
protected: protected:
virtual void applyFontSettings(); virtual void applyFontSettings();
bool saveImpl(QString *errorString, const Utils::FilePath &filePath, bool autoSave) override; bool saveImpl(QString *errorString, const Utils::FilePath &filePath, bool autoSave) override;