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>
This commit is contained in:
Daniel Teske
2015-09-30 18:04:17 +02:00
committed by André Hartmann
parent d728317c87
commit ef951eaea7
7 changed files with 114 additions and 2 deletions

View File

@@ -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,
[&parameters, 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 &paramet
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) {