Core: filepathify file renaming

Change-Id: I3d4f39e34e65cde3df7b7c19570e3a54d0625d53
Reviewed-by: Christian Stenger <christian.stenger@qt.io>
Reviewed-by: David Schulz <david.schulz@qt.io>
This commit is contained in:
David Schulz
2021-06-11 14:34:34 +02:00
parent 55b91a7617
commit f66df921d7
32 changed files with 203 additions and 176 deletions

View File

@@ -1450,30 +1450,29 @@ QSet<QString> CppModelManager::internalTargets(const Utils::FilePath &filePath)
return targets;
}
void CppModelManager::renameIncludes(const QString &oldFileName, const QString &newFileName)
void CppModelManager::renameIncludes(const Utils::FilePath &oldFilePath,
const Utils::FilePath &newFilePath)
{
if (oldFileName.isEmpty() || newFileName.isEmpty())
if (oldFilePath.isEmpty() || newFilePath.isEmpty())
return;
const QFileInfo oldFileInfo(oldFileName);
const QFileInfo newFileInfo(newFileName);
// We just want to handle renamings so return when the file was actually moved.
if (oldFileInfo.absoluteDir() != newFileInfo.absoluteDir())
if (oldFilePath.absolutePath() != newFilePath.absolutePath())
return;
const TextEditor::RefactoringChanges changes;
foreach (Snapshot::IncludeLocation loc, snapshot().includeLocationsOfDocument(oldFileName)) {
foreach (Snapshot::IncludeLocation loc,
snapshot().includeLocationsOfDocument(oldFilePath.toString())) {
TextEditor::RefactoringFilePtr file = changes.file(
Utils::FilePath::fromString(loc.first->fileName()));
const QTextBlock &block = file->document()->findBlockByNumber(loc.second - 1);
const int replaceStart = block.text().indexOf(oldFileInfo.fileName());
const int replaceStart = block.text().indexOf(oldFilePath.fileName());
if (replaceStart > -1) {
Utils::ChangeSet changeSet;
changeSet.replace(block.position() + replaceStart,
block.position() + replaceStart + oldFileInfo.fileName().length(),
newFileInfo.fileName());
block.position() + replaceStart + oldFilePath.fileName().length(),
newFilePath.fileName());
file->setChangeSet(changeSet);
file->apply();
}

View File

@@ -241,7 +241,7 @@ public:
QSet<QString> internalTargets(const Utils::FilePath &filePath) const;
void renameIncludes(const QString &oldFileName, const QString &newFileName);
void renameIncludes(const Utils::FilePath &oldFilePath, const Utils::FilePath &newFilePath);
// for VcsBaseSubmitEditor
Q_INVOKABLE QSet<QString> symbolsInFiles(const QSet<Utils::FilePath> &files) const;

View File

@@ -1035,7 +1035,9 @@ void CppToolsPlugin::test_modelmanager_renameIncludes()
QCOMPARE(snapshot.allIncludesForDocument(sourceFile), QSet<QString>() << oldHeader);
// Renaming the header
QVERIFY(Core::FileUtils::renameFile(oldHeader, newHeader, Core::HandleIncludeGuards::Yes));
QVERIFY(Core::FileUtils::renameFile(Utils::FilePath::fromString(oldHeader),
Utils::FilePath::fromString(newHeader),
Core::HandleIncludeGuards::Yes));
// Update the c++ model manager again and check for the new includes
modelManager->updateSourceFiles(sourceFiles).waitForFinished();
@@ -1103,12 +1105,14 @@ void CppToolsPlugin::test_modelmanager_renameIncludesInEditor()
QVERIFY(modelManager->workingCopy().contains(mainFile));
// Test the renaming of a header file where a pragma once guard is present
QVERIFY(Core::FileUtils::renameFile(headerWithPragmaOnce, renamedHeaderWithPragmaOnce,
QVERIFY(Core::FileUtils::renameFile(Utils::FilePath::fromString(headerWithPragmaOnce),
Utils::FilePath::fromString(renamedHeaderWithPragmaOnce),
Core::HandleIncludeGuards::Yes));
// Test the renaming the header with include guard:
// The contents should match the foobar2000.h in the testdata_project2 project
QVERIFY(Core::FileUtils::renameFile(headerWithNormalGuard, renamedHeaderWithNormalGuard,
QVERIFY(Core::FileUtils::renameFile(Utils::FilePath::fromString(headerWithNormalGuard),
Utils::FilePath::fromString(renamedHeaderWithNormalGuard),
Core::HandleIncludeGuards::Yes));
const MyTestDataDir testDir2(_("testdata_project2"));
@@ -1125,8 +1129,10 @@ void CppToolsPlugin::test_modelmanager_renameIncludesInEditor()
// Test the renaming the header with underscore pre/suffixed include guard:
// The contents should match the foobar2000.h in the testdata_project2 project
QVERIFY(Core::FileUtils::renameFile(headerWithUnderscoredGuard, renamedHeaderWithUnderscoredGuard,
Core::HandleIncludeGuards::Yes));
QVERIFY(
Core::FileUtils::renameFile(Utils::FilePath::fromString(headerWithUnderscoredGuard),
Utils::FilePath::fromString(renamedHeaderWithUnderscoredGuard),
Core::HandleIncludeGuards::Yes));
QFile foobar4000Header(testDir2.file("foobar4000.h"));
QVERIFY(foobar4000Header.open(QFile::ReadOnly | QFile::Text));
@@ -1146,7 +1152,8 @@ void CppToolsPlugin::test_modelmanager_renameIncludesInEditor()
auto originalMalformedGuardContents = renamedHeader.readAll();
renamedHeader.close();
QVERIFY(Core::FileUtils::renameFile(headerWithMalformedGuard, renamedHeaderWithMalformedGuard,
QVERIFY(Core::FileUtils::renameFile(Utils::FilePath::fromString(headerWithMalformedGuard),
Utils::FilePath::fromString(renamedHeaderWithMalformedGuard),
Core::HandleIncludeGuards::Yes));
renamedHeader.setFileName(renamedHeaderWithMalformedGuard);