From dc0efb9b70ba131cbf7172b306e96c6931787514 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mattias=20Lasersk=C3=B6ld?= Date: Sat, 27 Jul 2024 15:12:52 +0200 Subject: [PATCH] Update the cache from the root dir in project settings --- src/core/coreenvironment.cpp | 2 +- src/files/project.cpp | 10 +++--- src/files/project.h | 3 +- src/files/projectsettings.cpp | 62 +++++++++++++++++++++++------------ src/views/mainwindow.cpp | 4 +-- test/project_test.cpp | 2 +- 6 files changed, 50 insertions(+), 33 deletions(-) diff --git a/src/core/coreenvironment.cpp b/src/core/coreenvironment.cpp index e5cb93e..aa886e2 100644 --- a/src/core/coreenvironment.cpp +++ b/src/core/coreenvironment.cpp @@ -32,7 +32,7 @@ CoreEnvironment::CoreEnvironment(ThreadContext &context, std::ofstream{_consoleTtyPath.path()} << std::endl; // Prevent the listen file from waiting - _project->updateCache(std::filesystem::current_path()); + _project->updateCache(); subscribeToLog([this](LogType type, std::string_view str) { printToAllLogs(type, std::string{str}); diff --git a/src/files/project.cpp b/src/files/project.cpp index 8ecf4d3..833c197 100644 --- a/src/files/project.cpp +++ b/src/files/project.cpp @@ -41,11 +41,10 @@ Project::Project(std::filesystem::path projectPath, _settings.load(projectFile); } -void Project::updateCache(const std::filesystem::path &pathInProject, - size_t max) { +void Project::updateCache(size_t max) { _tv(); - _fileCache = findProjectFiles(pathInProject, max); loadProjectFile(); + _fileCache = findProjectFiles(_settings.root, max); if (_settings.buildCommand.empty()) { _settings.buildCommand = guessBuildCommand(); } @@ -141,9 +140,6 @@ std::vector Project::findProjectFiles( #endif auto &root = _settings.root; - // if (root.empty()) { - // root = this->findRoot(pathInProject); - // } if (root.empty()) { return {}; @@ -190,6 +186,8 @@ std::vector Project::findProjectFiles( } } + paths.push_back(_settings.settingsPath); + _extensions.clear(); _extensions.assign(extensions.begin(), extensions.end()); std::sort(_extensions.begin(), _extensions.end(), [](auto &a, auto &b) { diff --git a/src/files/project.h b/src/files/project.h index 21195bc..2853fed 100644 --- a/src/files/project.h +++ b/src/files/project.h @@ -20,8 +20,7 @@ class Project { DirectoryNotifications &files, IJobQueue &guiQueue); - void updateCache(const std::filesystem::path &pathInProject, - size_t max = 100000); + void updateCache(size_t max = 100000); const ProjectSettings &settings() const { return _settings; diff --git a/src/files/projectsettings.cpp b/src/files/projectsettings.cpp index 69ee043..27ef6af 100644 --- a/src/files/projectsettings.cpp +++ b/src/files/projectsettings.cpp @@ -7,6 +7,7 @@ #include #include #include +#include #include #include @@ -44,7 +45,12 @@ bool ProjectSettings::load(std::filesystem::path projectFile) { auto json = Json{}; try { - std::fstream{projectFile} >> json; + auto file = std::ifstream{projectFile}; + if (!file.is_open()) { + throw std::runtime_error{"could not open file " + + projectFile.string()}; + } + file >> json; } catch (Json::ParsingError &error) { logStatusMessage(error.what()); @@ -65,31 +71,31 @@ bool ProjectSettings::load(std::filesystem::path projectFile) { } } - // TODO: Add more multiple build configuration + // TODO: Add multiple build configuration if (auto it = json.find("build"); it != json.end()) { buildCommand = it->value; } - // TODO: Add more multiple build configuration + // TODO: Add multiple build configuration if (auto it = json.find("run"); it != json.end()) { runCommand = it->value; } - if (auto it = json.find("flags"); it != json.end()) { - flags.clear(); + // if (auto it = json.find("flags"); it != json.end()) { + // flags.clear(); - if (it->type == Json::String) { - flags.push_back(it->value); - } - else if (it->type == Json::Array) { - flags.clear(); - for (auto &value : *it) { - if (value.type == Json::String) { - flags.push_back(translateInclude(value.value, root)); - } - } - } - } + // if (it->type == Json::String) { + // flags.push_back(it->value); + // } + // else if (it->type == Json::Array) { + // flags.clear(); + // for (auto &value : *it) { + // if (value.type == Json::String) { + // flags.push_back(translateInclude(value.value, root)); + // } + // } + // } + // } if (auto it = json.find("debug"); it != json.end()) { if (auto command = it->find("command"); command != it->end()) { @@ -104,16 +110,26 @@ bool ProjectSettings::load(std::filesystem::path projectFile) { } void ProjectSettings::save() { - std::cerr << "saving project is not implemented yet" << std::endl; + // std::cerr << "saving project is not implemented yet" << std::endl; auto json = Json{}; - auto &flags = json["flags"] = Json{Json::Array}; + // auto &flags = json["flags"]; + // flags.type = Json::Array; - for (auto &flag : flags) { - flags.push_back(flag); + // for (auto &flag : flags) { + // flags.push_back(flag); + // } + + if (settingsPath.empty()) { + // Cannot save project + return; } + json["root"] = root; + + std::ofstream{settingsPath} << json; + // TODO: Continue implementing this } @@ -196,6 +212,9 @@ void ProjectSettings::makeProjectAvailable(std::filesystem::path path) { } std::filesystem::path ProjectSettings::findRoot(std::filesystem::path arg) { + if (arg.empty()) { + arg = std::filesystem::current_path(); + } auto path = std::filesystem::absolute(arg); do { @@ -223,6 +242,7 @@ std::filesystem::path ProjectSettings::createAnonymousProject( auto settings = ProjectSettings{}; settings.settingsPath = (projectDirectory() / root.stem().string()) .replace_extension(projectExtension); + settings.root = root; settings.save(); settings.makeProjectAvailable(settings.settingsPath); return settings.settingsPath; diff --git a/src/views/mainwindow.cpp b/src/views/mainwindow.cpp index 79a7f67..ff25b50 100644 --- a/src/views/mainwindow.cpp +++ b/src/views/mainwindow.cpp @@ -134,7 +134,7 @@ MainWindow::MainWindow(CoreEnvironment &core, _inputFocus = editor; }); - updateLocatorBuffer(); + // updateLocatorBuffer(); } MainWindow::~MainWindow() { @@ -262,7 +262,7 @@ bool MainWindow::keyPress(std::shared_ptr env) { void MainWindow::updateLocatorBuffer() { auto &project = _env->core().project(); - project.updateCache(std::filesystem::current_path()); + project.updateCache(); _env->core().files().directoryNotifications().path( project.settings() .root); // TODO: This should probably be handled somewhere else diff --git a/test/project_test.cpp b/test/project_test.cpp index e11e3da..3099693 100644 --- a/test/project_test.cpp +++ b/test/project_test.cpp @@ -19,7 +19,7 @@ TEST_CASE("load") { auto project = Project{path, dirNotifications, jobQueue}; - project.updateCache(path); + project.updateCache(); ASSERT_EQ(project.settings().root, std::filesystem::absolute(root));