diff options
Diffstat (limited to 'src/plugins/cpptools')
-rw-r--r-- | src/plugins/cpptools/cppfindreferences.cpp | 91 | ||||
-rw-r--r-- | src/plugins/cpptools/cppfindreferences.h | 6 | ||||
-rw-r--r-- | src/plugins/cpptools/cppmodelmanager.h | 3 |
3 files changed, 98 insertions, 2 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) { diff --git a/src/plugins/cpptools/cppfindreferences.h b/src/plugins/cpptools/cppfindreferences.h index 5a2dfc4cc5..422eedb0c9 100644 --- a/src/plugins/cpptools/cppfindreferences.h +++ b/src/plugins/cpptools/cppfindreferences.h @@ -40,6 +40,10 @@ class SearchResultItem; class SearchResult; } // namespace Core +namespace ProjectExplorer { +class Node; +} + namespace CppTools { class CppModelManager; @@ -50,6 +54,8 @@ class CppFindReferencesParameters public: QList<QByteArray> symbolId; QByteArray symbolFileName; + QString prettySymbolName; + QVector<ProjectExplorer::Node *> filesToRename; }; class CppFindReferences: public QObject diff --git a/src/plugins/cpptools/cppmodelmanager.h b/src/plugins/cpptools/cppmodelmanager.h index ce2a042bc9..97a93fce60 100644 --- a/src/plugins/cpptools/cppmodelmanager.h +++ b/src/plugins/cpptools/cppmodelmanager.h @@ -180,6 +180,8 @@ public: static void setRefactoringEngine(RefactoringEngineInterface *refactoringEngine); static RefactoringEngineInterface *refactoringEngine(); + void renameIncludes(const QString &oldFileName, const QString &newFileName); + signals: /// Project data might be locked while this is emitted. void aboutToRemoveFiles(const QStringList &files); @@ -205,7 +207,6 @@ private: // This should be executed in the GUI thread. friend class Tests::ModelManagerTestHelper; void onAboutToLoadSession(); - void renameIncludes(const QString &oldFileName, const QString &newFileName); void onProjectAdded(ProjectExplorer::Project *project); void onAboutToRemoveProject(ProjectExplorer::Project *project); void onActiveProjectChanged(ProjectExplorer::Project *project); |