Commit abdbae33 authored by Przemyslaw Gorszkowski's avatar Przemyslaw Gorszkowski Committed by Nikolai Kosjar

CppEditor: "Include Hierarchy": using snapshot from snapshotUpdater

For "includes" we should use snapshot from "SnapshotUpdater"(project
specific snapshot, not global snapshot) instead of "global" snapshot.
The snapshot of the editor's SnapshotUpdater contains the documents
parsed with the appropriate include paths/defines etc. and should
therefore be used for the include hierarchy.

Change-Id: I6bbaf5040536b771d32697aad1db364758ff8382
Reviewed-by: default avatarNikolai Kosjar <nikolai.kosjar@digia.com>
parent f744372e
......@@ -36,6 +36,7 @@
#include "cppincludehierarchymodel.h"
#include "cppincludehierarchytreeview.h"
#include <coreplugin/editormanager/editormanager.h>
#include <cplusplus/CppDocument.h>
#include <utils/annotateditemdelegate.h>
......@@ -89,7 +90,8 @@ CppIncludeHierarchyWidget::CppIncludeHierarchyWidget() :
m_treeView(0),
m_model(0),
m_delegate(0),
m_includeHierarchyInfoLabel(0)
m_includeHierarchyInfoLabel(0),
m_editor(0)
{
m_inspectedFile = new CppIncludeLabel(this);
m_inspectedFile->setMargin(5);
......@@ -118,6 +120,9 @@ CppIncludeHierarchyWidget::CppIncludeHierarchyWidget() :
setLayout(layout);
connect(CppEditorPlugin::instance(), SIGNAL(includeHierarchyRequested()), SLOT(perform()));
connect(Core::EditorManager::instance(), SIGNAL(editorsClosed(QList<Core::IEditor *>)),
this, SLOT(editorsClosed(QList<Core::IEditor *>)));
}
CppIncludeHierarchyWidget::~CppIncludeHierarchyWidget()
......@@ -128,15 +133,16 @@ void CppIncludeHierarchyWidget::perform()
{
showNoIncludeHierarchyLabel();
CPPEditor *editor = qobject_cast<CPPEditor *>(Core::EditorManager::currentEditor());
if (!editor)
m_editor = qobject_cast<CPPEditor *>(Core::EditorManager::currentEditor());
if (!m_editor)
return;
CPPEditorWidget *widget = qobject_cast<CPPEditorWidget *>(editor->widget());
CPPEditorWidget *widget = qobject_cast<CPPEditorWidget *>(m_editor->widget());
if (!widget)
return;
m_model->clear();
m_model->buildHierarchy(widget->editorDocument()->filePath());
m_model->buildHierarchy(m_editor, widget->editorDocument()->filePath());
if (m_model->isEmpty())
return;
......@@ -162,6 +168,14 @@ void CppIncludeHierarchyWidget::onItemClicked(const QModelIndex &index)
Constants::CPPEDITOR_ID);
}
void CppIncludeHierarchyWidget::editorsClosed(QList<Core::IEditor *> editors)
{
foreach (Core::IEditor *editor, editors) {
if (m_editor == editor)
perform();
}
}
void CppIncludeHierarchyWidget::showNoIncludeHierarchyLabel()
{
m_inspectedFile->hide();
......
......@@ -55,6 +55,7 @@ class FileName;
namespace CppEditor {
namespace Internal {
class CPPEditor;
class CPPEditorWidget;
class CppInclude;
class CppIncludeLabel;
......@@ -73,6 +74,7 @@ public slots:
private slots:
void onItemClicked(const QModelIndex &index);
void editorsClosed(QList<Core::IEditor *> editors);
private:
void showNoIncludeHierarchyLabel();
......@@ -84,6 +86,7 @@ private:
Utils::AnnotatedItemDelegate *m_delegate;
CppIncludeLabel *m_inspectedFile;
QLabel *m_includeHierarchyInfoLabel;
CPPEditor *m_editor;
};
// @todo: Pretty much the same design as the OutlineWidgetStack. Maybe we can generalize the
......
......@@ -30,6 +30,8 @@
#include "cppeditorplugin.h"
#include "cppincludehierarchymodel.h"
#include <coreplugin/editormanager/editormanager.h>
#include <cppeditor/cppeditor.h>
#include <cpptools/cppmodelmanagerinterface.h>
#include <utils/fileutils.h>
......@@ -47,6 +49,7 @@ class TestCase
public:
TestCase(const QList<QByteArray> &sourceList)
: m_cmm(CppModelManagerInterface::instance())
, m_editor(0)
{
QStringList filePaths;
const int sourceListSize = sourceList.size();
......@@ -82,22 +85,30 @@ public:
~TestCase()
{
// Close editor
if (m_editor)
Core::EditorManager::closeEditor(m_editor, false);
m_cmm->GC();
QVERIFY(m_cmm->snapshot().isEmpty());
}
void run(int includesCount, int includedByCount) const
void run(int includesCount, int includedByCount)
{
const QString fileName = QDir::tempPath() + QLatin1String("/file1.h");
m_editor = qobject_cast<CPPEditor *>(Core::EditorManager::openEditor(fileName));
QVERIFY(m_editor);
CppIncludeHierarchyModel model(0);
model.buildHierarchy(fileName);
model.buildHierarchy(m_editor, fileName);
QCOMPARE(model.rowCount(model.index(0, 0)), includesCount);
QCOMPARE(model.rowCount(model.index(1, 0)), includedByCount);
}
private:
CppModelManagerInterface *m_cmm;
CPPEditor *m_editor;
};
}
......
......@@ -35,10 +35,12 @@
#include <cplusplus/CppDocument.h>
#include <cppeditor/cppeditor.h>
#include <cpptools/cppmodelmanagerinterface.h>
#include <cpptools/cpptoolseditorsupport.h>
#include <QSet>
using namespace CPlusPlus;
using namespace CppTools;
namespace CppEditor {
namespace Internal {
......@@ -48,7 +50,7 @@ CppIncludeHierarchyModel::CppIncludeHierarchyModel(QObject *parent)
, m_rootItem(new CppIncludeHierarchyItem(QString()))
, m_includesItem(new CppIncludeHierarchyItem(tr("Includes"), m_rootItem))
, m_includedByItem(new CppIncludeHierarchyItem(tr("Included by"), m_rootItem))
, m_editor(0)
{
m_rootItem->appendChild(m_includesItem);
m_rootItem->appendChild(m_includedByItem);
......@@ -199,6 +201,15 @@ void CppIncludeHierarchyModel::clear()
endResetModel();
}
void CppIncludeHierarchyModel::buildHierarchy(CPPEditor *editor, const QString &filePath)
{
m_editor = editor;
beginResetModel();
buildHierarchyIncludes(filePath);
buildHierarchyIncludedBy(filePath);
endResetModel();
}
bool CppIncludeHierarchyModel::hasChildren(const QModelIndex &parent) const
{
if (!parent.isValid())
......@@ -210,14 +221,6 @@ bool CppIncludeHierarchyModel::hasChildren(const QModelIndex &parent) const
return parentItem->hasChildren();
}
void CppIncludeHierarchyModel::buildHierarchy(const QString &filePath)
{
beginResetModel();
buildHierarchyIncludes(filePath);
buildHierarchyIncludedBy(filePath);
endResetModel();
}
bool CppIncludeHierarchyModel::isEmpty() const
{
return !m_includesItem->hasChildren() && !m_includedByItem->hasChildren();
......@@ -233,7 +236,11 @@ void CppIncludeHierarchyModel::buildHierarchyIncludes_helper(const QString &file
CppIncludeHierarchyItem *parent,
QSet<QString> *cyclic, bool recursive)
{
const Snapshot &snapshot = CppTools::CppModelManagerInterface::instance()->snapshot();
if (!m_editor)
return;
CppModelManagerInterface *cppMM = CppModelManagerInterface::instance();
const Snapshot &snapshot = cppMM->cppEditorSupport(m_editor)->snapshotUpdater()->snapshot();
Document::Ptr doc = snapshot.document(filePath);
if (!doc)
return;
......
......@@ -41,9 +41,14 @@ enum ItemRole {
} // Anonymous
namespace Core {
class IEditor;
}
namespace CppEditor {
namespace Internal {
class CPPEditor;
class CppIncludeHierarchyItem;
class CppIncludeHierarchyModel : public QAbstractItemModel
......@@ -63,7 +68,7 @@ public:
bool hasChildren(const QModelIndex &parent) const;
void clear();
void buildHierarchy(const QString &filePath);
void buildHierarchy(CPPEditor *editor, const QString &filePath);
bool isEmpty() const;
private:
......@@ -77,6 +82,7 @@ private:
CppIncludeHierarchyItem *m_rootItem;
CppIncludeHierarchyItem *m_includesItem;
CppIncludeHierarchyItem *m_includedByItem;
CPPEditor *m_editor;
};
} // namespace Internal
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment