CppEditor: Move renameFilesForSymbol() to ProjectExplorer

We'd like to use it outside of CppEditor in the future.

Change-Id: Ie7553a17aa1d86fb898eaa315a1613ac0ff6b452
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Reviewed-by: <github-actions-qt-creator@cristianadam.eu>
Reviewed-by: David Schulz <david.schulz@qt.io>
This commit is contained in:
Christian Kandeler
2022-09-21 13:38:42 +02:00
parent 5e99a16e46
commit a522215840
7 changed files with 66 additions and 73 deletions

View File

@@ -15,6 +15,7 @@
#include <cppeditor/cpptoolsreuse.h>
#include <languageclient/languageclientsymbolsupport.h>
#include <languageserverprotocol/lsptypes.h>
#include <projectexplorer/projectexplorer.h>
#include <projectexplorer/projectnodes.h>
#include <projectexplorer/projecttree.h>
#include <projectexplorer/session.h>
@@ -169,14 +170,10 @@ void ClangdFindReferences::Private::handleRenameRequest(
if (!renameFilesCheckBox->isChecked())
return;
QVector<Node *> fileNodes;
for (const Utils::FilePath &file : replacementData.fileRenameCandidates) {
Node * const node = ProjectTree::nodeForFile(file);
if (node)
fileNodes << node;
}
if (!fileNodes.isEmpty())
renameFilesForSymbol(replacementData.oldSymbolName, newSymbolName, fileNodes);
ProjectExplorerPlugin::renameFilesForSymbol(
replacementData.oldSymbolName, newSymbolName,
Utils::toList(replacementData.fileRenameCandidates),
CppEditor::preferLowerCaseFileNames());
}
void ClangdFindReferences::Private::handleFindUsagesResult(const QList<Location> &locations)

View File

@@ -5,7 +5,6 @@
#include "cppcodemodelsettings.h"
#include "cppeditorconstants.h"
#include "cppfilesettingspage.h"
#include "cppmodelmanager.h"
#include "cpptoolsreuse.h"
#include "cppworkingcopy.h"
@@ -42,8 +41,6 @@ using namespace std::placeholders;
namespace CppEditor {
namespace { static bool isAllLowerCase(const QString &text) { return text.toLower() == text; } }
SearchResultColor::Style colorStyleForUsageType(CPlusPlus::Usage::Type type)
{
switch (type) {
@@ -60,51 +57,6 @@ SearchResultColor::Style colorStyleForUsageType(CPlusPlus::Usage::Type type)
return SearchResultColor::Style::Default; // For dumb compilers.
}
void renameFilesForSymbol(const QString &oldSymbolName, const QString &newSymbolName,
const QVector<Node *> &files)
{
Internal::CppFileSettings settings;
settings.fromSettings(Core::ICore::settings());
const QStringList newPaths =
Utils::transform<QList>(files,
[&oldSymbolName, newSymbolName, &settings](const Node *node) -> QString {
const QFileInfo fi = node->filePath().toFileInfo();
const QString oldBaseName = fi.baseName();
QString newBaseName = newSymbolName;
// 1) new symbol lowercase: new base name lowercase
if (isAllLowerCase(newSymbolName)) {
newBaseName = newSymbolName;
// 2) old base name mixed case: new base name is verbatim symbol name
} else if (!isAllLowerCase(oldBaseName)) {
newBaseName = newSymbolName;
// 3) old base name lowercase, old symbol mixed case: new base name lowercase
} else if (!isAllLowerCase(oldSymbolName)) {
newBaseName = newSymbolName.toLower();
// 4) old base name lowercase, old symbol lowercase, new symbol mixed case:
// use the preferences setting for new base name case
} else if (settings.lowerCaseFiles) {
newBaseName = newSymbolName.toLower();
}
if (newBaseName == oldBaseName)
return QString();
return fi.absolutePath() + "/" + newBaseName + '.' + fi.completeSuffix();
});
for (int i = 0; i < files.size(); ++i) {
if (!newPaths.at(i).isEmpty()) {
Node *node = files.at(i);
ProjectExplorerPlugin::renameFile(node, newPaths.at(i));
}
}
}
QWidget *CppSearchResultFilter::createWidget()
{
const auto widget = new QWidget;
@@ -531,7 +483,9 @@ void CppFindReferences::onReplaceButtonClicked(Core::SearchResult *search,
if (!renameFilesCheckBox || !renameFilesCheckBox->isChecked())
return;
renameFilesForSymbol(parameters.prettySymbolName, text, parameters.filesToRename);
ProjectExplorerPlugin::renameFilesForSymbol(
parameters.prettySymbolName, text, parameters.filesToRename,
preferLowerCaseFileNames());
}
void CppFindReferences::searchAgain(SearchResult *search)
@@ -639,16 +593,14 @@ static void displayResults(SearchResult *search,
if (parameters.prettySymbolName.isEmpty())
continue;
if (Utils::contains(parameters.filesToRename, Utils::equal(&Node::filePath, result.path)))
if (parameters.filesToRename.contains(result.path))
continue;
Node *node = ProjectTree::nodeForFile(result.path);
if (!node) // Not part of any project
if (!SessionManager::projectForFile(result.path))
continue;
const QFileInfo fi = node->filePath().toFileInfo();
if (fi.baseName().compare(parameters.prettySymbolName, Qt::CaseInsensitive) == 0)
parameters.filesToRename.append(node);
if (result.path.baseName().compare(parameters.prettySymbolName, Qt::CaseInsensitive) == 0)
parameters.filesToRename.append(result.path);
}
search->setUserData(QVariant::fromValue(parameters));
@@ -661,9 +613,7 @@ static void searchFinished(SearchResult *search, QFutureWatcher<CPlusPlus::Usage
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();
});
= Utils::transform<QList>(parameters.filesToRename, &FilePath::toUserOutput);
auto renameCheckBox = qobject_cast<QCheckBox *>(search->additionalReplaceWidget());
if (renameCheckBox) {

View File

@@ -7,6 +7,7 @@
#include <coreplugin/find/searchresultwindow.h>
#include <cplusplus/FindUsages.h>
#include <utils/filepath.h>
#include <QObject>
#include <QPointer>
@@ -19,18 +20,12 @@ class SearchResultItem;
class SearchResult;
} // namespace Core
namespace ProjectExplorer { class Node; }
namespace CppEditor {
class CppModelManager;
Core::SearchResultColor::Style CPPEDITOR_EXPORT
colorStyleForUsageType(CPlusPlus::Usage::Type type);
void CPPEDITOR_EXPORT renameFilesForSymbol(const QString &oldSymbolName,
const QString &newSymbolName,
const QVector<ProjectExplorer::Node *> &files);
class CPPEDITOR_EXPORT CppSearchResultFilter : public Core::SearchResultFilter
{
QWidget *createWidget() override;
@@ -52,7 +47,7 @@ public:
QList<QByteArray> symbolId;
QByteArray symbolFileName;
QString prettySymbolName;
QVector<ProjectExplorer::Node *> filesToRename;
Utils::FilePaths filesToRename;
bool categorize = false;
};

View File

@@ -9,6 +9,7 @@
#include "cppcompletionassist.h"
#include "cppeditorconstants.h"
#include "cppeditorplugin.h"
#include "cppfilesettingspage.h"
#include "cpphighlighter.h"
#include "cppqtstyleindenter.h"
#include "cppquickfixassistant.h"
@@ -614,6 +615,11 @@ void openEditor(const Utils::FilePath &filePath, bool inNextSplit, Utils::Id edi
: EditorManager::NoFlags);
}
bool preferLowerCaseFileNames()
{
return Internal::CppEditorPlugin::fileSettings()->lowerCaseFiles;
}
namespace Internal {
void decorateCppEditor(TextEditor::TextEditorWidget *editor)

View File

@@ -70,6 +70,8 @@ void CPPEDITOR_EXPORT openEditor(const Utils::FilePath &filePath, bool inNextSpl
class CppCodeModelSettings;
CppCodeModelSettings CPPEDITOR_EXPORT *codeModelSettings();
bool CPPEDITOR_EXPORT preferLowerCaseFileNames();
UsePrecompiledHeaders CPPEDITOR_EXPORT getPchUsage();
int indexerFileSizeLimitInMb();

View File

@@ -4350,6 +4350,46 @@ RecentProjectsEntries ProjectExplorerPlugin::recentProjects()
return dd->recentProjects();
}
void ProjectExplorerPlugin::renameFilesForSymbol(
const QString &oldSymbolName, const QString &newSymbolName, const Utils::FilePaths &files,
bool preferLowerCaseFileNames)
{
static const auto isAllLowerCase = [](const QString &text) { return text.toLower() == text; };
for (const FilePath &file : files) {
Node * const node = ProjectTree::nodeForFile(file);
if (!node)
continue;
const QString oldBaseName = file.baseName();
QString newBaseName = newSymbolName;
// 1) new symbol lowercase: new base name lowercase
if (isAllLowerCase(newSymbolName)) {
newBaseName = newSymbolName;
// 2) old base name mixed case: new base name is verbatim symbol name
} else if (!isAllLowerCase(oldBaseName)) {
newBaseName = newSymbolName;
// 3) old base name lowercase, old symbol mixed case: new base name lowercase
} else if (!isAllLowerCase(oldSymbolName)) {
newBaseName = newSymbolName.toLower();
// 4) old base name lowercase, old symbol lowercase, new symbol mixed case:
// use the preferences setting for new base name case
} else if (preferLowerCaseFileNames) {
newBaseName = newSymbolName.toLower();
}
if (newBaseName == oldBaseName)
continue;
const QString newFilePath = file.absolutePath().toString() + '/' + newBaseName + '.'
+ file.completeSuffix();
renameFile(node, newFilePath);
}
}
void ProjectManager::registerProjectCreator(const QString &mimeType,
const std::function<Project *(const FilePath &)> &creator)
{

View File

@@ -136,6 +136,9 @@ public:
static bool isProjectFile(const Utils::FilePath &filePath);
static RecentProjectsEntries recentProjects();
static void renameFilesForSymbol(const QString &oldSymbolName, const QString &newSymbolName,
const Utils::FilePaths &files, bool preferLowerCaseFileNames);
static bool canRunStartupProject(Utils::Id runMode, QString *whyNot = nullptr);
static void runProject(Project *pro, Utils::Id, const bool forceSkipDeploy = false);
static void runStartupProject(Utils::Id runMode, bool forceSkipDeploy = false);