forked from qt-creator/qt-creator
CMakePM: Rename source files in set_source_files_properties calls
Fixes: QTCREATORBUG-30174 Change-Id: I80b33c1193f4cd579abd295657dc223aad5801ea Reviewed-by: <github-actions-qt-creator@cristianadam.eu> Reviewed-by: Marcus Tillmanns <marcus.tillmanns@qt.io>
This commit is contained in:
@@ -749,14 +749,17 @@ CMakeBuildSystem::projectFileArgumentPosition(const QString &targetName, const Q
|
|||||||
&& func.Arguments().size() > 1 && func.Arguments().front().Value == target_name;
|
&& func.Arguments().size() > 1 && func.Arguments().front().Value == target_name;
|
||||||
});
|
});
|
||||||
|
|
||||||
for (const auto &func : {function, targetSourcesFunc, addQmlModuleFunc}) {
|
auto setSourceFilePropFunc = findFunction(*cmakeListFile, [](const auto &func) {
|
||||||
|
return func.LowerCaseName() == "set_source_files_properties";
|
||||||
|
});
|
||||||
|
|
||||||
|
for (const auto &func : {function, targetSourcesFunc, addQmlModuleFunc, setSourceFilePropFunc}) {
|
||||||
if (!func.has_value())
|
if (!func.has_value())
|
||||||
continue;
|
continue;
|
||||||
auto filePathArgument = Utils::findOrDefault(func->Arguments(),
|
auto filePathArgument = Utils::findOrDefault(
|
||||||
[file_name = fileName.toStdString()](
|
func->Arguments(), [file_name = fileName.toStdString()](const auto &arg) {
|
||||||
const auto &arg) {
|
return arg.Value == file_name;
|
||||||
return arg.Value == file_name;
|
});
|
||||||
});
|
|
||||||
|
|
||||||
if (!filePathArgument.Value.empty()) {
|
if (!filePathArgument.Value.empty()) {
|
||||||
return ProjectFileArgumentPosition{filePathArgument, targetCMakeFile, fileName};
|
return ProjectFileArgumentPosition{filePathArgument, targetCMakeFile, fileName};
|
||||||
@@ -925,48 +928,59 @@ bool CMakeBuildSystem::renameFile(Node *context,
|
|||||||
{
|
{
|
||||||
if (auto n = dynamic_cast<CMakeTargetNode *>(context)) {
|
if (auto n = dynamic_cast<CMakeTargetNode *>(context)) {
|
||||||
const FilePath projDir = n->filePath().canonicalPath();
|
const FilePath projDir = n->filePath().canonicalPath();
|
||||||
const QString newRelPathName
|
const FilePath newRelPath = newFilePath.canonicalPath().relativePathFrom(projDir).cleanPath();
|
||||||
= newFilePath.canonicalPath().relativePathFrom(projDir).cleanPath().toString();
|
const QString newRelPathName = newRelPath.toString();
|
||||||
|
|
||||||
|
// FilePath needs the file to exist on disk, the old file has already been renamed
|
||||||
|
const QString oldRelPathName
|
||||||
|
= newRelPath.parentDir().pathAppended(oldFilePath.fileName()).cleanPath().toString();
|
||||||
|
|
||||||
const QString targetName = n->buildKey();
|
const QString targetName = n->buildKey();
|
||||||
const QString key
|
const QString key
|
||||||
= QStringList{projDir.path(), targetName, oldFilePath.path(), newFilePath.path()}.join(
|
= QStringList{projDir.path(), targetName, oldFilePath.path(), newFilePath.path()}.join(
|
||||||
";");
|
";");
|
||||||
|
|
||||||
auto fileToRename = m_filesToBeRenamed.take(key);
|
std::optional<CMakeBuildSystem::ProjectFileArgumentPosition> fileToRename
|
||||||
if (!fileToRename.cmakeFile.exists()) {
|
= m_filesToBeRenamed.take(key);
|
||||||
|
if (!fileToRename->cmakeFile.exists()) {
|
||||||
qCCritical(cmakeBuildSystemLog).noquote()
|
qCCritical(cmakeBuildSystemLog).noquote()
|
||||||
<< "File" << fileToRename.cmakeFile.path() << "does not exist.";
|
<< "File" << fileToRename->cmakeFile.path() << "does not exist.";
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
BaseTextEditor *editor = qobject_cast<BaseTextEditor *>(
|
do {
|
||||||
Core::EditorManager::openEditorAt({fileToRename.cmakeFile,
|
BaseTextEditor *editor = qobject_cast<BaseTextEditor *>(
|
||||||
static_cast<int>(fileToRename.argumentPosition.Line),
|
Core::EditorManager::openEditorAt(
|
||||||
static_cast<int>(fileToRename.argumentPosition.Column
|
{fileToRename->cmakeFile,
|
||||||
- 1)},
|
static_cast<int>(fileToRename->argumentPosition.Line),
|
||||||
Constants::CMAKE_EDITOR_ID,
|
static_cast<int>(fileToRename->argumentPosition.Column - 1)},
|
||||||
Core::EditorManager::DoNotMakeVisible));
|
Constants::CMAKE_EDITOR_ID,
|
||||||
if (!editor) {
|
Core::EditorManager::DoNotMakeVisible));
|
||||||
qCCritical(cmakeBuildSystemLog).noquote()
|
if (!editor) {
|
||||||
<< "BaseTextEditor cannot be obtained for" << fileToRename.cmakeFile.path()
|
qCCritical(cmakeBuildSystemLog).noquote()
|
||||||
<< fileToRename.argumentPosition.Line << int(fileToRename.argumentPosition.Column);
|
<< "BaseTextEditor cannot be obtained for" << fileToRename->cmakeFile.path()
|
||||||
return false;
|
<< fileToRename->argumentPosition.Line
|
||||||
}
|
<< int(fileToRename->argumentPosition.Column);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
// If quotes were used for the source file, skip the starting quote
|
// If quotes were used for the source file, skip the starting quote
|
||||||
if (fileToRename.argumentPosition.Delim == cmListFileArgument::Quoted)
|
if (fileToRename->argumentPosition.Delim == cmListFileArgument::Quoted)
|
||||||
editor->setCursorPosition(editor->position() + 1);
|
editor->setCursorPosition(editor->position() + 1);
|
||||||
|
|
||||||
if (!fileToRename.fromGlobbing)
|
if (!fileToRename->fromGlobbing)
|
||||||
editor->replace(fileToRename.relativeFileName.length(), newRelPathName);
|
editor->replace(fileToRename->relativeFileName.length(), newRelPathName);
|
||||||
|
|
||||||
editor->editorWidget()->autoIndent();
|
editor->editorWidget()->autoIndent();
|
||||||
if (!Core::DocumentManager::saveDocument(editor->document())) {
|
if (!Core::DocumentManager::saveDocument(editor->document())) {
|
||||||
qCCritical(cmakeBuildSystemLog).noquote()
|
qCCritical(cmakeBuildSystemLog).noquote()
|
||||||
<< "Changes to" << fileToRename.cmakeFile.path() << "could not be saved.";
|
<< "Changes to" << fileToRename->cmakeFile.path() << "could not be saved.";
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Try the next occurrence. This can happen if set_source_file_properties is used
|
||||||
|
fileToRename = projectFileArgumentPosition(targetName, oldRelPathName);
|
||||||
|
} while (fileToRename);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user