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

CppEditor: introduce include hierarchy

Based on type hierarchy.

Added to context menu ('Open Include Hierarchy')
Shortcut(Ctrl+Shift+I)

'Include Hierarchy' contains:
FileName for which 'Include Hierarchy' is done
Includes(which files are included by current file - tree hierarchy)
Included by(which files include current file - tree hierarchy)

It is possible to open/goto each file by clicking on specific item.
Additionally for 'Included by' files it goes to the line where is include
directive for current file.

Change-Id: I549b1ed64e4c9f6955f60d00efb12950a3259f81
Reviewed-by: default avatarNikolai Kosjar <nikolai.kosjar@digia.com>
parent 8c79d5bc
......@@ -135,6 +135,9 @@
\li \gui {Type Hierarchy} shows the base classes of a class.
\li \gui {Include Hierarchy} shows which files are included in the current file
and which files include the current file.
\endlist
You can change the view of the sidebar in the following ways:
......@@ -234,6 +237,12 @@
To view the base classes of a class, right-click the class and select
\gui {Open Type Hierarchy} or press \key {Ctrl+Shift+T}.
\section2 Viewing Include Hierarchy
To view which files are included in the current file and which files include
the current file, right-click in the editor and select \gui {Open Include Hierarchy}
or press \key {Ctrl+Shift+I}.
\section1 Viewing Output
The task pane in \QC can display one of the following panes:
......
......@@ -19,7 +19,11 @@ HEADERS += cppeditorplugin.h \
cppquickfixes.h \
cppquickfix.h \
cppsnippetprovider.h \
cpptypehierarchy.h
cpptypehierarchy.h \
cppincludehierarchy.h \
cppincludehierarchymodel.h \
cppincludehierarchyitem.h \
cppincludehierarchytreeview.h
SOURCES += cppeditorplugin.cpp \
cppautocompleter.cpp \
......@@ -37,7 +41,11 @@ SOURCES += cppeditorplugin.cpp \
cppquickfix.cpp \
cppquickfixes.cpp \
cppsnippetprovider.cpp \
cpptypehierarchy.cpp
cpptypehierarchy.cpp \
cppincludehierarchy.cpp \
cppincludehierarchymodel.cpp \
cppincludehierarchyitem.cpp \
cppincludehierarchytreeview.cpp
RESOURCES += cppeditor.qrc
......
......@@ -40,6 +40,14 @@ QtcPlugin {
"cpphighlighter.h",
"cpphoverhandler.cpp",
"cpphoverhandler.h",
"cppincludehierarchy.cpp",
"cppincludehierarchy.h",
"cppincludehierarchyitem.cpp",
"cppincludehierarchyitem.h",
"cppincludehierarchymodel.cpp",
"cppincludehierarchymodel.h",
"cppincludehierarchytreeview.cpp",
"cppincludehierarchytreeview.h",
"cppoutline.cpp",
"cppoutline.h",
"cppquickfixassistant.cpp",
......
......@@ -48,6 +48,10 @@ const int TYPE_HIERARCHY_PRIORITY = 700;
const char TYPE_HIERARCHY_ID[] = "CppEditor.TypeHierarchy";
const char OPEN_TYPE_HIERARCHY[] = "CppEditor.OpenTypeHierarchy";
const int INCLUDE_HIERARCHY_PRIORITY = 800;
const char INCLUDE_HIERARCHY_ID[] = "CppEditor.IncludeHierarchy";
const char OPEN_INCLUDE_HIERARCHY[] = "CppEditor.OpenIncludeHierarchy";
const char C_SOURCE_MIMETYPE[] = "text/x-csrc";
const char C_HEADER_MIMETYPE[] = "text/x-chdr";
const char CPP_SOURCE_MIMETYPE[] = "text/x-c++src";
......
......@@ -36,6 +36,7 @@
#include "cpphoverhandler.h"
#include "cppoutline.h"
#include "cpptypehierarchy.h"
#include "cppincludehierarchy.h"
#include "cppsnippetprovider.h"
#include "cppquickfixassistant.h"
#include "cppquickfixes.h"
......@@ -103,6 +104,7 @@ CppEditorPlugin::CppEditorPlugin() :
m_findUsagesAction(0),
m_updateCodeModelAction(0),
m_openTypeHierarchyAction(0),
m_openIncludeHierarchyAction(0),
m_quickFixProvider(0)
{
m_instance = this;
......@@ -156,6 +158,7 @@ bool CppEditorPlugin::initialize(const QStringList & /*arguments*/, QString *err
addAutoReleasedObject(new CppHoverHandler);
addAutoReleasedObject(new CppOutlineWidgetFactory);
addAutoReleasedObject(new CppTypeHierarchyFactory);
addAutoReleasedObject(new CppIncludeHierarchyFactory);
addAutoReleasedObject(new CppSnippetProvider);
addAutoReleasedObject(new CppHighlighterFactory);
......@@ -244,6 +247,13 @@ bool CppEditorPlugin::initialize(const QStringList & /*arguments*/, QString *err
contextMenu->addAction(cmd);
cppToolsMenu->addAction(cmd);
m_openIncludeHierarchyAction = new QAction(tr("Open Include Hierarchy"), this);
cmd = Core::ActionManager::registerAction(m_openIncludeHierarchyAction, Constants::OPEN_INCLUDE_HIERARCHY, context);
cmd->setDefaultKeySequence(QKeySequence(Core::UseMacShortcuts ? tr("Meta+Shift+I") : tr("Ctrl+Shift+I")));
connect(m_openIncludeHierarchyAction, SIGNAL(triggered()), this, SLOT(openIncludeHierarchy()));
contextMenu->addAction(cmd);
cppToolsMenu->addAction(cmd);
// Refactoring sub-menu
Context globalContext(Core::Constants::C_GLOBAL);
Command *sep = contextMenu->addSeparator(globalContext);
......@@ -351,6 +361,7 @@ void CppEditorPlugin::onTaskStarted(Core::Id type)
m_findUsagesAction->setEnabled(false);
m_updateCodeModelAction->setEnabled(false);
m_openTypeHierarchyAction->setEnabled(false);
m_openIncludeHierarchyAction->setEnabled(false);
}
}
......@@ -361,6 +372,7 @@ void CppEditorPlugin::onAllTasksFinished(Core::Id type)
m_findUsagesAction->setEnabled(true);
m_updateCodeModelAction->setEnabled(true);
m_openTypeHierarchyAction->setEnabled(true);
m_openIncludeHierarchyAction->setEnabled(true);
}
}
......@@ -383,4 +395,15 @@ void CppEditorPlugin::openTypeHierarchy()
}
}
void CppEditorPlugin::openIncludeHierarchy()
{
CPPEditorWidget *editor
= qobject_cast<CPPEditorWidget*>(Core::EditorManager::currentEditor()->widget());
if (editor) {
Core::NavigationWidget *navigation = Core::NavigationWidget::instance();
navigation->activateSubWidget(Core::Id(Constants::INCLUDE_HIERARCHY_ID));
emit includeHierarchyRequested();
}
}
Q_EXPORT_PLUGIN(CppEditorPlugin)
......@@ -74,10 +74,12 @@ public:
signals:
void outlineSortingChanged(bool sort);
void typeHierarchyRequested();
void includeHierarchyRequested();
public slots:
void openDeclarationDefinitionInNextSplit();
void openTypeHierarchy();
void openIncludeHierarchy();
void findUsages();
void renameSymbolUnderCursor();
void switchDeclarationDefinition();
......@@ -291,6 +293,7 @@ private:
QAction *m_findUsagesAction;
QAction *m_updateCodeModelAction;
QAction *m_openTypeHierarchyAction;
QAction *m_openIncludeHierarchyAction;
CppQuickFixAssistProvider *m_quickFixProvider;
......
/****************************************************************************
**
** Copyright (C) 2013 Przemyslaw Gorszkowski <pgorszkowski@gmail.com>
** Contact: http://www.qt-project.org/legal
**
** 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 Digia. For licensing terms and
** conditions see http://qt.digia.com/licensing. For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/
#include "cppincludehierarchy.h"
#include "cppeditor.h"
#include "cppeditorconstants.h"
#include "cppeditorplugin.h"
#include "cppelementevaluator.h"
#include "cppincludehierarchymodel.h"
#include "cppincludehierarchytreeview.h"
#include <cplusplus/CppDocument.h>
#include <utils/annotateditemdelegate.h>
#include <utils/fileutils.h>
#include <QDir>
#include <QLabel>
#include <QLatin1String>
#include <QModelIndex>
#include <QStandardItem>
#include <QVBoxLayout>
using namespace CppEditor;
using namespace Internal;
using namespace Utils;
namespace CppEditor {
namespace Internal {
class CppIncludeLabel : public QLabel
{
public:
CppIncludeLabel(QWidget *parent)
: QLabel(parent)
{}
void setup(const QString &fileName, const QString &filePath)
{
setText(fileName);
m_link = CPPEditorWidget::Link(filePath);
}
private:
void mousePressEvent(QMouseEvent *)
{
if (!m_link.hasValidTarget())
return;
Core::EditorManager::openEditorAt(m_link.targetFileName,
m_link.targetLine,
m_link.targetColumn,
Constants::CPPEDITOR_ID);
}
CPPEditorWidget::Link m_link;
};
// CppIncludeHierarchyWidget
CppIncludeHierarchyWidget::CppIncludeHierarchyWidget() :
QWidget(0),
m_treeView(0),
m_model(0),
m_delegate(0),
m_includeHierarchyInfoLabel(0)
{
m_inspectedFile = new CppIncludeLabel(this);
m_inspectedFile->setMargin(5);
m_model = new CppIncludeHierarchyModel(this);
m_treeView = new CppIncludeHierarchyTreeView(this);
m_delegate = new AnnotatedItemDelegate(this);
m_delegate->setDelimiter(QLatin1String(" "));
m_delegate->setAnnotationRole(AnnotationRole);
m_treeView->setModel(m_model);
m_treeView->setEditTriggers(QAbstractItemView::NoEditTriggers);
m_treeView->setItemDelegate(m_delegate);
connect(m_treeView, SIGNAL(clicked(QModelIndex)), this, SLOT(onItemClicked(QModelIndex)));
m_includeHierarchyInfoLabel = new QLabel(tr("No include hierarchy available"), this);
m_includeHierarchyInfoLabel->setAlignment(Qt::AlignCenter);
m_includeHierarchyInfoLabel->setAutoFillBackground(true);
m_includeHierarchyInfoLabel->setBackgroundRole(QPalette::Base);
QVBoxLayout *layout = new QVBoxLayout;
layout->setMargin(0);
layout->setSpacing(0);
layout->addWidget(m_inspectedFile);
layout->addWidget(m_treeView);
layout->addWidget(m_includeHierarchyInfoLabel);
setLayout(layout);
connect(CppEditorPlugin::instance(), SIGNAL(includeHierarchyRequested()), SLOT(perform()));
}
CppIncludeHierarchyWidget::~CppIncludeHierarchyWidget()
{
}
void CppIncludeHierarchyWidget::perform()
{
showNoIncludeHierarchyLabel();
CPPEditor *editor = qobject_cast<CPPEditor *>(Core::EditorManager::currentEditor());
if (!editor)
return;
CPPEditorWidget *widget = qobject_cast<CPPEditorWidget *>(editor->widget());
if (!widget)
return;
m_model->clear();
m_model->buildHierarchy(widget->editorDocument()->filePath());
if (m_model->isEmpty())
return;
m_inspectedFile->setup(widget->editorDocument()->displayName(),
widget->editorDocument()->filePath());
//expand "Includes"
m_treeView->expand(m_model->index(0, 0));
//expand "Included by"
m_treeView->expand(m_model->index(1, 0));
showIncludeHierarchy();
}
void CppIncludeHierarchyWidget::onItemClicked(const QModelIndex &index)
{
const TextEditor::BaseTextEditorWidget::Link link
= index.data(LinkRole).value<TextEditor::BaseTextEditorWidget::Link>();
if (link.hasValidTarget())
Core::EditorManager::openEditorAt(link.targetFileName,
link.targetLine,
link.targetColumn,
Constants::CPPEDITOR_ID);
}
void CppIncludeHierarchyWidget::showNoIncludeHierarchyLabel()
{
m_inspectedFile->hide();
m_treeView->hide();
m_includeHierarchyInfoLabel->show();
}
void CppIncludeHierarchyWidget::showIncludeHierarchy()
{
m_inspectedFile->show();
m_treeView->show();
m_includeHierarchyInfoLabel->hide();
}
// CppIncludeHierarchyStackedWidget
CppIncludeHierarchyStackedWidget::CppIncludeHierarchyStackedWidget(QWidget *parent) :
QStackedWidget(parent),
m_typeHiearchyWidgetInstance(new CppIncludeHierarchyWidget)
{
addWidget(m_typeHiearchyWidgetInstance);
}
CppIncludeHierarchyStackedWidget::~CppIncludeHierarchyStackedWidget()
{
delete m_typeHiearchyWidgetInstance;
}
// CppIncludeHierarchyFactory
CppIncludeHierarchyFactory::CppIncludeHierarchyFactory()
{
}
CppIncludeHierarchyFactory::~CppIncludeHierarchyFactory()
{
}
QString CppIncludeHierarchyFactory::displayName() const
{
return tr("Include Hierarchy");
}
int CppIncludeHierarchyFactory::priority() const
{
return Constants::INCLUDE_HIERARCHY_PRIORITY;
}
Core::Id CppIncludeHierarchyFactory::id() const
{
return Core::Id(Constants::INCLUDE_HIERARCHY_ID);
}
QKeySequence CppIncludeHierarchyFactory::activationSequence() const
{
return QKeySequence();
}
Core::NavigationView CppIncludeHierarchyFactory::createWidget()
{
CppIncludeHierarchyStackedWidget *w = new CppIncludeHierarchyStackedWidget;
static_cast<CppIncludeHierarchyWidget *>(w->currentWidget())->perform();
Core::NavigationView navigationView;
navigationView.widget = w;
return navigationView;
}
} // namespace Internal
} // namespace CppEditor
/****************************************************************************
**
** Copyright (C) 2013 Przemyslaw Gorszkowski <pgorszkowski@gmail.com>
** Contact: http://www.qt-project.org/legal
**
** 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 Digia. For licensing terms and
** conditions see http://qt.digia.com/licensing. For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/
#ifndef CPPINCLUDEHIERARCHY_H
#define CPPINCLUDEHIERARCHY_H
#include <coreplugin/inavigationwidgetfactory.h>
#include <QString>
#include <QStackedWidget>
#include <QWidget>
QT_BEGIN_NAMESPACE
class QStandardItemModel;
class QStandardItem;
class QModelIndex;
class QLabel;
QT_END_NAMESPACE
namespace Core {
class IEditor;
}
namespace Utils {
class AnnotatedItemDelegate;
class FileName;
}
namespace CppEditor {
namespace Internal {
class CPPEditorWidget;
class CppInclude;
class CppIncludeLabel;
class CppIncludeHierarchyModel;
class CppIncludeHierarchyTreeView;
class CppIncludeHierarchyWidget : public QWidget
{
Q_OBJECT
public:
CppIncludeHierarchyWidget();
virtual ~CppIncludeHierarchyWidget();
public slots:
void perform();
private slots:
void onItemClicked(const QModelIndex &index);
private:
void showNoIncludeHierarchyLabel();
void showIncludeHierarchy();
CPPEditorWidget *m_cppEditor;
CppIncludeHierarchyTreeView *m_treeView;
CppIncludeHierarchyModel *m_model;
Utils::AnnotatedItemDelegate *m_delegate;
CppIncludeLabel *m_inspectedFile;
QLabel *m_includeHierarchyInfoLabel;
};
// @todo: Pretty much the same design as the OutlineWidgetStack. Maybe we can generalize the
// outline factory so that it works for different widgets that support the same editor.
class CppIncludeHierarchyStackedWidget : public QStackedWidget
{
Q_OBJECT
public:
CppIncludeHierarchyStackedWidget(QWidget *parent = 0);
virtual ~CppIncludeHierarchyStackedWidget();
private:
CppIncludeHierarchyWidget *m_typeHiearchyWidgetInstance;
};
class CppIncludeHierarchyFactory : public Core::INavigationWidgetFactory
{
Q_OBJECT
public:
CppIncludeHierarchyFactory();
virtual ~CppIncludeHierarchyFactory();
QString displayName() const;
int priority() const;
Core::Id id() const;
QKeySequence activationSequence() const;
Core::NavigationView createWidget();
};
} // namespace Internal
} // namespace CppEditor
#endif // CPPINCLUDEHIERARCHY_H
/****************************************************************************
**
** Copyright (C) 2013 Przemyslaw Gorszkowski <pgorszkowski@gmail.com>
** Contact: http://www.qt-project.org/legal
**
** 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 Digia. For licensing terms and
** conditions see http://qt.digia.com/licensing. For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/
#include "cppincludehierarchyitem.h"
namespace CppEditor {
namespace Internal {
CppIncludeHierarchyItem::CppIncludeHierarchyItem(const QString &filePath,
CppIncludeHierarchyItem *parent,
bool isCyclic)
: m_fileName(filePath.mid(filePath.lastIndexOf(QLatin1Char('/')) + 1))
, m_filePath(filePath)
, m_parentItem(parent)
, m_isCyclic(isCyclic)
, m_hasChildren(false)
, m_line(0)
{
}
CppIncludeHierarchyItem::~CppIncludeHierarchyItem()
{
removeChildren();
}
const QString &CppIncludeHierarchyItem::fileName() const
{
return m_fileName;
}
const QString &CppIncludeHierarchyItem::filePath() const
{
return m_filePath;
}
CppIncludeHierarchyItem *CppIncludeHierarchyItem::parent() const
{
return m_parentItem;
}
bool CppIncludeHierarchyItem::isCyclic() const
{
return m_isCyclic;
}
void CppIncludeHierarchyItem::appendChild(CppIncludeHierarchyItem *childItem)
{
m_childItems.append(childItem);
}
CppIncludeHierarchyItem *CppIncludeHierarchyItem::child(int row)
{
return m_childItems.at(row);
}
int CppIncludeHierarchyItem::row() const
{
if (m_parentItem)
return m_parentItem->m_childItems.indexOf(const_cast<CppIncludeHierarchyItem*>(this));
return 0;
}
int CppIncludeHierarchyItem::childCount() const
{
return m_childItems.size();
}
void CppIncludeHierarchyItem::removeChildren()
{
qDeleteAll(m_childItems);
m_childItems.clear();
}
bool CppIncludeHierarchyItem::needChildrenPopulate() const
{
return m_hasChildren && m_childItems.isEmpty();
}
bool CppIncludeHierarchyItem::hasChildren() const
{
return m_hasChildren;
}
void CppIncludeHierarchyItem::setHasChildren(bool hasChildren)
{
m_hasChildren = hasChildren;
}
int CppIncludeHierarchyItem::line() const
{
return m_line;
}
void CppIncludeHierarchyItem::setLine(int line)
{
m_line = line;
}
} // namespace Internal
} // namespace CppEditor
/****************************************************************************
**
** Copyright (C) 2013 Przemyslaw Gorszkowski <pgorszkowski@gmail.com>
** Contact: http://www.qt-project.org/legal
**
** 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 Digia. For licensing terms and
** conditions see http://qt.digia.com/licensing. For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/
#ifndef CPPINCLUDEHIERARCHYITEM_H
#define CPPINCLUDEHIERARCHYITEM_H
#include <QList>
#include <QString>