mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 06:48:12 +00:00
HackStudio: Support opening the same file in both editors
Hey, it actually works! You can now edit the same file in both editors and even the C++ highlighting updates correctly in both of them. :^)
This commit is contained in:
parent
9b13a3905b
commit
e2d7f585da
5 changed files with 26 additions and 7 deletions
|
@ -79,3 +79,12 @@ bool Project::add_file(const String& filename)
|
||||||
m_model->update();
|
m_model->update();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TextDocument* Project::get_file(const String& filename)
|
||||||
|
{
|
||||||
|
for (auto& file : m_files) {
|
||||||
|
if (file.name() == filename)
|
||||||
|
return &file;
|
||||||
|
}
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
|
@ -14,6 +14,8 @@ public:
|
||||||
|
|
||||||
[[nodiscard]] bool add_file(const String& filename);
|
[[nodiscard]] bool add_file(const String& filename);
|
||||||
|
|
||||||
|
TextDocument* get_file(const String& filename);
|
||||||
|
|
||||||
GModel& model() { return *m_model; }
|
GModel& model() { return *m_model; }
|
||||||
|
|
||||||
template<typename Callback>
|
template<typename Callback>
|
||||||
|
|
|
@ -2,6 +2,15 @@
|
||||||
#include <LibCore/CFile.h>
|
#include <LibCore/CFile.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
|
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
|
const ByteBuffer& TextDocument::contents() const
|
||||||
{
|
{
|
||||||
if (m_contents.is_null()) {
|
if (m_contents.is_null()) {
|
||||||
|
|
|
@ -4,6 +4,7 @@
|
||||||
#include <AK/NonnullRefPtr.h>
|
#include <AK/NonnullRefPtr.h>
|
||||||
#include <AK/RefCounted.h>
|
#include <AK/RefCounted.h>
|
||||||
#include <AK/String.h>
|
#include <AK/String.h>
|
||||||
|
#include <LibGUI/GTextDocument.h>
|
||||||
|
|
||||||
class TextDocument : public RefCounted<TextDocument> {
|
class TextDocument : public RefCounted<TextDocument> {
|
||||||
public:
|
public:
|
||||||
|
@ -18,6 +19,8 @@ public:
|
||||||
|
|
||||||
Vector<int> find(const StringView&) const;
|
Vector<int> find(const StringView&) const;
|
||||||
|
|
||||||
|
const GTextDocument& document() const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
explicit TextDocument(const String& name)
|
explicit TextDocument(const String& name)
|
||||||
: m_name(name)
|
: m_name(name)
|
||||||
|
@ -26,4 +29,5 @@ private:
|
||||||
|
|
||||||
String m_name;
|
String m_name;
|
||||||
mutable ByteBuffer m_contents;
|
mutable ByteBuffer m_contents;
|
||||||
|
mutable RefPtr<GTextDocument> m_document;
|
||||||
};
|
};
|
||||||
|
|
|
@ -305,13 +305,8 @@ static void rehighlight()
|
||||||
|
|
||||||
void open_file(const String& filename)
|
void open_file(const String& filename)
|
||||||
{
|
{
|
||||||
auto file = CFile::construct(filename);
|
auto file = g_project->get_file(filename);
|
||||||
if (!file->open(CFile::ReadOnly)) {
|
current_editor().set_document(const_cast<GTextDocument&>(file->document()));
|
||||||
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);
|
|
||||||
|
|
||||||
if (filename.ends_with(".cpp")) {
|
if (filename.ends_with(".cpp")) {
|
||||||
current_editor().on_change = [] { rehighlight(); };
|
current_editor().on_change = [] { rehighlight(); };
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue