2
Copyright 2012 Olivier de Gaalon <olivier.jg@gmail.com>
3
Copyright 2014 Kevin Funk <kfunk@kde.org>
5
This library is free software; you can redistribute it and/or
6
modify it under the terms of the GNU Library General Public
7
License version 2 as published by the Free Software Foundation.
9
This library is distributed in the hope that it will be useful,
10
but WITHOUT ANY WARRANTY; without even the implied warranty of
11
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12
Library General Public License for more details.
14
You should have received a copy of the GNU Library General Public License
15
along with this library; see the file COPYING.LIB. If not, write to
16
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17
Boston, MA 02110-1301, USA.
20
#include "renameaction.h"
22
#include <language/duchain/duchainutils.h>
23
#include <language/duchain/duchainlock.h>
24
#include <language/duchain/duchain.h>
25
#include <language/codegen/documentchangeset.h>
26
#include <language/backgroundparser/backgroundparser.h>
27
#include <interfaces/icore.h>
28
#include <interfaces/ilanguagecontroller.h>
30
#include <KMessageBox>
31
#include <KLocalizedString>
33
using namespace KDevelop;
35
QVector<RevisionedFileRanges> RevisionedFileRanges::convert(const QMap<IndexedString, QList<RangeInRevision> >& uses)
37
QVector<RevisionedFileRanges> ret(uses.size());
38
auto insertIt = ret.begin();
39
for (auto it = uses.constBegin(); it != uses.constEnd(); ++it, ++insertIt)
41
insertIt->file = it.key();
42
insertIt->ranges = it.value();
44
DocumentChangeTracker* tracker = ICore::self()->languageController()->backgroundParser()->trackerForUrl(it.key());
46
insertIt->revision = tracker->revisionAtLastReset();
52
struct RenameAction::Private
56
Identifier m_oldDeclarationName;
57
QString m_newDeclarationName;
58
QVector<RevisionedFileRanges> m_oldDeclarationUses;
61
RenameAction::RenameAction(const Identifier& oldDeclarationName, const QString& newDeclarationName,
62
const QVector<RevisionedFileRanges>& oldDeclarationUses)
65
d->m_oldDeclarationName = oldDeclarationName;
66
d->m_newDeclarationName = newDeclarationName;
67
d->m_oldDeclarationUses = oldDeclarationUses;
70
RenameAction::~RenameAction()
74
QString RenameAction::description() const
76
return i18n("Rename \"%1\" to \"%2\"", d->m_oldDeclarationName.toString(), d->m_newDeclarationName);
79
QString RenameAction::newDeclarationName() const
81
return d->m_newDeclarationName;
84
QString RenameAction::oldDeclarationName() const
86
return d->m_oldDeclarationName.toString();
89
void RenameAction::execute()
91
DocumentChangeSet changes;
93
foreach(const RevisionedFileRanges& ranges, d->m_oldDeclarationUses) {
94
foreach (const RangeInRevision &range, ranges.ranges) {
95
SimpleRange currentRange;
96
if (ranges.revision && ranges.revision->valid()) {
97
currentRange = ranges.revision->transformToCurrentRevision(range);
99
currentRange = range.castToSimpleRange();
101
DocumentChange useRename(ranges.file, currentRange,
102
d->m_oldDeclarationName.toString(), d->m_newDeclarationName);
103
changes.addChange( useRename );
104
changes.setReplacementPolicy(DocumentChangeSet::WarnOnFailedChange);
108
DocumentChangeSet::ChangeResult result = changes.applyAllChanges();
110
KMessageBox::error(0, i18n("Failed to apply changes: %1", result.m_failureReason));