1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
|
#pragma once
#include <AK/Badge.h>
#include <AK/HashTable.h>
#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:
enum class SearchShouldWrap {
No = 0,
Yes
};
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;
virtual void document_did_change() = 0;
};
static NonnullRefPtr<GTextDocument> create(Client* client = nullptr)
{
return adopt(*new GTextDocument(client));
}
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; }
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&);
void update_views(Badge<GTextDocumentLine>);
String text_in_range(const GTextRange&) const;
Vector<GTextRange> find_all(const StringView& needle) const;
GTextRange find_next(const StringView&, const GTextPosition& start = {}, SearchShouldWrap = SearchShouldWrap::Yes) const;
GTextRange find_previous(const StringView&, const GTextPosition& start = {}, SearchShouldWrap = SearchShouldWrap::Yes) const;
GTextPosition next_position_after(const GTextPosition&, SearchShouldWrap = SearchShouldWrap::Yes) const;
GTextPosition previous_position_before(const GTextPosition&, SearchShouldWrap = SearchShouldWrap::Yes) const;
char character_at(const GTextPosition&) const;
private:
explicit GTextDocument(Client* client);
NonnullOwnPtrVector<GTextDocumentLine> m_lines;
Vector<GTextDocumentSpan> m_spans;
HashTable<Client*> m_clients;
};
class GTextDocumentLine {
friend class GTextEditor;
friend class GTextDocument;
public:
explicit GTextDocumentLine(GTextDocument&);
explicit GTextDocumentLine(GTextDocument&, const StringView&);
StringView view() const { return { characters(), length() }; }
const char* characters() const { return m_text.data(); }
int length() const { return m_text.size() - 1; }
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&);
int first_non_whitespace_column() const;
private:
// NOTE: This vector is null terminated.
Vector<char> m_text;
};
|