ProjectExplorer: Update header guards also on copying a file

... rather than only on renaming.

Fixes: QTCREATORBUG-26654
Change-Id: I5a723969a078d76cc2e833d5e505ff64a3063bbe
Reviewed-by: <github-actions-qt-creator@cristianadam.eu>
Reviewed-by: Christian Stenger <christian.stenger@qt.io>
This commit is contained in:
Christian Kandeler
2021-12-08 16:26:32 +01:00
parent f1a5a1f716
commit f98a15d544
3 changed files with 35 additions and 14 deletions

View File

@@ -228,18 +228,25 @@ bool FileUtils::renameFile(const FilePath &orgFilePath, const FilePath &newFileP
DocumentManager::renamedFile(orgFilePath, newFilePath); DocumentManager::renamedFile(orgFilePath, newFilePath);
} }
if (result && handleGuards == HandleIncludeGuards::Yes) { if (result)
bool headerUpdateSuccess = updateHeaderFileGuardAfterRename(newFilePath.toString(), updateHeaderFileGuardIfApplicable(orgFilePath, newFilePath, handleGuards);
orgFilePath.baseName()); return result;
if (!headerUpdateSuccess) { }
Core::MessageManager::writeDisrupting(
void FileUtils::updateHeaderFileGuardIfApplicable(const Utils::FilePath &oldFilePath,
const Utils::FilePath &newFilePath,
HandleIncludeGuards handleGuards)
{
if (handleGuards == HandleIncludeGuards::No)
return;
const bool headerUpdateSuccess = updateHeaderFileGuardAfterRename(newFilePath.toString(),
oldFilePath.baseName());
if (headerUpdateSuccess)
return;
MessageManager::writeDisrupting(
QCoreApplication::translate("Core::FileUtils", QCoreApplication::translate("Core::FileUtils",
"Failed to rename the include guard in file \"%1\".") "Failed to rename the include guard in file \"%1\".")
.arg(newFilePath.toUserOutput())); .arg(newFilePath.toUserOutput()));
}
}
return result;
} }
bool FileUtils::updateHeaderFileGuardAfterRename(const QString &headerPath, bool FileUtils::updateHeaderFileGuardAfterRename(const QString &headerPath,

View File

@@ -56,8 +56,15 @@ struct CORE_EXPORT FileUtils
static void removeFiles(const Utils::FilePaths &filePaths, bool deleteFromFS); static void removeFiles(const Utils::FilePaths &filePaths, bool deleteFromFS);
static bool renameFile(const Utils::FilePath &from, const Utils::FilePath &to, static bool renameFile(const Utils::FilePath &from, const Utils::FilePath &to,
HandleIncludeGuards handleGuards = HandleIncludeGuards::No); HandleIncludeGuards handleGuards = HandleIncludeGuards::No);
static void updateHeaderFileGuardIfApplicable(const Utils::FilePath &oldFilePath,
const Utils::FilePath &newFilePath,
HandleIncludeGuards handleGuards);
private:
// This method is used to refactor the include guards in the renamed headers // This method is used to refactor the include guards in the renamed headers
static bool updateHeaderFileGuardAfterRename(const QString &headerPath, const QString &oldHeaderBaseName); static bool updateHeaderFileGuardAfterRename(const QString &headerPath,
const QString &oldHeaderBaseName);
}; };
} // namespace Core } // namespace Core

View File

@@ -3952,6 +3952,12 @@ void ProjectExplorerPluginPrivate::removeFile()
Core::FileUtils::removeFiles(pathList, deleteFile); Core::FileUtils::removeFiles(pathList, deleteFile);
} }
static HandleIncludeGuards canTryToRenameIncludeGuards(const Node *node)
{
return node->asFileNode() && node->asFileNode()->fileType() == FileType::Header
? HandleIncludeGuards::Yes : HandleIncludeGuards::No;
}
void ProjectExplorerPluginPrivate::duplicateFile() void ProjectExplorerPluginPrivate::duplicateFile()
{ {
Node *currentNode = ProjectTree::currentNode(); Node *currentNode = ProjectTree::currentNode();
@@ -3987,6 +3993,9 @@ void ProjectExplorerPluginPrivate::duplicateFile()
QDir::toNativeSeparators(newFilePath), sourceFile.errorString())); QDir::toNativeSeparators(newFilePath), sourceFile.errorString()));
return; return;
} }
Core::FileUtils::updateHeaderFileGuardIfApplicable(currentNode->filePath(),
FilePath::fromString(newFilePath),
canTryToRenameIncludeGuards(currentNode));
if (!folderNode->addFiles({FilePath::fromString(newFilePath)})) { if (!folderNode->addFiles({FilePath::fromString(newFilePath)})) {
QMessageBox::critical(ICore::dialogParent(), tr("Duplicating File Failed"), QMessageBox::critical(ICore::dialogParent(), tr("Duplicating File Failed"),
tr("Failed to add new file \"%1\" to the project.") tr("Failed to add new file \"%1\" to the project.")
@@ -4055,9 +4064,7 @@ void ProjectExplorerPlugin::renameFile(Node *node, const QString &newFileName)
if (oldFilePath == newFilePath) if (oldFilePath == newFilePath)
return; return;
auto handleGuards = Core::HandleIncludeGuards::No; const HandleIncludeGuards handleGuards = canTryToRenameIncludeGuards(node);
if (node->asFileNode() && node->asFileNode()->fileType() == FileType::Header)
handleGuards = Core::HandleIncludeGuards::Yes;
if (!folderNode->canRenameFile(oldFilePath, newFilePath)) { if (!folderNode->canRenameFile(oldFilePath, newFilePath)) {
QTimer::singleShot(0, [oldFilePath, newFilePath, projectFileName, handleGuards] { QTimer::singleShot(0, [oldFilePath, newFilePath, projectFileName, handleGuards] {
int res = QMessageBox::question(ICore::dialogParent(), int res = QMessageBox::question(ICore::dialogParent(),