1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
|
/*
* This file is part of KDevelop
*
* Copyright 2006 Adam Treat <treat@kde.org>
* Copyright 2006-2008 Hamish Rodda <rodda@kde.org>
* Copyright 2009 Lior Mualem <lior.m.kde@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Library General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program; if not, write to the
* Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "classbrowserplugin.h"
#include <QAction>
#include <KLocalizedString>
#include <KPluginFactory>
#include "interfaces/icore.h"
#include "interfaces/iuicontroller.h"
#include "interfaces/idocumentcontroller.h"
#include "interfaces/contextmenuextension.h"
#include "language/interfaces/codecontext.h"
#include "language/duchain/duchainbase.h"
#include "language/duchain/duchain.h"
#include "language/duchain/duchainlock.h"
#include "language/duchain/declaration.h"
#include "debug.h"
#include "classtree.h"
#include "classwidget.h"
#include <language/duchain/persistentsymboltable.h>
#include <language/duchain/functiondeclaration.h>
#include <language/duchain/classfunctiondeclaration.h>
#include <language/duchain/functiondefinition.h>
#include <interfaces/iprojectcontroller.h>
K_PLUGIN_FACTORY_WITH_JSON(KDevClassBrowserFactory, "kdevclassbrowser.json", registerPlugin<ClassBrowserPlugin>(); )
using namespace KDevelop;
class ClassBrowserFactory
: public KDevelop::IToolViewFactory
{
public:
explicit ClassBrowserFactory(ClassBrowserPlugin* plugin) : m_plugin(plugin) {}
QWidget* create(QWidget* parent = nullptr) override
{
return new ClassWidget(parent, m_plugin);
}
Qt::DockWidgetArea defaultPosition() const override
{
return Qt::LeftDockWidgetArea;
}
QString id() const override
{
return QStringLiteral("org.kdevelop.ClassBrowserView");
}
private:
ClassBrowserPlugin* m_plugin;
};
ClassBrowserPlugin::ClassBrowserPlugin(QObject* parent, const QVariantList&)
: KDevelop::IPlugin(QStringLiteral("kdevclassbrowser"), parent)
, m_factory(new ClassBrowserFactory(this))
, m_activeClassTree(nullptr)
{
core()->uiController()->addToolView(i18nc("@title:window", "Classes"), m_factory);
setXMLFile(QStringLiteral("kdevclassbrowser.rc"));
m_findInBrowser = new QAction(i18nc("@action", "Find in &Class Browser"), this);
connect(m_findInBrowser, &QAction::triggered, this, &ClassBrowserPlugin::findInClassBrowser);
}
ClassBrowserPlugin::~ClassBrowserPlugin()
{
}
void ClassBrowserPlugin::unload()
{
core()->uiController()->removeToolView(m_factory);
}
KDevelop::ContextMenuExtension ClassBrowserPlugin::contextMenuExtension(KDevelop::Context* context, QWidget* parent)
{
KDevelop::ContextMenuExtension menuExt = KDevelop::IPlugin::contextMenuExtension(context, parent);
// No context menu if we don't have a class browser at hand.
if (m_activeClassTree == nullptr)
return menuExt;
auto* codeContext = dynamic_cast<KDevelop::DeclarationContext*>(context);
if (!codeContext)
return menuExt;
DUChainReadLocker readLock(DUChain::lock());
Declaration* decl(codeContext->declaration().data());
if (decl) {
if (decl->inSymbolTable()) {
if (!ClassTree::populatingClassBrowserContextMenu() &&
ICore::self()->projectController()->findProjectForUrl(decl->url().toUrl()) &&
decl->kind() == Declaration::Type && decl->internalContext() &&
decl->internalContext()->type() == DUContext::Class) {
//Currently "Find in Class Browser" seems to only work for classes, so only show it in that case
m_findInBrowser->setData(QVariant::fromValue(DUChainBasePointer(decl)));
menuExt.addAction(KDevelop::ContextMenuExtension::NavigationGroup, m_findInBrowser);
}
}
}
return menuExt;
}
void ClassBrowserPlugin::findInClassBrowser()
{
ICore::self()->uiController()->findToolView(i18nc("@title:window", "Classes"), m_factory, KDevelop::IUiController::CreateAndRaise);
Q_ASSERT(qobject_cast<QAction*>(sender()));
if (m_activeClassTree == nullptr)
return;
DUChainReadLocker readLock(DUChain::lock());
auto* a = static_cast<QAction*>(sender());
Q_ASSERT(a->data().canConvert<DUChainBasePointer>());
DeclarationPointer decl = qvariant_cast<DUChainBasePointer>(a->data()).dynamicCast<Declaration>();
if (decl)
m_activeClassTree->highlightIdentifier(decl->qualifiedIdentifier());
}
void ClassBrowserPlugin::showDefinition(const DeclarationPointer& declaration)
{
DUChainReadLocker readLock(DUChain::lock());
if (!declaration)
return;
Declaration* decl = declaration.data();
// If it's a function, find the function definition to go to the actual declaration.
if (decl && decl->isFunctionDeclaration()) {
auto* funcDefinition = dynamic_cast<FunctionDefinition*>(decl);
if (funcDefinition == nullptr)
funcDefinition = FunctionDefinition::definition(decl);
if (funcDefinition)
decl = funcDefinition;
}
if (decl) {
QUrl url = decl->url().toUrl();
KTextEditor::Range range = decl->rangeInCurrentRevision();
readLock.unlock();
ICore::self()->documentController()->openDocument(url, range.start());
}
}
#include "classbrowserplugin.moc"
|