diff --git a/DevTools/HackStudio/Project.cpp b/DevTools/HackStudio/Project.cpp index 0a3dacd0b7..f841e33d85 100644 --- a/DevTools/HackStudio/Project.cpp +++ b/DevTools/HackStudio/Project.cpp @@ -79,3 +79,12 @@ bool Project::add_file(const String& filename) m_model->update(); return true; } + +TextDocument* Project::get_file(const String& filename) +{ + for (auto& file : m_files) { + if (file.name() == filename) + return &file; + } + return nullptr; +} diff --git a/DevTools/HackStudio/Project.h b/DevTools/HackStudio/Project.h index d68b936e59..f564ae15c6 100644 --- a/DevTools/HackStudio/Project.h +++ b/DevTools/HackStudio/Project.h @@ -14,6 +14,8 @@ public: [[nodiscard]] bool add_file(const String& filename); + TextDocument* get_file(const String& filename); + GModel& model() { return *m_model; } template diff --git a/DevTools/HackStudio/TextDocument.cpp b/DevTools/HackStudio/TextDocument.cpp index 6dd74b3c04..6bcd83efb3 100644 --- a/DevTools/HackStudio/TextDocument.cpp +++ b/DevTools/HackStudio/TextDocument.cpp @@ -2,6 +2,15 @@ #include #include +const GTextDocument& TextDocument::document() const +{ + if (!m_document) { + m_document = GTextDocument::create(nullptr); + m_document->set_text(contents()); + } + return *m_document; +} + const ByteBuffer& TextDocument::contents() const { if (m_contents.is_null()) { diff --git a/DevTools/HackStudio/TextDocument.h b/DevTools/HackStudio/TextDocument.h index 81cd1a51e5..6d52960e3b 100644 --- a/DevTools/HackStudio/TextDocument.h +++ b/DevTools/HackStudio/TextDocument.h @@ -4,6 +4,7 @@ #include #include #include +#include class TextDocument : public RefCounted { public: @@ -18,6 +19,8 @@ public: Vector find(const StringView&) const; + const GTextDocument& document() const; + private: explicit TextDocument(const String& name) : m_name(name) @@ -26,4 +29,5 @@ private: String m_name; mutable ByteBuffer m_contents; + mutable RefPtr m_document; }; diff --git a/DevTools/HackStudio/main.cpp b/DevTools/HackStudio/main.cpp index 9ae7ef47c4..e90a01c474 100644 --- a/DevTools/HackStudio/main.cpp +++ b/DevTools/HackStudio/main.cpp @@ -305,13 +305,8 @@ static void rehighlight() void open_file(const String& filename) { - auto file = CFile::construct(filename); - if (!file->open(CFile::ReadOnly)) { - GMessageBox::show("Could not open!", "Error", GMessageBox::Type::Error, GMessageBox::InputType::OK, g_window); - return; - } - auto contents = file->read_all(); - current_editor().set_text(contents); + auto file = g_project->get_file(filename); + current_editor().set_document(const_cast(file->document())); if (filename.ends_with(".cpp")) { current_editor().on_change = [] { rehighlight(); };