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 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
|
/*
* This file is part of KDevelop
* Copyright 2012 André Stein <andre.stein@rwth-aachen.de>
* Copyright 2014 Kevin Funk <kfunk@kde.org>
*
* 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 "switchtobuddyplugin.h"
#include <KLocalizedString>
#include <KPluginFactory>
#include <KActionCollection>
#include <QAction>
#include <QFile>
#include <QMimeDatabase>
#include <QMimeType>
#include <interfaces/icore.h>
#include <interfaces/idocumentcontroller.h>
#include <interfaces/ibuddydocumentfinder.h>
#include <interfaces/contextmenuextension.h>
#include <language/interfaces/editorcontext.h>
#include <language/duchain/duchainutils.h>
#include <language/duchain/functiondefinition.h>
#include <language/duchain/parsingenvironment.h>
#include <language/duchain/classfunctiondeclaration.h>
#include <debug.h>
#include <KTextEditor/Document>
#include <KTextEditor/View>
using namespace KDevelop;
namespace {
KTextEditor::Cursor normalizeCursor(KTextEditor::Cursor c)
{
c.setColumn(0);
return c;
}
///Tries to find a definition for the declaration at given cursor-position and document-url. DUChain must be locked.
Declaration* definitionForCursorDeclaration(const KTextEditor::Cursor& cursor, const QUrl& url)
{
const QList<TopDUContext*> topContexts = DUChain::self()->chainsForDocument(url);
for (TopDUContext* ctx : topContexts) {
Declaration* decl = DUChainUtils::declarationInLine(cursor, ctx);
if (!decl) {
continue;
}
if (auto definition = FunctionDefinition::definition(decl)) {
return definition;
}
}
return nullptr;
}
QString findSwitchCandidate(const QUrl& docUrl)
{
QMimeDatabase db;
IBuddyDocumentFinder* finder = IBuddyDocumentFinder::finderForMimeType(db.mimeTypeForUrl(docUrl).name());
if (finder) {
// get the first entry that exists, use that as candidate
const auto potentialBuddies = finder->potentialBuddies(docUrl);
for (const QUrl& buddyUrl : potentialBuddies) {
if (!QFile::exists(buddyUrl.toLocalFile())) {
continue;
}
return buddyUrl.toLocalFile();
}
}
return QString();
}
}
K_PLUGIN_FACTORY_WITH_JSON(SwitchToBuddyPluginFactory, "kdevswitchtobuddy.json", registerPlugin<SwitchToBuddyPlugin>(); )
SwitchToBuddyPlugin::SwitchToBuddyPlugin ( QObject* parent, const QVariantList& )
: IPlugin ( QStringLiteral("kdevswitchtobuddy"), parent )
{
setXMLFile(QStringLiteral("kdevswitchtobuddy.rc"));
}
SwitchToBuddyPlugin::~SwitchToBuddyPlugin()
{
}
ContextMenuExtension SwitchToBuddyPlugin::contextMenuExtension(Context* context, QWidget* parent)
{
auto* ctx = dynamic_cast<EditorContext*>(context);
if (!ctx) {
return ContextMenuExtension();
}
QUrl currentUrl = ctx->url();
IBuddyDocumentFinder* buddyFinder = IBuddyDocumentFinder::finderForMimeType(QMimeDatabase().mimeTypeForUrl(currentUrl).name());
if (!buddyFinder)
return ContextMenuExtension();
// Get all potential buddies for the current document and add a switch-to action
// for each buddy who really exists in the file system. Note: if no buddies could be calculated
// no extension actions are generated.
const QVector<QUrl>& potentialBuddies = buddyFinder->potentialBuddies(currentUrl);
ContextMenuExtension extension;
for (const QUrl& url : potentialBuddies) {
if (!QFile::exists(url.toLocalFile())) {
continue;
}
auto* action = new QAction(i18nc("@action:inmenu", "Switch to '%1'", url.fileName()), parent);
const QString surl = url.toLocalFile();
connect(action, &QAction::triggered, this, [this, surl](){ switchToBuddy(surl); }, Qt::QueuedConnection);
extension.addAction(ContextMenuExtension::NavigationGroup, action);
}
return extension;
}
void SwitchToBuddyPlugin::createActionsForMainWindow(Sublime::MainWindow* /*window*/, QString& xmlFile, KActionCollection& actions)
{
xmlFile = this->xmlFile();
QAction* switchDefinitionDeclaration = actions.addAction(QStringLiteral("switch_definition_declaration"));
switchDefinitionDeclaration->setText(i18nc("@action", "&Switch Definition/Declaration"));
actions.setDefaultShortcut(switchDefinitionDeclaration, Qt::CTRL | Qt::SHIFT | Qt::Key_C);
connect(switchDefinitionDeclaration, &QAction::triggered, this, &SwitchToBuddyPlugin::switchDefinitionDeclaration);
QAction* switchHeaderSource = actions.addAction(QStringLiteral("switch_header_source"));
switchHeaderSource->setText(i18nc("@action", "Switch Header/Source"));
actions.setDefaultShortcut(switchHeaderSource, Qt::CTRL | Qt::Key_Slash);
connect(switchHeaderSource, &QAction::triggered, this, &SwitchToBuddyPlugin::switchHeaderSource);
}
void SwitchToBuddyPlugin::switchHeaderSource()
{
qCDebug(PLUGIN_SWITCHTOBUDDY) << "switching header/source";
auto doc = ICore::self()->documentController()->activeDocument();
if (!doc)
return;
QString buddyUrl = findSwitchCandidate(doc->url());
if (!buddyUrl.isEmpty())
switchToBuddy(buddyUrl);
}
void SwitchToBuddyPlugin::switchToBuddy(const QString& url)
{
KDevelop::ICore::self()->documentController()->openDocument(QUrl::fromLocalFile(url));
}
void SwitchToBuddyPlugin::switchDefinitionDeclaration()
{
qCDebug(PLUGIN_SWITCHTOBUDDY) << "switching definition/declaration";
QUrl docUrl;
KTextEditor::Cursor cursor;
///Step 1: Find the current top-level context of type DUContext::Other(the highest code-context).
///-- If it belongs to a function-declaration or definition, it can be retrieved through owner(), and we are in a definition.
///-- If no such context could be found, search for a declaration on the same line as the cursor, and switch to the according definition
{
auto view = ICore::self()->documentController()->activeTextDocumentView();
if (!view) {
qCDebug(PLUGIN_SWITCHTOBUDDY) << "No active document";
return;
}
docUrl = view->document()->url();
cursor = view->cursorPosition();
}
QString switchCandidate = findSwitchCandidate(docUrl);
if(!switchCandidate.isEmpty()) {
DUChainReadLocker lock;
//If the file has not been parsed yet, update it
TopDUContext* ctx = DUChainUtils::standardContextForUrl(docUrl);
//At least 'VisibleDeclarationsAndContexts' is required so we can do a switch
if (!ctx || (ctx->parsingEnvironmentFile() && !ctx->parsingEnvironmentFile()->featuresSatisfied(TopDUContext::AllDeclarationsContextsAndUses))) {
lock.unlock();
qCDebug(PLUGIN_SWITCHTOBUDDY) << "Parsing switch-candidate before switching" << switchCandidate;
ReferencedTopDUContext updatedContext = DUChain::self()->waitForUpdate(IndexedString(switchCandidate), TopDUContext::AllDeclarationsContextsAndUses);
if (!updatedContext) {
qCDebug(PLUGIN_SWITCHTOBUDDY) << "Failed to update document:" << switchCandidate;
return;
}
}
}
qCDebug(PLUGIN_SWITCHTOBUDDY) << "Document:" << docUrl;
DUChainReadLocker lock;
TopDUContext* standardCtx = DUChainUtils::standardContextForUrl(docUrl);
bool wasSignal = false;
if (standardCtx) {
Declaration* definition = nullptr;
DUContext* ctx = standardCtx->findContext(standardCtx->transformToLocalRevision(cursor));
if (!ctx) {
ctx = standardCtx;
}
while (ctx && ctx->parentContext() && (ctx->parentContext()->type() == DUContext::Other || ctx->parentContext()->type() == DUContext::Function)) {
ctx = ctx->parentContext();
}
if (ctx && ctx->owner() && (ctx->type() == DUContext::Other || ctx->type() == DUContext::Function) && ctx->owner()->isDefinition()) {
definition = ctx->owner();
qCDebug(PLUGIN_SWITCHTOBUDDY) << "found definition while traversing:" << definition->toString();
}
if (!definition && ctx) {
definition = DUChainUtils::declarationInLine(cursor, ctx);
}
if (auto* cDef = dynamic_cast<ClassFunctionDeclaration*>(definition)) {
if (cDef->isSignal()) {
qCDebug(PLUGIN_SWITCHTOBUDDY) << "found definition is a signal, not switching to .moc implementation";
definition = nullptr;
wasSignal = true;
}
}
auto* def = dynamic_cast<FunctionDefinition*>(definition);
if (def && def->declaration()) {
Declaration* declaration = def->declaration();
KTextEditor::Range targetRange = declaration->rangeInCurrentRevision();
const auto url = declaration->url().toUrl();
qCDebug(PLUGIN_SWITCHTOBUDDY) << "found definition that has declaration: " << definition->toString() << "range" << targetRange << "url" << url;
lock.unlock();
auto view = ICore::self()->documentController()->activeTextDocumentView();
if (view && !targetRange.contains(view->cursorPosition())) {
const auto pos = normalizeCursor(targetRange.start());
ICore::self()->documentController()->openDocument(url, KTextEditor::Range(pos, pos));
} else {
ICore::self()->documentController()->openDocument(url);
}
return;
} else {
qCDebug(PLUGIN_SWITCHTOBUDDY) << "Definition has no assigned declaration";
}
qCDebug(PLUGIN_SWITCHTOBUDDY) << "Could not get definition/declaration from context";
} else {
qCDebug(PLUGIN_SWITCHTOBUDDY) << "Got no context for the current document";
}
Declaration* def = nullptr;
if (!wasSignal) {
def = definitionForCursorDeclaration(cursor, docUrl);
}
if (def) {
const auto url = def->url().toUrl();
KTextEditor::Range targetRange = def->rangeInCurrentRevision();
if (def->internalContext()) {
targetRange.end() = def->internalContext()->rangeInCurrentRevision().end();
} else {
qCDebug(PLUGIN_SWITCHTOBUDDY) << "Declaration does not have internal context";
}
lock.unlock();
auto view = ICore::self()->documentController()->activeTextDocumentView();
if (view && !targetRange.contains(view->cursorPosition())) {
KTextEditor::Cursor pos(normalizeCursor(targetRange.start()));
ICore::self()->documentController()->openDocument(url, KTextEditor::Range(pos, pos));
} else {
//The cursor is already in the target range, only open the document
ICore::self()->documentController()->openDocument(url);
}
return;
} else if (!wasSignal) {
qCWarning(PLUGIN_SWITCHTOBUDDY) << "Found no definition assigned to cursor position";
}
lock.unlock();
///- If no definition/declaration could be found to switch to, just switch the document using normal header/source heuristic by file-extension
if (!switchCandidate.isEmpty()) {
ICore::self()->documentController()->openDocument(QUrl::fromUserInput(switchCandidate));
} else {
qCDebug(PLUGIN_SWITCHTOBUDDY) << "Found no source/header candidate to switch";
}
}
#include "switchtobuddyplugin.moc"
|