TextEditor: move setIfdefedOutBlocks to TextDocument

The location of the blocks to marked ifdefed out are not tied to a
specific editor instance, but just depend on the document content.

Change-Id: I837730dc00e1d6060dd46bbb2cfccbfa5f72e6ce
Reviewed-by: Christian Kandeler <christian.kandeler@qt.io>
This commit is contained in:
David Schulz
2021-12-03 11:19:59 +01:00
parent 76024f6fcc
commit 7552c9958f
6 changed files with 61 additions and 61 deletions

View File

@@ -2525,7 +2525,7 @@ static void semanticHighlighter(QFutureInterface<HighlightingResult> &future,
const Utils::FilePath &filePath,
const QList<ExpandedSemanticToken> &tokens,
const QString &docContents, const AstNode &ast,
const QPointer<TextEditorWidget> &widget,
const QPointer<TextDocument> &textDocument,
int docRevision, const QVersionNumber &clangdVersion)
{
ThreadedSubtaskTimer t("highlighting");
@@ -2677,9 +2677,9 @@ static void semanticHighlighter(QFutureInterface<HighlightingResult> &future,
auto results = QtConcurrent::blockingMapped<HighlightingResults>(tokens, toResult);
const QList<BlockRange> ifdefedOutBlocks = cleanupDisabledCode(results, &doc, docContents);
QMetaObject::invokeMethod(widget, [widget, ifdefedOutBlocks, docRevision] {
if (widget && widget->textDocument()->document()->revision() == docRevision)
widget->setIfdefedOutBlocks(ifdefedOutBlocks);
QMetaObject::invokeMethod(textDocument, [textDocument, ifdefedOutBlocks, docRevision] {
if (textDocument && textDocument->document()->revision() == docRevision)
textDocument->setIfdefedOutBlocks(ifdefedOutBlocks);
}, Qt::QueuedConnection);
ExtraHighlightingResultsCollector(future, results, filePath, ast, &doc, docContents).collect();
if (!future.isCanceled()) {
@@ -2756,10 +2756,9 @@ void ClangdClient::Private::handleSemanticTokens(TextDocument *doc,
const auto runner = [tokens, filePath = doc->filePath(),
text = doc->document()->toPlainText(), ast,
w = QPointer<TextEditorWidget>(widgetFromDocument(doc)),
rev = doc->document()->revision(),
doc = QPointer(doc), rev = doc->document()->revision(),
clangdVersion = q->versionNumber()] {
return Utils::runAsync(semanticHighlighter, filePath, tokens, text, ast, w, rev,
return Utils::runAsync(semanticHighlighter, filePath, tokens, text, ast, doc, rev,
clangdVersion);
};

View File

@@ -670,7 +670,7 @@ void CppEditorWidget::onIfdefedOutBlocksUpdated(unsigned revision,
{
if (revision != documentRevision())
return;
setIfdefedOutBlocks(ifdefedOutBlocks);
textDocument()->setIfdefedOutBlocks(ifdefedOutBlocks);
}
void CppEditorWidget::onShowInfoBarAction(const Id &id, bool show)

View File

@@ -484,6 +484,56 @@ bool TextDocument::applyChangeSet(const ChangeSet &changeSet)
return file->apply();
}
// the blocks list must be sorted
void TextDocument::setIfdefedOutBlocks(const QList<BlockRange> &blocks)
{
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) {
TextDocumentLayout::changeBraceDepth(block,braceDepthDelta);
TextDocumentLayout::changeFoldingIndent(block, braceDepthDelta); // ### C++ only, refactor!
}
block = block.next();
}
if (needUpdate)
documentLayout->requestUpdate();
}
const ExtraEncodingSettings &TextDocument::extraEncodingSettings() const
{
return d->m_extraEncodingSettings;

View File

@@ -49,6 +49,7 @@ QT_END_NAMESPACE
namespace TextEditor {
class BlockRange;
class CompletionAssistProvider;
class ExtraEncodingSettings;
class FontSettings;
@@ -103,6 +104,9 @@ public:
void autoFormat(const QTextCursor &cursor);
bool applyChangeSet(const Utils::ChangeSet &changeSet);
// the blocks list must be sorted
void setIfdefedOutBlocks(const QList<BlockRange> &blocks);
TextMarks marks() const;
bool addMark(TextMark *mark);
TextMarks marksAt(int line) const;

View File

@@ -6795,56 +6795,6 @@ QString TextEditorWidget::extraSelectionTooltip(int pos) const
return QString();
}
// the blocks list must be sorted
void TextEditorWidget::setIfdefedOutBlocks(const QList<BlockRange> &blocks)
{
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) {
TextDocumentLayout::changeBraceDepth(block,braceDepthDelta);
TextDocumentLayout::changeFoldingIndent(block, braceDepthDelta); // ### C++ only, refactor!
}
block = block.next();
}
if (needUpdate)
documentLayout->requestUpdate();
}
void TextEditorWidget::autoIndent()
{
MultiTextCursor cursor = multiTextCursor();

View File

@@ -331,9 +331,6 @@ public:
RefactorMarkers refactorMarkers() const;
void setRefactorMarkers(const RefactorMarkers &markers);
// the blocks list must be sorted
void setIfdefedOutBlocks(const QList<BlockRange> &blocks);
enum Side { Left, Right };
QAction *insertExtraToolBarWidget(Side side, QWidget *widget);