1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 14:48:14 +00:00

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.
This commit is contained in:
Itamar 2021-03-05 16:42:32 +02:00 committed by Andreas Kling
parent 74070ef74d
commit fa4d9da237
2 changed files with 15 additions and 4 deletions

View file

@ -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();
}
}