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 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
|
/***************************************************************************
* This file is part of KDevelop *
* Copyright 2008 David Nolden <david.nolden.kdevelop@art-master.de> *
* *
* 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 Library 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 "simplerefactoring.h"
#include "../cpputils.h"
#include <QAction>
#include <KMessageBox>
#include <ktexteditor/document.h>
#include <kparts/mainwindow.h>
#include <language/backgroundparser/backgroundparser.h>
#include <language/codegen/documentchangeset.h>
#include <language/duchain/classdeclaration.h>
#include <language/duchain/classfunctiondeclaration.h>
#include <language/duchain/classmemberdeclaration.h>
#include <language/duchain/duchain.h>
#include <language/duchain/duchainlock.h>
#include <language/duchain/duchainutils.h>
#include <language/duchain/functiondefinition.h>
#include <language/duchain/types/functiontype.h>
#include <language/duchain/use.h>
#include <language/interfaces/codecontext.h>
#include <interfaces/contextmenuextension.h>
#include <interfaces/icore.h>
#include <interfaces/idocumentcontroller.h>
#include <interfaces/ilanguagecontroller.h>
#include <interfaces/iproject.h>
#include <interfaces/iuicontroller.h>
#include <templatedeclaration.h>
#include <sourcemanipulation.h>
using namespace KDevelop;
SimpleRefactoring::SimpleRefactoring(QObject *parent)
: BasicRefactoring(parent)
{
/* There's nothing to do here. */
}
void SimpleRefactoring::fillContextMenu(KDevelop::ContextMenuExtension& extension, KDevelop::Context* context) {
if(DeclarationContext* declContext = dynamic_cast<DeclarationContext*>(context)){
//Actions on declarations
qRegisterMetaType<KDevelop::IndexedDeclaration>("KDevelop::IndexedDeclaration");
DUChainReadLocker lock(DUChain::lock());
Declaration* declaration = declContext->declaration().data();
if(declaration) {
QFileInfo finfo(declaration->topContext()->url().str());
if (finfo.isWritable()) {
QAction* action = new QAction(i18n("Rename %1", declaration->qualifiedIdentifier().toString()), this);
action->setData(QVariant::fromValue(IndexedDeclaration(declaration)));
action->setIcon(KIcon("edit-rename"));
connect(action, SIGNAL(triggered(bool)), this, SLOT(executeRenameAction()));
extension.addAction(ContextMenuExtension::RefactorGroup, action);
if(declContext->use().isEmpty() && declaration->isFunctionDeclaration() && declaration->internalContext() && declaration->internalContext()->type() == DUContext::Other &&
!dynamic_cast<Cpp::TemplateDeclaration*>(declaration)) {
AbstractFunctionDeclaration* funDecl = dynamic_cast<AbstractFunctionDeclaration*>(declaration);
if(funDecl && !funDecl->isInline() && !dynamic_cast<FunctionDefinition*>(funDecl)) {
//Is a candidate for moving into source
QAction* action = new QAction(i18n("Create separate definition for %1", declaration->qualifiedIdentifier().toString()), this);
action->setData(QVariant::fromValue(IndexedDeclaration(declaration)));
// action->setIcon(KIcon("arrow-right"));
connect(action, SIGNAL(triggered(bool)), this, SLOT(executeMoveIntoSourceAction()));
extension.addAction(ContextMenuExtension::RefactorGroup, action);
}
}
}
}
}
}
QString SimpleRefactoring::moveIntoSource(const IndexedDeclaration& iDecl)
{
DUChainReadLocker lock;
Declaration* decl = iDecl.data();
if(!decl) {
return i18n("No declaration under cursor");
}
const KDevelop::IndexedString url = decl->url();
QString targetUrl = url.str();
if(CppUtils::headerExtensions().contains(QFileInfo(targetUrl).suffix())) {
targetUrl = CppUtils::sourceOrHeaderCandidate(targetUrl);
}
if(targetUrl.isEmpty()) {
///@todo Create source file if it doesn't exist yet
return i18n("No source file available for %1.", url.str());
}
lock.unlock();
const IndexedString indexedTargetUrl(targetUrl);
KDevelop::ReferencedTopDUContext top = DUChain::self()->waitForUpdate(url, KDevelop::TopDUContext::AllDeclarationsAndContexts);
KDevelop::ReferencedTopDUContext targetTopContext = DUChain::self()->waitForUpdate(indexedTargetUrl, KDevelop::TopDUContext::AllDeclarationsAndContexts);
lock.lock();
if(!targetTopContext) {
///@todo Eventually create source file if it doesn't exist yet
return i18n("Failed to update DUChain for %1.", targetUrl);
}
if(!top || !iDecl.data() || iDecl.data() != decl) {
return i18n("Declaration lost while updating.");
}
kDebug() << "moving" << decl->qualifiedIdentifier();
AbstractFunctionDeclaration* funDecl = dynamic_cast<AbstractFunctionDeclaration*>(decl);
FunctionType::Ptr funType = decl->type<FunctionType>();
if( !(decl->internalContext() && decl->internalContext()->type() == DUContext::Other
&& funDecl && funDecl->internalFunctionContext() && funType) )
{
return i18n("Cannot create definition for this declaration.");
}
CodeRepresentation::Ptr code = createCodeRepresentation(decl->url());
if(!code) {
return i18n("No document for %1", decl->url().toUrl().prettyUrl());
}
SimpleRange headerRange = decl->internalContext()->rangeInCurrentRevision();
// remove whitespace in front of the header range
KTextEditor::Range prefixRange(funDecl->internalFunctionContext()->range().end.castToSimpleCursor().textCursor()
+ KTextEditor::Cursor(0, 1) /* skip ) of function context */,
headerRange.start.textCursor());
const QString prefixText = code->rangeText(prefixRange);
for (int i = prefixText.length() - 1; i >= 0 && prefixText.at(i).isSpace(); --i) {
if (headerRange.start.column == 0) {
headerRange.start.line--;
if (headerRange.start.line == prefixRange.start().line()) {
headerRange.start.column = prefixRange.start().column() + i;
} else {
int lastNewline = prefixText.lastIndexOf('\n', i - 1);
headerRange.start.column = i - lastNewline - 1;
kWarning() << "UNSUPPORTED" << headerRange.start.column << lastNewline << i << prefixText;
}
} else {
headerRange.start.column--;
}
}
const QString body = code->rangeText(headerRange.textRange());
SourceCodeInsertion ins(targetTopContext);
QualifiedIdentifier namespaceIdentifier = decl->internalContext()->parentContext()->scopeIdentifier(false);
ins.setSubScope(namespaceIdentifier);
QList<SourceCodeInsertion::SignatureItem> signature;
foreach(Declaration* argument, funDecl->internalFunctionContext()->localDeclarations()) {
SourceCodeInsertion::SignatureItem item;
item.name = argument->identifier().toString();
item.type = argument->abstractType();
signature.append(item);
}
kDebug() << "qualified id:" << decl->qualifiedIdentifier() << "from mid:" << decl->qualifiedIdentifier().mid(namespaceIdentifier.count()) << namespaceIdentifier.count();
Identifier id(IndexedString(decl->qualifiedIdentifier().mid(namespaceIdentifier.count()).toString()));
kDebug() << "id:" << id;
if(!ins.insertFunctionDeclaration(id, funType->returnType(), signature, funType->modifiers() & AbstractType::ConstModifier, body)) {
return i18n("Insertion failed");
}
lock.unlock();
DocumentChangeSet::ChangeResult applied = ins.changes().applyAllChanges();
if(!applied) {
return i18n("Applying changes failed: %1", applied.m_failureReason);
}
// replace header function body with a semicolon
DocumentChangeSet changeHeader;
changeHeader.addChange(DocumentChange(decl->url(), headerRange, body, ";"));
applied = changeHeader.applyAllChanges();
if(!applied) {
return i18n("Applying changes failed: %1", applied.m_failureReason);
}
ICore::self()->languageController()->backgroundParser()->addDocument(url);
ICore::self()->languageController()->backgroundParser()->addDocument(indexedTargetUrl);
return QString();
}
void SimpleRefactoring::executeMoveIntoSourceAction() {
QAction* action = qobject_cast<QAction*>(sender());
if(action) {
IndexedDeclaration iDecl = action->data().value<IndexedDeclaration>();
if(!iDecl.isValid())
iDecl = declarationUnderCursor(false);
const QString error = moveIntoSource(iDecl);
if (!error.isEmpty()) {
KMessageBox::error(ICore::self()->uiController()->activeMainWindow(), error);
}
}else{
kWarning() << "strange problem";
}
}
DocumentChangeSet::ChangeResult SimpleRefactoring::addRenameFileChanges(const KUrl& current, const QString& newName, DocumentChangeSet* changes)
{
auto result = KDevelop::BasicRefactoring::addRenameFileChanges(current, newName, changes);
if (!result) {
return result;
}
// check for implementation file
const KUrl otherFile = CppUtils::sourceOrHeaderCandidate(current.toLocalFile());
if (otherFile.isValid()) {
// also rename this other file
result = changes->addDocumentRenameChange(
IndexedString(otherFile), IndexedString(newFileName(otherFile, newName)));
if(!result) {
return result;
}
}
return true;
}
void SimpleRefactoring::startInteractiveRename(const KDevelop::IndexedDeclaration &decl)
{
QString originalName;
Declaration* declaration = nullptr;
{
DUChainReadLocker lock;
declaration = decl.data();
if (!declaration) {
KMessageBox::error(ICore::self()->uiController()->activeMainWindow(), i18n("No declaration under cursor"));
return;
}
QFileInfo info(declaration->topContext()->url().str());
if (!info.isWritable()) {
KMessageBox::error(ICore::self()->uiController()->activeMainWindow(),
i18n("Declaration is located in non-writeable file %1.", declaration->topContext()->url().str()));
return;
}
if (FunctionDefinition* definition = dynamic_cast<FunctionDefinition*>(declaration)) {
// If this is a function-definition, and there is a separate declaration
// available, rename that declaration instead
Declaration* realDeclaration = definition->declaration(declaration->topContext());
if (realDeclaration)
declaration = realDeclaration;
}
// if renaming a ctor, use the class instead which will trigger renaming of all ctors as well
if (ClassFunctionDeclaration* cFunc = dynamic_cast<ClassFunctionDeclaration*>(declaration)) {
if ((cFunc->isConstructor() || cFunc->isDestructor()) && cFunc->context() && cFunc->context()->type() == DUContext::Class && cFunc->context()->owner()) {
declaration = cFunc->context()->owner();
}
}
if (!declaration)
return;
originalName = declaration->identifier().identifier().str();
}
NameAndCollector nc = newNameForDeclaration(DeclarationPointer(declaration));
if (nc.newName == originalName || nc.newName.isEmpty())
return;
DocumentChangeSet changes = BasicRefactoring::renameCollectedDeclarations(nc.collector.data(), nc.newName, originalName, false);
changes.setFormatPolicy(KDevelop::DocumentChangeSet::NoAutoFormat);
m_pendingChanges = changes;
///NOTE: this is required, otherwise, if you rename a file it will crash...
QMetaObject::invokeMethod(this, "applyChangesDelayed", Qt::QueuedConnection);
}
DocumentChangeSet::ChangeResult SimpleRefactoring::applyChangesToDeclarations(
const QString& oldName,
const QString& newName,
DocumentChangeSet& changes,
const QList<IndexedDeclaration>& declarations)
{
auto result = BasicRefactoring::applyChangesToDeclarations(oldName, newName, changes, declarations);
if (!result) {
return result;
}
for (const auto & decl : declarations) {
Declaration* declaration = decl.data();
if (!declaration) {
continue;
}
if (shouldRenameFile(declaration)) {
result = addRenameFileChanges(declaration->topContext()->url().toUrl(), newName, &changes);
if (!result) {
return result;
}
}
}
return DocumentChangeSet::ChangeResult(true);
}
void SimpleRefactoring::applyChangesDelayed()
{
DocumentChangeSet::ChangeResult result = m_pendingChanges.applyAllChanges();
m_pendingChanges = DocumentChangeSet();
if(!result) {
KMessageBox::error(0, i18n("Applying changes failed: %1", result.m_failureReason));
}
}
// #include "simplerefactoring.moc"
|