mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-04-25 22:08:59 +00:00
Projects now contain a set of TextDocument objects. Each TextDocument represents a member file in the project. TextDocuments may not have their file contents loaded at all times, but they will be loaded on demand when calling TextDocument::contents(). "Find in files" works by iterating over the documents in the project and calling find(needle) on each one. The return value from find() is a vector of line numbers where the needle was found. This is obviously going to need a bunch more work. :^)
56 lines
1.5 KiB
C++
56 lines
1.5 KiB
C++
#include "Project.h"
|
|
#include <LibCore/CFile.h>
|
|
|
|
class ProjectModel final : public GModel {
|
|
public:
|
|
explicit ProjectModel(Project& project)
|
|
: m_project(project)
|
|
{
|
|
}
|
|
|
|
virtual int row_count(const GModelIndex& = GModelIndex()) const override { return m_project.m_files.size(); }
|
|
virtual int column_count(const GModelIndex& = GModelIndex()) const override { return 1; }
|
|
virtual GVariant data(const GModelIndex& index, Role role = Role::Display) const override
|
|
{
|
|
int row = index.row();
|
|
if (role == Role::Display) {
|
|
return m_project.m_files.at(row).name();
|
|
}
|
|
if (role == Role::Font) {
|
|
extern String g_currently_open_file;
|
|
if (m_project.m_files.at(row).name() == g_currently_open_file)
|
|
return Font::default_bold_font();
|
|
return {};
|
|
}
|
|
return {};
|
|
}
|
|
virtual void update() override {}
|
|
|
|
private:
|
|
Project& m_project;
|
|
};
|
|
|
|
Project::Project(Vector<String>&& filenames)
|
|
{
|
|
for (auto& filename : filenames) {
|
|
m_files.append(TextDocument::construct_with_name(filename));
|
|
}
|
|
m_model = adopt(*new ProjectModel(*this));
|
|
}
|
|
|
|
OwnPtr<Project> Project::load_from_file(const String& path)
|
|
{
|
|
auto file = CFile::construct(path);
|
|
if (!file->open(CFile::ReadOnly))
|
|
return nullptr;
|
|
|
|
Vector<String> files;
|
|
for (;;) {
|
|
auto line = file->read_line(1024);
|
|
if (line.is_null())
|
|
break;
|
|
files.append(String::copy(line, Chomp));
|
|
}
|
|
|
|
return OwnPtr(new Project(move(files)));
|
|
}
|