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
|
/*
* This file is part of KDevelop
*
* Copyright 2015 Sergey Kalinichev <kalinichev.so.0@gmail.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License or (at your option) version 3 or any later version
* accepted by the membership of KDE e.V. (or its successor approved
* by the membership of KDE e.V.), which shall act as a proxy
* defined in Section 14 of version 3 of the license.
*
* 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, see <http://www.gnu.org/licenses/>.
*
*/
#include "clangrefactoring.h"
#include <QAction>
#include <QIcon>
#include <interfaces/context.h>
#include <interfaces/contextmenuextension.h>
#include <language/duchain/classfunctiondeclaration.h>
#include <language/duchain/duchain.h>
#include <language/duchain/duchainlock.h>
#include <language/duchain/functiondeclaration.h>
#include <language/duchain/functiondefinition.h>
#include <language/duchain/types/functiontype.h>
#include <language/interfaces/codecontext.h>
#include <interfaces/icore.h>
#include <interfaces/ilanguagecontroller.h>
#include <interfaces/iuicontroller.h>
#include <language/backgroundparser/backgroundparser.h>
#include <sublime/message.h>
#include "duchain/clanghelpers.h"
#include "duchain/documentfinderhelpers.h"
#include "duchain/duchainutils.h"
#include "sourcemanipulation.h"
#include "util/clangdebug.h"
using namespace KDevelop;
namespace {
bool isDestructor(Declaration* decl)
{
if (auto functionDef = dynamic_cast<FunctionDefinition*>(decl)) {
// we found a definition, e.g. "Foo::~Foo()"
const auto functionDecl = functionDef->declaration(decl->topContext());
if (auto classFunctionDecl = dynamic_cast<ClassFunctionDeclaration*>(functionDecl)) {
return classFunctionDecl->isDestructor();
}
}
else if (auto classFunctionDecl = dynamic_cast<ClassFunctionDeclaration*>(decl)) {
// we found a declaration, e.g. "~Foo()"
return classFunctionDecl->isDestructor();
}
return false;
}
}
ClangRefactoring::ClangRefactoring(QObject* parent)
: BasicRefactoring(parent)
{
qRegisterMetaType<IndexedDeclaration>();
}
void ClangRefactoring::fillContextMenu(ContextMenuExtension& extension, Context* context, QWidget* parent)
{
auto declContext = dynamic_cast<DeclarationContext*>(context);
if (!declContext) {
return;
}
DUChainReadLocker lock;
auto declaration = declContext->declaration().data();
if (!declaration) {
return;
}
QFileInfo fileInfo(declaration->topContext()->url().str());
if (!fileInfo.isWritable()) {
return;
}
auto action = new QAction(i18nc("@action", "Rename %1", declaration->qualifiedIdentifier().toString()), parent);
action->setData(QVariant::fromValue(IndexedDeclaration(declaration)));
action->setIcon(QIcon::fromTheme(QStringLiteral("edit-rename")));
connect(action, &QAction::triggered, this, &ClangRefactoring::executeRenameAction);
extension.addAction(ContextMenuExtension::RefactorGroup, action);
if (!validCandidateToMoveIntoSource(declaration)) {
return;
}
action = new QAction(
i18n("Create separate definition for %1", declaration->qualifiedIdentifier().toString()), parent);
action->setData(QVariant::fromValue(IndexedDeclaration(declaration)));
connect(action, &QAction::triggered, this, &ClangRefactoring::executeMoveIntoSourceAction);
extension.addAction(ContextMenuExtension::RefactorGroup, action);
}
bool ClangRefactoring::validCandidateToMoveIntoSource(Declaration* decl)
{
if (!decl || !decl->isFunctionDeclaration() || !decl->type<FunctionType>()) {
return false;
}
if (!decl->internalContext() || decl->internalContext()->type() != DUContext::Function) {
return false;
}
if (!decl->isDefinition()) {
return false;
}
auto childCtx = decl->internalContext()->childContexts();
if (childCtx.isEmpty()) {
return false;
}
auto ctx = childCtx.first();
if (!ctx || ctx->type() != DUContext::Other) {
return false;
}
auto functionDecl = dynamic_cast<AbstractFunctionDeclaration*>(decl);
if (!functionDecl || functionDecl->isInline()) {
return false;
}
return true;
}
QString ClangRefactoring::moveIntoSource(const IndexedDeclaration& iDecl)
{
DUChainReadLocker lock;
auto decl = iDecl.data();
if (!decl) {
return i18n("No declaration under cursor");
}
const auto headerUrl = decl->url();
auto targetUrl = DocumentFinderHelpers::sourceForHeader(headerUrl.str());
if (targetUrl.isEmpty() || targetUrl == headerUrl.str()) {
// TODO: Create source file if it doesn't exist
return i18n("No source file available for %1.", headerUrl.str());
}
lock.unlock();
const IndexedString indexedTargetUrl(targetUrl);
auto top
= DUChain::self()->waitForUpdate(headerUrl, KDevelop::TopDUContext::AllDeclarationsAndContexts);
auto targetTopContext
= DUChain::self()->waitForUpdate(indexedTargetUrl, KDevelop::TopDUContext::AllDeclarationsAndContexts);
lock.lock();
if (!targetTopContext) {
return i18n("Failed to update DUChain for %1.", targetUrl);
}
if (!top || !iDecl.data() || iDecl.data() != decl) {
return i18n("Declaration lost while updating.");
}
clangDebug() << "moving" << decl->qualifiedIdentifier();
if (!validCandidateToMoveIntoSource(decl)) {
return i18n("Cannot create definition for this declaration.");
}
auto otherCtx = decl->internalContext()->childContexts().first();
auto code = createCodeRepresentation(headerUrl);
if (!code) {
return i18n("No document for %1", headerUrl.str());
}
auto bodyRange = otherCtx->rangeInCurrentRevision();
auto prefixRange(ClangIntegration::DUChainUtils::functionSignatureRange(decl));
const auto prefixText = code->rangeText(prefixRange);
for (int i = prefixText.length() - 1; i >= 0 && prefixText.at(i).isSpace(); --i) {
if (bodyRange.start().column() == 0) {
bodyRange.setStart(bodyRange.start() - KTextEditor::Cursor(1, 0));
if (bodyRange.start().line() == prefixRange.start().line()) {
bodyRange.setStart(KTextEditor::Cursor(bodyRange.start().line(), prefixRange.start().column() + i));
} else {
int lastNewline = prefixText.lastIndexOf(QLatin1Char('\n'), i - 1);
bodyRange.setStart(KTextEditor::Cursor(bodyRange.start().line(), i - lastNewline - 1));
}
} else {
bodyRange.setStart(bodyRange.start() - KTextEditor::Cursor(0, 1));
}
}
const QString body = code->rangeText(bodyRange);
SourceCodeInsertion ins(targetTopContext);
auto parentId = decl->internalContext()->parentContext()->scopeIdentifier(false);
ins.setSubScope(parentId);
Identifier id(IndexedString(decl->qualifiedIdentifier().mid(parentId.count()).toString()));
clangDebug() << "id:" << id;
if (!ins.insertFunctionDeclaration(decl, id, body)) {
return i18n("Insertion failed");
}
lock.unlock();
auto applied = ins.changes().applyAllChanges();
if (!applied) {
return i18n("Applying changes failed: %1", applied.m_failureReason);
}
// replace function body with a semicolon
DocumentChangeSet changeHeader;
changeHeader.addChange(DocumentChange(headerUrl, bodyRange, body, QStringLiteral(";")));
applied = changeHeader.applyAllChanges();
if (!applied) {
return i18n("Applying changes failed: %1", applied.m_failureReason);
}
ICore::self()->languageController()->backgroundParser()->addDocument(headerUrl);
ICore::self()->languageController()->backgroundParser()->addDocument(indexedTargetUrl);
return {};
}
void ClangRefactoring::executeMoveIntoSourceAction()
{
auto action = qobject_cast<QAction*>(sender());
Q_ASSERT(action);
auto iDecl = action->data().value<IndexedDeclaration>();
if (!iDecl.isValid()) {
iDecl = declarationUnderCursor(false);
}
const auto error = moveIntoSource(iDecl);
if (!error.isEmpty()) {
auto* message = new Sublime::Message(error, Sublime::Message::Error);
ICore::self()->uiController()->postMessage(message);
}
}
DocumentChangeSet::ChangeResult ClangRefactoring::applyChangesToDeclarations(const QString& oldName,
const QString& newName,
DocumentChangeSet& changes,
const QList<IndexedDeclaration>& declarations)
{
for (const IndexedDeclaration decl : declarations) {
Declaration *declaration = decl.data();
if (!declaration)
continue;
if (declaration->range().isEmpty())
clangDebug() << "found empty declaration:" << declaration->toString();
// special handling for dtors, their name is not "Foo", but "~Foo"
// see https://bugs.kde.org/show_bug.cgi?id=373452
QString fixedOldName = oldName;
QString fixedNewName = newName;
if (isDestructor(declaration)) {
clangDebug() << "found destructor:" << declaration->toString() << "-- making sure we replace the identifier correctly";
fixedOldName = QLatin1Char('~') + oldName;
fixedNewName = QLatin1Char('~') + newName;
}
TopDUContext *top = declaration->topContext();
DocumentChangeSet::ChangeResult result = changes.addChange(DocumentChange(top->url(), declaration->rangeInCurrentRevision(), fixedOldName, fixedNewName));
if (!result)
return result;
}
return KDevelop::DocumentChangeSet::ChangeResult::successfulResult();
}
|