forked from qt-creator/qt-creator
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:
@@ -2525,7 +2525,7 @@ static void semanticHighlighter(QFutureInterface<HighlightingResult> &future,
|
|||||||
const Utils::FilePath &filePath,
|
const Utils::FilePath &filePath,
|
||||||
const QList<ExpandedSemanticToken> &tokens,
|
const QList<ExpandedSemanticToken> &tokens,
|
||||||
const QString &docContents, const AstNode &ast,
|
const QString &docContents, const AstNode &ast,
|
||||||
const QPointer<TextEditorWidget> &widget,
|
const QPointer<TextDocument> &textDocument,
|
||||||
int docRevision, const QVersionNumber &clangdVersion)
|
int docRevision, const QVersionNumber &clangdVersion)
|
||||||
{
|
{
|
||||||
ThreadedSubtaskTimer t("highlighting");
|
ThreadedSubtaskTimer t("highlighting");
|
||||||
@@ -2677,9 +2677,9 @@ static void semanticHighlighter(QFutureInterface<HighlightingResult> &future,
|
|||||||
|
|
||||||
auto results = QtConcurrent::blockingMapped<HighlightingResults>(tokens, toResult);
|
auto results = QtConcurrent::blockingMapped<HighlightingResults>(tokens, toResult);
|
||||||
const QList<BlockRange> ifdefedOutBlocks = cleanupDisabledCode(results, &doc, docContents);
|
const QList<BlockRange> ifdefedOutBlocks = cleanupDisabledCode(results, &doc, docContents);
|
||||||
QMetaObject::invokeMethod(widget, [widget, ifdefedOutBlocks, docRevision] {
|
QMetaObject::invokeMethod(textDocument, [textDocument, ifdefedOutBlocks, docRevision] {
|
||||||
if (widget && widget->textDocument()->document()->revision() == docRevision)
|
if (textDocument && textDocument->document()->revision() == docRevision)
|
||||||
widget->setIfdefedOutBlocks(ifdefedOutBlocks);
|
textDocument->setIfdefedOutBlocks(ifdefedOutBlocks);
|
||||||
}, Qt::QueuedConnection);
|
}, Qt::QueuedConnection);
|
||||||
ExtraHighlightingResultsCollector(future, results, filePath, ast, &doc, docContents).collect();
|
ExtraHighlightingResultsCollector(future, results, filePath, ast, &doc, docContents).collect();
|
||||||
if (!future.isCanceled()) {
|
if (!future.isCanceled()) {
|
||||||
@@ -2756,10 +2756,9 @@ void ClangdClient::Private::handleSemanticTokens(TextDocument *doc,
|
|||||||
|
|
||||||
const auto runner = [tokens, filePath = doc->filePath(),
|
const auto runner = [tokens, filePath = doc->filePath(),
|
||||||
text = doc->document()->toPlainText(), ast,
|
text = doc->document()->toPlainText(), ast,
|
||||||
w = QPointer<TextEditorWidget>(widgetFromDocument(doc)),
|
doc = QPointer(doc), rev = doc->document()->revision(),
|
||||||
rev = doc->document()->revision(),
|
|
||||||
clangdVersion = q->versionNumber()] {
|
clangdVersion = q->versionNumber()] {
|
||||||
return Utils::runAsync(semanticHighlighter, filePath, tokens, text, ast, w, rev,
|
return Utils::runAsync(semanticHighlighter, filePath, tokens, text, ast, doc, rev,
|
||||||
clangdVersion);
|
clangdVersion);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@@ -670,7 +670,7 @@ void CppEditorWidget::onIfdefedOutBlocksUpdated(unsigned revision,
|
|||||||
{
|
{
|
||||||
if (revision != documentRevision())
|
if (revision != documentRevision())
|
||||||
return;
|
return;
|
||||||
setIfdefedOutBlocks(ifdefedOutBlocks);
|
textDocument()->setIfdefedOutBlocks(ifdefedOutBlocks);
|
||||||
}
|
}
|
||||||
|
|
||||||
void CppEditorWidget::onShowInfoBarAction(const Id &id, bool show)
|
void CppEditorWidget::onShowInfoBarAction(const Id &id, bool show)
|
||||||
|
@@ -484,6 +484,56 @@ bool TextDocument::applyChangeSet(const ChangeSet &changeSet)
|
|||||||
return file->apply();
|
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
|
const ExtraEncodingSettings &TextDocument::extraEncodingSettings() const
|
||||||
{
|
{
|
||||||
return d->m_extraEncodingSettings;
|
return d->m_extraEncodingSettings;
|
||||||
|
@@ -49,6 +49,7 @@ QT_END_NAMESPACE
|
|||||||
|
|
||||||
namespace TextEditor {
|
namespace TextEditor {
|
||||||
|
|
||||||
|
class BlockRange;
|
||||||
class CompletionAssistProvider;
|
class CompletionAssistProvider;
|
||||||
class ExtraEncodingSettings;
|
class ExtraEncodingSettings;
|
||||||
class FontSettings;
|
class FontSettings;
|
||||||
@@ -103,6 +104,9 @@ 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;
|
||||||
|
@@ -6795,56 +6795,6 @@ QString TextEditorWidget::extraSelectionTooltip(int pos) const
|
|||||||
return QString();
|
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()
|
void TextEditorWidget::autoIndent()
|
||||||
{
|
{
|
||||||
MultiTextCursor cursor = multiTextCursor();
|
MultiTextCursor cursor = multiTextCursor();
|
||||||
|
@@ -331,9 +331,6 @@ public:
|
|||||||
RefactorMarkers refactorMarkers() const;
|
RefactorMarkers refactorMarkers() const;
|
||||||
void setRefactorMarkers(const RefactorMarkers &markers);
|
void setRefactorMarkers(const RefactorMarkers &markers);
|
||||||
|
|
||||||
// the blocks list must be sorted
|
|
||||||
void setIfdefedOutBlocks(const QList<BlockRange> &blocks);
|
|
||||||
|
|
||||||
enum Side { Left, Right };
|
enum Side { Left, Right };
|
||||||
QAction *insertExtraToolBarWidget(Side side, QWidget *widget);
|
QAction *insertExtraToolBarWidget(Side side, QWidget *widget);
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user