From 71b3c6950f93a7761d44079ea2065d487a8bd5c9 Mon Sep 17 00:00:00 2001 From: Cristian Adam Date: Wed, 26 Apr 2023 18:09:24 +0200 Subject: [PATCH] CMakePM: Parse the CMake file before adding new / existing files This way the functionality would work even if autorun CMake is disabled. Change-Id: I54ab47d72664cb42486b260b895f58d37a885cce Reviewed-by: hjk Reviewed-by: --- .../cmakeprojectmanager/cmakebuildsystem.cpp | 20 ++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/src/plugins/cmakeprojectmanager/cmakebuildsystem.cpp b/src/plugins/cmakeprojectmanager/cmakebuildsystem.cpp index a493062621c..0ea477752ca 100644 --- a/src/plugins/cmakeprojectmanager/cmakebuildsystem.cpp +++ b/src/plugins/cmakeprojectmanager/cmakebuildsystem.cpp @@ -271,10 +271,19 @@ bool CMakeBuildSystem::addFiles(Node *context, const FilePaths &filePaths, FileP const FilePath targetCMakeFile = target.backtrace.last().path; const int targetDefinitionLine = target.backtrace.last().line; - auto cmakeListFile - = Utils::findOrDefault(m_cmakeFiles, [targetCMakeFile](const CMakeFileInfo &info) { - return info.path == targetCMakeFile; - }).cmakeListFile; + // Have a fresh look at the CMake file, not relying on a cached value + expected_str fileContent = targetCMakeFile.fileContents(); + cmListFile cmakeListFile; + std::string errorString; + if (fileContent) { + fileContent = fileContent->replace("\r\n", "\n"); + if (!cmakeListFile.ParseString(fileContent->toStdString(), + targetCMakeFile.fileName().toStdString(), + errorString)) { + *notAdded = filePaths; + return false; + } + } auto function = std::find_if(cmakeListFile.Functions.begin(), cmakeListFile.Functions.end(), @@ -360,7 +369,8 @@ bool CMakeBuildSystem::addFiles(Node *context, const FilePaths &filePaths, FileP editor->insert(snippet); editor->editorWidget()->autoIndent(); - Core::DocumentManager::saveDocument(editor->document()); + if (!Core::DocumentManager::saveDocument(editor->document())) + return false; return true; }