File indexing completed on 2024-05-12 04:37:41
0001 /* 0002 SPDX-FileCopyrightText: 2012 Olivier de Gaalon <olivier.jg@gmail.com> 0003 SPDX-FileCopyrightText: 2014 Kevin Funk <kfunk@kde.org> 0004 0005 SPDX-License-Identifier: LGPL-2.0-only 0006 */ 0007 0008 #include "renameaction.h" 0009 0010 #include <language/duchain/duchainutils.h> 0011 #include <language/duchain/duchainlock.h> 0012 #include <language/duchain/duchain.h> 0013 #include <language/codegen/documentchangeset.h> 0014 #include <language/backgroundparser/backgroundparser.h> 0015 #include <interfaces/icore.h> 0016 #include <interfaces/ilanguagecontroller.h> 0017 #include <interfaces/iuicontroller.h> 0018 #include <sublime/message.h> 0019 // KF 0020 #include <KLocalizedString> 0021 0022 using namespace KDevelop; 0023 0024 QVector<RevisionedFileRanges> RevisionedFileRanges::convert(const QMap<IndexedString, QVector<RangeInRevision>>& uses) 0025 { 0026 QVector<RevisionedFileRanges> ret(uses.size()); 0027 auto insertIt = ret.begin(); 0028 for (auto it = uses.constBegin(); it != uses.constEnd(); ++it, ++insertIt) { 0029 insertIt->file = it.key(); 0030 insertIt->ranges = it.value(); 0031 0032 DocumentChangeTracker* tracker = 0033 ICore::self()->languageController()->backgroundParser()->trackerForUrl(it.key()); 0034 if (tracker) { 0035 insertIt->revision = tracker->revisionAtLastReset(); 0036 } 0037 } 0038 0039 return ret; 0040 } 0041 0042 class KDevelop::RenameActionPrivate 0043 { 0044 public: 0045 Identifier m_oldDeclarationName; 0046 QString m_newDeclarationName; 0047 QVector<RevisionedFileRanges> m_oldDeclarationUses; 0048 }; 0049 0050 RenameAction::RenameAction(const Identifier& oldDeclarationName, const QString& newDeclarationName, 0051 const QVector<RevisionedFileRanges>& oldDeclarationUses) 0052 : d_ptr(new RenameActionPrivate) 0053 { 0054 Q_D(RenameAction); 0055 0056 d->m_oldDeclarationName = oldDeclarationName; 0057 d->m_newDeclarationName = newDeclarationName.trimmed(); 0058 d->m_oldDeclarationUses = oldDeclarationUses; 0059 } 0060 0061 RenameAction::~RenameAction() 0062 { 0063 } 0064 0065 QString RenameAction::description() const 0066 { 0067 Q_D(const RenameAction); 0068 0069 return i18n("Rename \"%1\" to \"%2\"", d->m_oldDeclarationName.toString(), d->m_newDeclarationName); 0070 } 0071 0072 QString RenameAction::newDeclarationName() const 0073 { 0074 Q_D(const RenameAction); 0075 0076 return d->m_newDeclarationName; 0077 } 0078 0079 QString RenameAction::oldDeclarationName() const 0080 { 0081 Q_D(const RenameAction); 0082 0083 return d->m_oldDeclarationName.toString(); 0084 } 0085 0086 void RenameAction::execute() 0087 { 0088 Q_D(RenameAction); 0089 0090 DocumentChangeSet changes; 0091 0092 for (const RevisionedFileRanges& ranges : qAsConst(d->m_oldDeclarationUses)) { 0093 for (const RangeInRevision range : ranges.ranges) { 0094 KTextEditor::Range currentRange; 0095 if (ranges.revision && ranges.revision->valid()) { 0096 currentRange = ranges.revision->transformToCurrentRevision(range); 0097 } else { 0098 currentRange = range.castToSimpleRange(); 0099 } 0100 DocumentChange useRename(ranges.file, currentRange, 0101 d->m_oldDeclarationName.toString(), d->m_newDeclarationName); 0102 changes.addChange(useRename); 0103 changes.setReplacementPolicy(DocumentChangeSet::WarnOnFailedChange); 0104 } 0105 } 0106 0107 DocumentChangeSet::ChangeResult result = changes.applyAllChanges(); 0108 if (!result) { 0109 auto* message = new Sublime::Message(i18n("Failed to apply changes: %1", result.m_failureReason), Sublime::Message::Error); 0110 ICore::self()->uiController()->postMessage(message); 0111 } 0112 0113 emit executed(this); 0114 } 0115 0116 #include "moc_renameaction.cpp"