From fa4d9da2373995f16bd6a494f0e39077d11096d3 Mon Sep 17 00:00:00 2001 From: Itamar Date: Fri, 5 Mar 2021 16:42:32 +0200 Subject: [PATCH] HackStudio: Make sure project files are created with an absolute path This fixes an issue were "Find in Files" would not use the up-to-date content of a file with unsaved changes. The issue existed because 'FindInFilesWidget' uses Project::for_each_text_file, which retrieves files by their absolute path. However, when a file is opened in an Editor, it is created with a relative path. This caused us to store two ProjectFile objects that refer to the same file - one with a relative path and one with an absolute path. --- Userland/DevTools/HackStudio/Project.cpp | 15 ++++++++++++--- Userland/DevTools/HackStudio/Project.h | 4 +++- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/Userland/DevTools/HackStudio/Project.cpp b/Userland/DevTools/HackStudio/Project.cpp index f83f2e8940..6db51516fc 100644 --- a/Userland/DevTools/HackStudio/Project.cpp +++ b/Userland/DevTools/HackStudio/Project.cpp @@ -70,15 +70,24 @@ void Project::for_each_text_file(Function callback) co }); } -RefPtr Project::get_file(const String& path) const +NonnullRefPtr Project::get_file(const String& path) const { + auto full_path = to_absolute_path(path); for (auto& file : m_files) { - if (file.name() == path) + if (file.name() == full_path) return file; } - auto file = ProjectFile::construct_with_name(path); + auto file = ProjectFile::construct_with_name(full_path); m_files.append(file); return file; } +String Project::to_absolute_path(const String& path) const +{ + if (LexicalPath { path }.is_absolute()) { + return path; + } + return LexicalPath { String::formatted("{}/{}", m_root_path, path) }.string(); +} + } diff --git a/Userland/DevTools/HackStudio/Project.h b/Userland/DevTools/HackStudio/Project.h index 6d3453e84e..afa1a49f9c 100644 --- a/Userland/DevTools/HackStudio/Project.h +++ b/Userland/DevTools/HackStudio/Project.h @@ -48,13 +48,15 @@ public: String name() const { return LexicalPath(m_root_path).basename(); } String root_path() const { return m_root_path; } - RefPtr get_file(const String& path) const; + NonnullRefPtr get_file(const String& path) const; void for_each_text_file(Function) const; private: explicit Project(const String& root_path); + String to_absolute_path(const String&) const; + RefPtr m_model; mutable NonnullRefPtrVector m_files;