summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorItamar <itamar8910@gmail.com>2021-03-05 16:42:32 +0200
committerAndreas Kling <kling@serenityos.org>2021-03-06 09:40:33 +0100
commitfa4d9da2373995f16bd6a494f0e39077d11096d3 (patch)
tree904e3276763ea38d61e32bf23c0700edbe465ead
parent74070ef74dae78fed179f80ee18f5b079b8843ef (diff)
downloadserenity-fa4d9da2373995f16bd6a494f0e39077d11096d3.zip
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.
-rw-r--r--Userland/DevTools/HackStudio/Project.cpp15
-rw-r--r--Userland/DevTools/HackStudio/Project.h4
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<void(const ProjectFile&)> callback) co
});
}
-RefPtr<ProjectFile> Project::get_file(const String& path) const
+NonnullRefPtr<ProjectFile> 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<ProjectFile> get_file(const String& path) const;
+ NonnullRefPtr<ProjectFile> get_file(const String& path) const;
void for_each_text_file(Function<void(const ProjectFile&)>) const;
private:
explicit Project(const String& root_path);
+ String to_absolute_path(const String&) const;
+
RefPtr<GUI::FileSystemModel> m_model;
mutable NonnullRefPtrVector<ProjectFile> m_files;