From f98a15d544f867343526e847e8f5a918731b6037 Mon Sep 17 00:00:00 2001 From: Christian Kandeler Date: Wed, 8 Dec 2021 16:26:32 +0100 Subject: [PATCH] ProjectExplorer: Update header guards also on copying a file ... rather than only on renaming. Fixes: QTCREATORBUG-26654 Change-Id: I5a723969a078d76cc2e833d5e505ff64a3063bbe Reviewed-by: Reviewed-by: Christian Stenger --- src/plugins/coreplugin/fileutils.cpp | 27 ++++++++++++------- src/plugins/coreplugin/fileutils.h | 9 ++++++- .../projectexplorer/projectexplorer.cpp | 13 ++++++--- 3 files changed, 35 insertions(+), 14 deletions(-) diff --git a/src/plugins/coreplugin/fileutils.cpp b/src/plugins/coreplugin/fileutils.cpp index 64cf96613e3..6e7e7407cfc 100644 --- a/src/plugins/coreplugin/fileutils.cpp +++ b/src/plugins/coreplugin/fileutils.cpp @@ -228,18 +228,25 @@ bool FileUtils::renameFile(const FilePath &orgFilePath, const FilePath &newFileP DocumentManager::renamedFile(orgFilePath, newFilePath); } - if (result && handleGuards == HandleIncludeGuards::Yes) { - bool headerUpdateSuccess = updateHeaderFileGuardAfterRename(newFilePath.toString(), - orgFilePath.baseName()); - if (!headerUpdateSuccess) { - Core::MessageManager::writeDisrupting( + if (result) + updateHeaderFileGuardIfApplicable(orgFilePath, newFilePath, handleGuards); + return result; +} + +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", "Failed to rename the include guard in file \"%1\".") - .arg(newFilePath.toUserOutput())); - } - } - - return result; + .arg(newFilePath.toUserOutput())); } bool FileUtils::updateHeaderFileGuardAfterRename(const QString &headerPath, diff --git a/src/plugins/coreplugin/fileutils.h b/src/plugins/coreplugin/fileutils.h index 74f14f9e748..d024583000f 100644 --- a/src/plugins/coreplugin/fileutils.h +++ b/src/plugins/coreplugin/fileutils.h @@ -56,8 +56,15 @@ struct CORE_EXPORT FileUtils static void removeFiles(const Utils::FilePaths &filePaths, bool deleteFromFS); static bool renameFile(const Utils::FilePath &from, const Utils::FilePath &to, 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 - static bool updateHeaderFileGuardAfterRename(const QString &headerPath, const QString &oldHeaderBaseName); + static bool updateHeaderFileGuardAfterRename(const QString &headerPath, + const QString &oldHeaderBaseName); }; } // namespace Core diff --git a/src/plugins/projectexplorer/projectexplorer.cpp b/src/plugins/projectexplorer/projectexplorer.cpp index d30891a7260..02b553fb0b9 100644 --- a/src/plugins/projectexplorer/projectexplorer.cpp +++ b/src/plugins/projectexplorer/projectexplorer.cpp @@ -3952,6 +3952,12 @@ void ProjectExplorerPluginPrivate::removeFile() 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() { Node *currentNode = ProjectTree::currentNode(); @@ -3987,6 +3993,9 @@ void ProjectExplorerPluginPrivate::duplicateFile() QDir::toNativeSeparators(newFilePath), sourceFile.errorString())); return; } + Core::FileUtils::updateHeaderFileGuardIfApplicable(currentNode->filePath(), + FilePath::fromString(newFilePath), + canTryToRenameIncludeGuards(currentNode)); if (!folderNode->addFiles({FilePath::fromString(newFilePath)})) { QMessageBox::critical(ICore::dialogParent(), tr("Duplicating File Failed"), 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) return; - auto handleGuards = Core::HandleIncludeGuards::No; - if (node->asFileNode() && node->asFileNode()->fileType() == FileType::Header) - handleGuards = Core::HandleIncludeGuards::Yes; + const HandleIncludeGuards handleGuards = canTryToRenameIncludeGuards(node); if (!folderNode->canRenameFile(oldFilePath, newFilePath)) { QTimer::singleShot(0, [oldFilePath, newFilePath, projectFileName, handleGuards] { int res = QMessageBox::question(ICore::dialogParent(),