diff --git a/src/plugins/clangcodemodel/clangdclient.cpp b/src/plugins/clangcodemodel/clangdclient.cpp index db7f1e5798c..97a31fa0fdb 100644 --- a/src/plugins/clangcodemodel/clangdclient.cpp +++ b/src/plugins/clangcodemodel/clangdclient.cpp @@ -2525,7 +2525,7 @@ static void semanticHighlighter(QFutureInterface &future, const Utils::FilePath &filePath, const QList &tokens, const QString &docContents, const AstNode &ast, - const QPointer &widget, + const QPointer &textDocument, int docRevision, const QVersionNumber &clangdVersion) { ThreadedSubtaskTimer t("highlighting"); @@ -2677,9 +2677,9 @@ static void semanticHighlighter(QFutureInterface &future, auto results = QtConcurrent::blockingMapped(tokens, toResult); const QList 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(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); }; diff --git a/src/plugins/cppeditor/cppeditorwidget.cpp b/src/plugins/cppeditor/cppeditorwidget.cpp index 6ef037d36be..8c9887b1de2 100644 --- a/src/plugins/cppeditor/cppeditorwidget.cpp +++ b/src/plugins/cppeditor/cppeditorwidget.cpp @@ -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) diff --git a/src/plugins/texteditor/textdocument.cpp b/src/plugins/texteditor/textdocument.cpp index 9462b06b829..4376cc17398 100644 --- a/src/plugins/texteditor/textdocument.cpp +++ b/src/plugins/texteditor/textdocument.cpp @@ -484,6 +484,56 @@ bool TextDocument::applyChangeSet(const ChangeSet &changeSet) return file->apply(); } +// the blocks list must be sorted +void TextDocument::setIfdefedOutBlocks(const QList &blocks) +{ + QTextDocument *doc = document(); + auto documentLayout = qobject_cast(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; diff --git a/src/plugins/texteditor/textdocument.h b/src/plugins/texteditor/textdocument.h index 26eab78ab46..71a9a120a6c 100644 --- a/src/plugins/texteditor/textdocument.h +++ b/src/plugins/texteditor/textdocument.h @@ -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 &blocks); + TextMarks marks() const; bool addMark(TextMark *mark); TextMarks marksAt(int line) const; diff --git a/src/plugins/texteditor/texteditor.cpp b/src/plugins/texteditor/texteditor.cpp index 28fe278cabd..a02a29b8fc4 100644 --- a/src/plugins/texteditor/texteditor.cpp +++ b/src/plugins/texteditor/texteditor.cpp @@ -6795,56 +6795,6 @@ QString TextEditorWidget::extraSelectionTooltip(int pos) const return QString(); } -// the blocks list must be sorted -void TextEditorWidget::setIfdefedOutBlocks(const QList &blocks) -{ - QTextDocument *doc = document(); - auto documentLayout = qobject_cast(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(); diff --git a/src/plugins/texteditor/texteditor.h b/src/plugins/texteditor/texteditor.h index 9d71130e742..2e702e20d28 100644 --- a/src/plugins/texteditor/texteditor.h +++ b/src/plugins/texteditor/texteditor.h @@ -331,9 +331,6 @@ public: RefactorMarkers refactorMarkers() const; void setRefactorMarkers(const RefactorMarkers &markers); - // the blocks list must be sorted - void setIfdefedOutBlocks(const QList &blocks); - enum Side { Left, Right }; QAction *insertExtraToolBarWidget(Side side, QWidget *widget);