diff options
author | Daniel Teske <qt@squorn.de> | 2015-09-30 18:04:17 +0200 |
---|---|---|
committer | André Hartmann <aha_1980@gmx.de> | 2017-05-16 16:04:17 +0000 |
commit | ef951eaea775c150378bbb2ef81c292f72aa7360 (patch) | |
tree | 7dbe46c9013e5955a6e7349377f2275c98272b57 /src/plugins/cpptools/cppfindreferences.cpp | |
parent | d728317c87a6e2bfae93eb1516bce0fb80341561 (diff) | |
download | qt-creator-ef951eaea775c150378bbb2ef81c292f72aa7360.tar.gz |
Class Renaming: Offer to rename files that match the symbols name
Task-number: QTCREATORBUG-14696
Change-Id: I6d140dac510e47d1a19d6759148f5f24dad44062
Reviewed-by: Ivan Donchevskii <ivan.donchevskii@qt.io>
Reviewed-by: Nikolai Kosjar <nikolai.kosjar@qt.io>
Reviewed-by: Orgad Shaneh <orgads@gmail.com>
Diffstat (limited to 'src/plugins/cpptools/cppfindreferences.cpp')
-rw-r--r-- | src/plugins/cpptools/cppfindreferences.cpp | 91 |
1 files changed, 90 insertions, 1 deletions
diff --git a/src/plugins/cpptools/cppfindreferences.cpp b/src/plugins/cpptools/cppfindreferences.cpp index de598e3d61..5426664483 100644 --- a/src/plugins/cpptools/cppfindreferences.cpp +++ b/src/plugins/cpptools/cppfindreferences.cpp @@ -33,6 +33,9 @@ #include <coreplugin/icore.h> #include <coreplugin/progressmanager/futureprogress.h> #include <coreplugin/progressmanager/progressmanager.h> +#include <projectexplorer/projectexplorer.h> +#include <projectexplorer/projectnodes.h> +#include <projectexplorer/session.h> #include <texteditor/basefilefind.h> #include <utils/algorithm.h> @@ -42,6 +45,7 @@ #include <cplusplus/Overview.h> #include <QtConcurrentMap> +#include <QCheckBox> #include <QDir> #include <functional> @@ -50,6 +54,7 @@ using namespace Core; using namespace CppTools::Internal; using namespace CppTools; using namespace CPlusPlus; +using namespace ProjectExplorer; static QByteArray getSource(const Utils::FileName &fileName, const WorkingCopy &workingCopy) @@ -336,6 +341,12 @@ void CppFindReferences::findUsages(Symbol *symbol, CppFindReferencesParameters parameters; parameters.symbolId = fullIdForSymbol(symbol); parameters.symbolFileName = QByteArray(symbol->fileName()); + + if (symbol->isClass() || symbol->isForwardClassDeclaration()) { + Overview overview; + parameters.prettySymbolName = overview.prettyName(context.path(symbol).last()); + } + search->setUserData(qVariantFromValue(parameters)); findAll_helper(search, symbol, context); } @@ -382,12 +393,48 @@ void CppFindReferences::onReplaceButtonClicked(const QString &text, m_modelManager->updateSourceFiles(fileNames.toSet()); SearchResultWindow::instance()->hide(); } + + auto search = qobject_cast<SearchResult *>(sender()); + QTC_ASSERT(search, return); + + CppFindReferencesParameters parameters = search->userData().value<CppFindReferencesParameters>(); + if (parameters.filesToRename.isEmpty()) + return; + + auto renameFilesCheckBox = qobject_cast<QCheckBox *>(search->additionalReplaceWidget()); + if (!renameFilesCheckBox || !renameFilesCheckBox->isChecked()) + return; + + const QStringList newPaths = + Utils::transform<QList>(parameters.filesToRename, + [¶meters, text](const Node *node) -> QString { + const QFileInfo fi = node->filePath().toFileInfo(); + const QString fileName = fi.fileName(); + QString newName = fileName; + newName.replace(parameters.prettySymbolName, text, Qt::CaseInsensitive); + + if (newName != fileName) { + newName = Utils::matchCaseReplacement(fileName, newName); + + return fi.absolutePath() + "/" + newName; + } + + return QString(); + }); + + for (int i = 0; i < parameters.filesToRename.size(); ++i) { + if (!newPaths.at(i).isEmpty()) { + Node *node = parameters.filesToRename.at(i); + ProjectExplorerPlugin::renameFile(node, newPaths.at(i)); + } + } } void CppFindReferences::searchAgain() { SearchResult *search = qobject_cast<SearchResult *>(sender()); CppFindReferencesParameters parameters = search->userData().value<CppFindReferencesParameters>(); + parameters.filesToRename.clear(); Snapshot snapshot = CppModelManager::instance()->snapshot(); search->restart(); LookupContext context; @@ -467,6 +514,8 @@ Symbol *CppFindReferences::findSymbol(const CppFindReferencesParameters ¶met static void displayResults(SearchResult *search, QFutureWatcher<Usage> *watcher, int first, int last) { + CppFindReferencesParameters parameters = search->userData().value<CppFindReferencesParameters>(); + for (int index = first; index != last; ++index) { Usage result = watcher->future().resultAt(index); search->addResult(result.path.toString(), @@ -474,7 +523,45 @@ static void displayResults(SearchResult *search, QFutureWatcher<Usage> *watcher, result.lineText, result.col, result.len); + + if (parameters.prettySymbolName.isEmpty()) + continue; + + if (Utils::contains(parameters.filesToRename, Utils::equal(&Node::filePath, result.path))) + continue; + + Node *node = SessionManager::nodeForFile(result.path); + if (!node) // Not part of any project + continue; + + const QFileInfo fi = node->filePath().toFileInfo(); + if (fi.baseName().compare(parameters.prettySymbolName, Qt::CaseInsensitive) == 0) + parameters.filesToRename.append(node); } + + search->setUserData(qVariantFromValue(parameters)); +} + +static void searchFinished(SearchResult *search, QFutureWatcher<Usage> *watcher) +{ + search->finishSearch(watcher->isCanceled()); + + CppFindReferencesParameters parameters = search->userData().value<CppFindReferencesParameters>(); + if (!parameters.filesToRename.isEmpty()) { + const QStringList filesToRename + = Utils::transform<QList>(parameters.filesToRename, [](const Node *node) { + return node->filePath().toUserOutput(); + }); + + auto renameCheckBox = qobject_cast<QCheckBox *>(search->additionalReplaceWidget()); + if (renameCheckBox) { + renameCheckBox->setText(CppFindReferences::tr("Re&name %1 files.").arg(filesToRename.size())); + renameCheckBox->setToolTip(CppFindReferences::tr("Files:\n%1").arg(filesToRename.join('\n'))); + renameCheckBox->setVisible(true); + } + } + + watcher->deleteLater(); } void CppFindReferences::openEditor(const SearchResultItem &item) @@ -651,7 +738,9 @@ void CppFindReferences::createWatcher(const QFuture<Usage> &future, SearchResult { QFutureWatcher<Usage> *watcher = new QFutureWatcher<Usage>(); // auto-delete: - connect(watcher, &QFutureWatcherBase::finished, watcher, &QObject::deleteLater); + connect(watcher, &QFutureWatcherBase::finished, watcher, [search, watcher]() { + searchFinished(search, watcher); + }); connect(watcher, &QFutureWatcherBase::resultsReadyAt, search, [search, watcher](int first, int last) { |