2019-10-27 16:10:07 +01:00
|
|
|
#pragma once
|
|
|
|
|
2019-10-27 19:36:59 +01:00
|
|
|
#include <AK/Badge.h>
|
2019-10-27 18:00:07 +01:00
|
|
|
#include <AK/HashTable.h>
|
2019-10-27 16:10:07 +01:00
|
|
|
#include <AK/NonnullOwnPtrVector.h>
|
|
|
|
#include <AK/NonnullRefPtr.h>
|
|
|
|
#include <AK/RefCounted.h>
|
|
|
|
#include <LibDraw/Color.h>
|
|
|
|
#include <LibDraw/Font.h>
|
|
|
|
#include <LibGUI/GTextRange.h>
|
|
|
|
|
|
|
|
class GTextEditor;
|
|
|
|
class GTextDocumentLine;
|
|
|
|
|
|
|
|
struct GTextDocumentSpan {
|
|
|
|
GTextRange range;
|
|
|
|
Color color;
|
|
|
|
const Font* font { nullptr };
|
|
|
|
};
|
|
|
|
|
|
|
|
class GTextDocument : public RefCounted<GTextDocument> {
|
|
|
|
public:
|
2019-10-27 18:00:07 +01:00
|
|
|
class Client {
|
|
|
|
public:
|
|
|
|
virtual ~Client();
|
|
|
|
virtual void document_did_append_line() = 0;
|
|
|
|
virtual void document_did_insert_line(int) = 0;
|
|
|
|
virtual void document_did_remove_line(int) = 0;
|
|
|
|
virtual void document_did_remove_all_lines() = 0;
|
2019-10-27 19:36:59 +01:00
|
|
|
virtual void document_did_change() = 0;
|
2019-10-27 18:00:07 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
static NonnullRefPtr<GTextDocument> create(Client* client = nullptr)
|
2019-10-27 16:10:07 +01:00
|
|
|
{
|
2019-10-27 18:00:07 +01:00
|
|
|
return adopt(*new GTextDocument(client));
|
2019-10-27 16:10:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int line_count() const { return m_lines.size(); }
|
|
|
|
const GTextDocumentLine& line(int line_index) const { return m_lines[line_index]; }
|
|
|
|
GTextDocumentLine& line(int line_index) { return m_lines[line_index]; }
|
|
|
|
|
|
|
|
void set_spans(const Vector<GTextDocumentSpan>& spans) { m_spans = spans; }
|
|
|
|
|
|
|
|
void set_text(const StringView&);
|
|
|
|
|
|
|
|
const NonnullOwnPtrVector<GTextDocumentLine>& lines() const { return m_lines; }
|
|
|
|
NonnullOwnPtrVector<GTextDocumentLine>& lines() { return m_lines; }
|
|
|
|
|
|
|
|
bool has_spans() const { return !m_spans.is_empty(); }
|
|
|
|
const Vector<GTextDocumentSpan>& spans() const { return m_spans; }
|
|
|
|
|
2019-10-27 18:00:07 +01:00
|
|
|
void append_line(NonnullOwnPtr<GTextDocumentLine>);
|
|
|
|
void remove_line(int line_index);
|
|
|
|
void remove_all_lines();
|
|
|
|
void insert_line(int line_index, NonnullOwnPtr<GTextDocumentLine>);
|
|
|
|
|
|
|
|
void register_client(Client&);
|
|
|
|
void unregister_client(Client&);
|
|
|
|
|
2019-10-27 19:36:59 +01:00
|
|
|
void update_views(Badge<GTextDocumentLine>);
|
|
|
|
|
2019-10-29 21:36:47 +01:00
|
|
|
String text_in_range(const GTextRange&) const;
|
|
|
|
|
2019-10-27 16:10:07 +01:00
|
|
|
private:
|
2019-10-27 18:00:07 +01:00
|
|
|
explicit GTextDocument(Client* client);
|
2019-10-27 16:10:07 +01:00
|
|
|
|
|
|
|
NonnullOwnPtrVector<GTextDocumentLine> m_lines;
|
|
|
|
Vector<GTextDocumentSpan> m_spans;
|
|
|
|
|
2019-10-27 18:00:07 +01:00
|
|
|
HashTable<Client*> m_clients;
|
2019-10-27 16:10:07 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
class GTextDocumentLine {
|
|
|
|
friend class GTextEditor;
|
|
|
|
friend class GTextDocument;
|
|
|
|
|
|
|
|
public:
|
2019-10-27 19:36:59 +01:00
|
|
|
explicit GTextDocumentLine(GTextDocument&);
|
|
|
|
explicit GTextDocumentLine(GTextDocument&, const StringView&);
|
2019-10-27 16:10:07 +01:00
|
|
|
|
|
|
|
StringView view() const { return { characters(), length() }; }
|
|
|
|
const char* characters() const { return m_text.data(); }
|
|
|
|
int length() const { return m_text.size() - 1; }
|
2019-10-27 19:36:59 +01:00
|
|
|
void set_text(GTextDocument&, const StringView&);
|
|
|
|
void append(GTextDocument&, char);
|
|
|
|
void prepend(GTextDocument&, char);
|
|
|
|
void insert(GTextDocument&, int index, char);
|
|
|
|
void remove(GTextDocument&, int index);
|
|
|
|
void append(GTextDocument&, const char*, int);
|
|
|
|
void truncate(GTextDocument&, int length);
|
|
|
|
void clear(GTextDocument&);
|
2019-10-27 16:10:07 +01:00
|
|
|
int first_non_whitespace_column() const;
|
|
|
|
|
|
|
|
private:
|
|
|
|
// NOTE: This vector is null terminated.
|
|
|
|
Vector<char> m_text;
|
|
|
|
};
|