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,19 +228,26 @@ 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;
}
bool FileUtils::updateHeaderFileGuardAfterRename(const QString &headerPath,
const QString &oldHeaderBaseName)

View File

@@ -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

View File

@@ -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(),