CppEditor: remove unneeded indirection for the outline model

Change-Id: I01e9fb0dae30d6df22c0f5d7255ab247c48749fc
Reviewed-by: <github-actions-qt-creator@cristianadam.eu>
Reviewed-by: Christian Kandeler <christian.kandeler@qt.io>
This commit is contained in:
David Schulz
2022-08-15 09:42:49 +02:00
parent 8703bcd777
commit b39c7cd782
15 changed files with 86 additions and 150 deletions

View File

@@ -38,7 +38,6 @@
#include <coreplugin/icore.h> #include <coreplugin/icore.h>
#include <coreplugin/messagemanager.h> #include <coreplugin/messagemanager.h>
#include <cppeditor/abstractoverviewmodel.h>
#include <cppeditor/cppcodemodelsettings.h> #include <cppeditor/cppcodemodelsettings.h>
#include <cppeditor/cppeditorconstants.h> #include <cppeditor/cppeditorconstants.h>
#include <cppeditor/cppeditorwidget.h> #include <cppeditor/cppeditorwidget.h>

View File

@@ -5,7 +5,6 @@ add_qtc_plugin(CppEditor
PLUGIN_TEST_DEPENDS QbsProjectManager QmakeProjectManager PLUGIN_TEST_DEPENDS QbsProjectManager QmakeProjectManager
SOURCES SOURCES
abstracteditorsupport.cpp abstracteditorsupport.h abstracteditorsupport.cpp abstracteditorsupport.h
abstractoverviewmodel.h
baseeditordocumentparser.cpp baseeditordocumentparser.h baseeditordocumentparser.cpp baseeditordocumentparser.h
baseeditordocumentprocessor.cpp baseeditordocumentprocessor.h baseeditordocumentprocessor.cpp baseeditordocumentprocessor.h
builtincursorinfo.cpp builtincursorinfo.h builtincursorinfo.cpp builtincursorinfo.h

View File

@@ -1,104 +0,0 @@
/****************************************************************************
**
** Copyright (C) 2018 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/
#pragma once
#include "cppeditor_global.h"
#include <utils/dropsupport.h>
#include <utils/treemodel.h>
#include <QSharedPointer>
#include <utility>
namespace CPlusPlus { class Document; }
namespace Utils {
class LineColumn;
class Link;
}
namespace CppEditor {
class CPPEDITOR_EXPORT AbstractOverviewModel : public Utils::TreeModel<>
{
Q_OBJECT
public:
enum Role {
FileNameRole = Qt::UserRole + 1,
LineNumberRole
};
virtual void rebuild(QSharedPointer<CPlusPlus::Document>) {}
virtual bool rebuild(const QString &) { return false; }
Qt::ItemFlags flags(const QModelIndex &index) const override
{
if (!index.isValid())
return Qt::NoItemFlags;
return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsDragEnabled;
}
Qt::DropActions supportedDragActions() const override
{
return Qt::MoveAction;
}
QStringList mimeTypes() const override
{
return Utils::DropSupport::mimeTypesForFilePaths();
}
QMimeData *mimeData(const QModelIndexList &indexes) const override
{
auto mimeData = new Utils::DropMimeData;
for (const QModelIndex &index : indexes) {
const QVariant fileName = data(index, FileNameRole);
if (!fileName.canConvert<QString>())
continue;
const QVariant lineNumber = data(index, LineNumberRole);
if (!lineNumber.canConvert<unsigned>())
continue;
mimeData->addFile(Utils::FilePath::fromVariant(fileName),
static_cast<int>(lineNumber.value<unsigned>()));
}
return mimeData;
}
virtual bool isGenerated(const QModelIndex &) const { return false; }
virtual Utils::Link linkFromIndex(const QModelIndex &) const = 0;
virtual Utils::LineColumn lineColumnFromIndex(const QModelIndex &) const = 0;
using Range = std::pair<Utils::LineColumn, Utils::LineColumn>;
virtual Range rangeFromIndex(const QModelIndex &) const = 0;
signals:
void needsUpdate();
};
} // namespace CppEditor

View File

@@ -113,11 +113,6 @@ TextEditor::BaseHoverHandler *BuiltinModelManagerSupport::createHoverHandler()
return new CppHoverHandler; return new CppHoverHandler;
} }
std::unique_ptr<AbstractOverviewModel> BuiltinModelManagerSupport::createOverviewModel()
{
return std::make_unique<OverviewModel>();
}
void BuiltinModelManagerSupport::followSymbol(const CursorInEditor &data, void BuiltinModelManagerSupport::followSymbol(const CursorInEditor &data,
const Utils::LinkHandler &processLinkCallback, const Utils::LinkHandler &processLinkCallback,
bool resolveTarget, bool inNextSplit) bool resolveTarget, bool inNextSplit)

View File

@@ -43,7 +43,6 @@ public:
CppCompletionAssistProvider *completionAssistProvider(); CppCompletionAssistProvider *completionAssistProvider();
TextEditor::BaseHoverHandler *createHoverHandler(); TextEditor::BaseHoverHandler *createHoverHandler();
std::unique_ptr<AbstractOverviewModel> createOverviewModel();
BaseEditorDocumentProcessor *createEditorDocumentProcessor( BaseEditorDocumentProcessor *createEditorDocumentProcessor(
TextEditor::TextDocument *baseTextDocument) final; TextEditor::TextDocument *baseTextDocument) final;

View File

@@ -29,7 +29,6 @@ QtcPlugin {
files: [ files: [
"abstracteditorsupport.cpp", "abstracteditorsupport.cpp",
"abstracteditorsupport.h", "abstracteditorsupport.h",
"abstractoverviewmodel.h",
"baseeditordocumentparser.cpp", "baseeditordocumentparser.cpp",
"baseeditordocumentparser.h", "baseeditordocumentparser.h",
"baseeditordocumentprocessor.cpp", "baseeditordocumentprocessor.cpp",

View File

@@ -57,7 +57,7 @@ class OverviewProxyModel : public QSortFilterProxyModel
Q_OBJECT Q_OBJECT
public: public:
OverviewProxyModel(CppEditor::AbstractOverviewModel &sourceModel, QObject *parent) OverviewProxyModel(CppEditor::Internal::OverviewModel &sourceModel, QObject *parent)
: QSortFilterProxyModel(parent) : QSortFilterProxyModel(parent)
, m_sourceModel(sourceModel) , m_sourceModel(sourceModel)
{ {
@@ -73,7 +73,7 @@ public:
return QSortFilterProxyModel::filterAcceptsRow(sourceRow, sourceParent); return QSortFilterProxyModel::filterAcceptsRow(sourceRow, sourceParent);
} }
private: private:
CppEditor::AbstractOverviewModel &m_sourceModel; CppEditor::Internal::OverviewModel &m_sourceModel;
}; };
QTimer *newSingleShotTimer(QObject *parent, int msInternal, const QString &objectName) QTimer *newSingleShotTimer(QObject *parent, int msInternal, const QString &objectName)
@@ -94,7 +94,7 @@ CppEditorOutline::CppEditorOutline(TextEditor::TextEditorWidget *editorWidget)
, m_editorWidget(editorWidget) , m_editorWidget(editorWidget)
, m_combo(new Utils::TreeViewComboBox) , m_combo(new Utils::TreeViewComboBox)
{ {
m_model = CppModelManager::instance()->createOverviewModel(); m_model = std::make_unique<OverviewModel>();
m_proxyModel = new OverviewProxyModel(*m_model, this); m_proxyModel = new OverviewProxyModel(*m_model, this);
m_proxyModel->setSourceModel(m_model.get()); m_proxyModel->setSourceModel(m_model.get());
@@ -130,7 +130,7 @@ CppEditorOutline::CppEditorOutline(TextEditor::TextEditorWidget *editorWidget)
m_updateTimer = newSingleShotTimer(this, UpdateOutlineIntervalInMs, m_updateTimer = newSingleShotTimer(this, UpdateOutlineIntervalInMs,
QLatin1String("CppEditorOutline::m_updateTimer")); QLatin1String("CppEditorOutline::m_updateTimer"));
connect(m_updateTimer, &QTimer::timeout, this, &CppEditorOutline::updateNow); connect(m_updateTimer, &QTimer::timeout, this, &CppEditorOutline::updateNow);
connect(m_model.get(), &AbstractOverviewModel::needsUpdate, this, connect(m_model.get(), &OverviewModel::needsUpdate, this,
&CppEditorOutline::updateNow); &CppEditorOutline::updateNow);
m_updateIndexTimer = newSingleShotTimer(this, UpdateOutlineIntervalInMs, m_updateIndexTimer = newSingleShotTimer(this, UpdateOutlineIntervalInMs,
@@ -148,7 +148,7 @@ bool CppEditorOutline::isSorted() const
return m_proxyModel->sortColumn() == 0; return m_proxyModel->sortColumn() == 0;
} }
AbstractOverviewModel *CppEditorOutline::model() const OverviewModel *CppEditorOutline::model() const
{ {
return m_model.get(); return m_model.get();
} }
@@ -189,7 +189,6 @@ void CppEditorOutline::updateNow()
return; return;
} }
if (!m_model->rebuild(filePath))
m_model->rebuild(m_document); m_model->rebuild(m_document);
m_combo->view()->expandAll(); m_combo->view()->expandAll();
@@ -244,7 +243,7 @@ void CppEditorOutline::gotoSymbolInEditor()
emit m_editorWidget->activateEditor(); emit m_editorWidget->activateEditor();
} }
static bool contains(const AbstractOverviewModel::Range &range, int line, int column) static bool contains(const OverviewModel::Range &range, int line, int column)
{ {
if (line < range.first.line || line > range.second.line) if (line < range.first.line || line > range.second.line)
return false; return false;
@@ -262,7 +261,7 @@ QModelIndex CppEditorOutline::indexForPosition(int line, int column,
const int rowCount = m_model->rowCount(rootIndex); const int rowCount = m_model->rowCount(rootIndex);
for (int row = 0; row < rowCount; ++row) { for (int row = 0; row < rowCount; ++row) {
const QModelIndex index = m_model->index(row, 0, rootIndex); const QModelIndex index = m_model->index(row, 0, rootIndex);
const AbstractOverviewModel::Range range = m_model->rangeFromIndex(index); const OverviewModel::Range range = m_model->rangeFromIndex(index);
if (range.first.line > line) if (range.first.line > line)
break; break;
// Skip ranges that do not include current line and column. // Skip ranges that do not include current line and column.

View File

@@ -25,7 +25,7 @@
#pragma once #pragma once
#include "abstractoverviewmodel.h" #include "cppoverviewmodel.h"
#include <QModelIndex> #include <QModelIndex>
#include <QObject> #include <QObject>
@@ -52,7 +52,7 @@ public:
void update(); void update();
AbstractOverviewModel *model() const; OverviewModel *model() const;
QModelIndex modelIndex(); QModelIndex modelIndex();
QWidget *widget() const; // Must be deleted by client. QWidget *widget() const; // Must be deleted by client.
@@ -77,7 +77,7 @@ private:
private: private:
QSharedPointer<CPlusPlus::Document> m_document; QSharedPointer<CPlusPlus::Document> m_document;
std::unique_ptr<AbstractOverviewModel> m_model; std::unique_ptr<OverviewModel> m_model;
TextEditor::TextEditorWidget *m_editorWidget; TextEditor::TextEditorWidget *m_editorWidget;

View File

@@ -26,7 +26,7 @@
#include "cppmodelmanager.h" #include "cppmodelmanager.h"
#include "abstracteditorsupport.h" #include "abstracteditorsupport.h"
#include "abstractoverviewmodel.h" #include "cppoverviewmodel.h"
#include "baseeditordocumentprocessor.h" #include "baseeditordocumentprocessor.h"
#include "builtinindexingsupport.h" #include "builtinindexingsupport.h"
#include "cppcodemodelinspectordumper.h" #include "cppcodemodelinspectordumper.h"
@@ -549,11 +549,6 @@ Core::ILocatorFilter *CppModelManager::currentDocumentFilter() const
return d->m_currentDocumentFilter.get(); return d->m_currentDocumentFilter.get();
} }
std::unique_ptr<AbstractOverviewModel> CppModelManager::createOverviewModel() const
{
return d->m_builtinModelManagerSupport.createOverviewModel();
}
QString CppModelManager::configurationFileName() QString CppModelManager::configurationFileName()
{ {
return Preprocessor::configurationFileName(); return Preprocessor::configurationFileName();

View File

@@ -61,7 +61,6 @@ class TextDocument;
namespace CppEditor { namespace CppEditor {
class AbstractEditorSupport; class AbstractEditorSupport;
class AbstractOverviewModel;
class BaseEditorDocumentProcessor; class BaseEditorDocumentProcessor;
class CppCompletionAssistProvider; class CppCompletionAssistProvider;
class CppEditorDocumentHandle; class CppEditorDocumentHandle;
@@ -203,8 +202,6 @@ public:
static Core::ILocatorFilter *createAuxiliaryCurrentDocumentFilter(); static Core::ILocatorFilter *createAuxiliaryCurrentDocumentFilter();
std::unique_ptr<AbstractOverviewModel> createOverviewModel() const;
CppIndexingSupport *indexingSupport(); CppIndexingSupport *indexingSupport();
QStringList projectFiles(); QStringList projectFiles();

View File

@@ -42,7 +42,7 @@ class BaseHoverHandler;
namespace CppEditor { namespace CppEditor {
class AbstractOverviewModel; class OverviewModel;
class BaseEditorDocumentProcessor; class BaseEditorDocumentProcessor;
class CppCompletionAssistProvider; class CppCompletionAssistProvider;
class ProjectPart; class ProjectPart;

View File

@@ -68,7 +68,7 @@ void CppOutlineTreeView::contextMenuEvent(QContextMenuEvent *event)
event->accept(); event->accept();
} }
CppOutlineFilterModel::CppOutlineFilterModel(AbstractOverviewModel &sourceModel, CppOutlineFilterModel::CppOutlineFilterModel(OverviewModel &sourceModel,
QObject *parent) QObject *parent)
: QSortFilterProxyModel(parent) : QSortFilterProxyModel(parent)
, m_sourceModel(sourceModel) , m_sourceModel(sourceModel)
@@ -102,7 +102,7 @@ CppOutlineWidget::CppOutlineWidget(CppEditorWidget *editor) :
m_blockCursorSync(false), m_blockCursorSync(false),
m_sorted(false) m_sorted(false)
{ {
AbstractOverviewModel *model = m_editor->outline()->model(); OverviewModel *model = m_editor->outline()->model();
m_proxyModel = new CppOutlineFilterModel(*model, this); m_proxyModel = new CppOutlineFilterModel(*model, this);
m_proxyModel->setSourceModel(model); m_proxyModel->setSourceModel(model);
@@ -179,7 +179,7 @@ void CppOutlineWidget::updateSelectionInTree(const QModelIndex &index)
void CppOutlineWidget::updateTextCursor(const QModelIndex &proxyIndex) void CppOutlineWidget::updateTextCursor(const QModelIndex &proxyIndex)
{ {
QModelIndex index = m_proxyModel->mapToSource(proxyIndex); QModelIndex index = m_proxyModel->mapToSource(proxyIndex);
AbstractOverviewModel *model = m_editor->outline()->model(); OverviewModel *model = m_editor->outline()->model();
Utils::LineColumn lineColumn = model->lineColumnFromIndex(index); Utils::LineColumn lineColumn = model->lineColumnFromIndex(index);
if (!lineColumn.isValid()) if (!lineColumn.isValid())
return; return;

View File

@@ -25,8 +25,8 @@
#pragma once #pragma once
#include "abstractoverviewmodel.h"
#include "cppeditorwidget.h" #include "cppeditorwidget.h"
#include "cppoverviewmodel.h"
#include <texteditor/ioutlinewidget.h> #include <texteditor/ioutlinewidget.h>
@@ -50,13 +50,13 @@ class CppOutlineFilterModel : public QSortFilterProxyModel
{ {
Q_OBJECT Q_OBJECT
public: public:
CppOutlineFilterModel(AbstractOverviewModel &sourceModel, QObject *parent); CppOutlineFilterModel(OverviewModel &sourceModel, QObject *parent);
// QSortFilterProxyModel // QSortFilterProxyModel
bool filterAcceptsRow(int sourceRow, bool filterAcceptsRow(int sourceRow,
const QModelIndex &sourceParent) const override; const QModelIndex &sourceParent) const override;
Qt::DropActions supportedDragActions() const override; Qt::DropActions supportedDragActions() const override;
private: private:
AbstractOverviewModel &m_sourceModel; OverviewModel &m_sourceModel;
}; };
class CppOutlineWidget : public TextEditor::IOutlineWidget class CppOutlineWidget : public TextEditor::IOutlineWidget

View File

@@ -126,10 +126,10 @@ public:
case Qt::DecorationRole: case Qt::DecorationRole:
return Icons::iconForSymbol(symbol); return Icons::iconForSymbol(symbol);
case AbstractOverviewModel::FileNameRole: case OverviewModel::FileNameRole:
return QString::fromUtf8(symbol->fileName(), symbol->fileNameLength()); return QString::fromUtf8(symbol->fileName(), symbol->fileNameLength());
case AbstractOverviewModel::LineNumberRole: case OverviewModel::LineNumberRole:
return symbol->line(); return symbol->line();
default: default:
@@ -168,6 +168,40 @@ Symbol *OverviewModel::symbolFromIndex(const QModelIndex &index) const
return item ? item->symbol : nullptr; return item ? item->symbol : nullptr;
} }
Qt::ItemFlags OverviewModel::flags(const QModelIndex &index) const
{
if (!index.isValid())
return Qt::NoItemFlags;
return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsDragEnabled;
}
Qt::DropActions OverviewModel::supportedDragActions() const
{
return Qt::MoveAction;
}
QStringList OverviewModel::mimeTypes() const
{
return Utils::DropSupport::mimeTypesForFilePaths();
}
QMimeData *OverviewModel::mimeData(const QModelIndexList &indexes) const
{
auto mimeData = new Utils::DropMimeData;
for (const QModelIndex &index : indexes) {
const QVariant fileName = data(index, FileNameRole);
if (!fileName.canConvert<QString>())
continue;
const QVariant lineNumber = data(index, LineNumberRole);
if (!lineNumber.canConvert<unsigned>())
continue;
mimeData->addFile(Utils::FilePath::fromVariant(fileName),
static_cast<int>(lineNumber.value<unsigned>()));
}
return mimeData;
}
void OverviewModel::rebuild(Document::Ptr doc) void OverviewModel::rebuild(Document::Ptr doc)
{ {
beginResetModel(); beginResetModel();

View File

@@ -25,25 +25,49 @@
#pragma once #pragma once
#include "abstractoverviewmodel.h" #include <utils/dropsupport.h>
#include <utils/treemodel.h>
#include <cplusplus/CppDocument.h> #include <cplusplus/CppDocument.h>
#include <cplusplus/Overview.h> #include <cplusplus/Overview.h>
#include <QSharedPointer>
#include <utility>
namespace Utils {
class LineColumn;
class Link;
} // namespace Utils
namespace CppEditor::Internal { namespace CppEditor::Internal {
class SymbolItem; class SymbolItem;
class OverviewModel : public AbstractOverviewModel class OverviewModel : public Utils::TreeModel<>
{ {
Q_OBJECT Q_OBJECT
public: public:
void rebuild(CPlusPlus::Document::Ptr doc) override; enum Role {
FileNameRole = Qt::UserRole + 1,
LineNumberRole
};
bool isGenerated(const QModelIndex &sourceIndex) const override; Qt::ItemFlags flags(const QModelIndex &index) const override;
Utils::Link linkFromIndex(const QModelIndex &sourceIndex) const override; Qt::DropActions supportedDragActions() const override;
Utils::LineColumn lineColumnFromIndex(const QModelIndex &sourceIndex) const override; QStringList mimeTypes() const override;
Range rangeFromIndex(const QModelIndex &sourceIndex) const override; QMimeData *mimeData(const QModelIndexList &indexes) const override;
void rebuild(CPlusPlus::Document::Ptr doc);
bool isGenerated(const QModelIndex &sourceIndex) const;
Utils::Link linkFromIndex(const QModelIndex &sourceIndex) const;
Utils::LineColumn lineColumnFromIndex(const QModelIndex &sourceIndex) const;
using Range = std::pair<Utils::LineColumn, Utils::LineColumn>;
Range rangeFromIndex(const QModelIndex &sourceIndex) const;
signals:
void needsUpdate();
private: private:
CPlusPlus::Symbol *symbolFromIndex(const QModelIndex &index) const; CPlusPlus::Symbol *symbolFromIndex(const QModelIndex &index) const;