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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
|
#ifndef display_buffer_hh_INCLUDED
#define display_buffer_hh_INCLUDED
#include <vector>
#include "string.hh"
#include "color.hh"
#include "line_and_column.hh"
#include "buffer.hh"
#include "utf8.hh"
namespace Kakoune
{
struct DisplayCoord : LineAndColumn<DisplayCoord, LineCount, CharCount>
{
constexpr DisplayCoord(LineCount line = 0, CharCount column = 0)
: LineAndColumn(line, column) {}
};
typedef char Attribute;
enum Attributes
{
Normal = 0,
Underline = 1,
Reverse = 2,
Blink = 4,
Bold = 8
};
class DisplayLine;
struct AtomContent
{
public:
enum Type { BufferRange, ReplacedBufferRange, Text };
AtomContent(BufferIterator begin, BufferIterator end)
: m_type(BufferRange),
m_begin(std::move(begin)),
m_end(std::move(end)) {}
AtomContent(String str)
: m_type(Text), m_text(std::move(str)) {}
String content() const
{
switch (m_type)
{
case BufferRange:
return m_begin.buffer().string(m_begin, m_end);
case Text:
case ReplacedBufferRange:
return m_text;
}
assert(false);
return 0;
}
CharCount length() const
{
switch (m_type)
{
case BufferRange:
return utf8::distance(m_begin, m_end);
case Text:
case ReplacedBufferRange:
return m_text.char_length();
}
assert(false);
return 0;
}
const BufferIterator& begin() const
{
assert(has_buffer_range());
return m_begin;
}
const BufferIterator& end() const
{
assert(has_buffer_range());
return m_end;
}
void replace(String text)
{
assert(m_type == BufferRange);
m_type = ReplacedBufferRange;
m_text = std::move(text);
}
bool has_buffer_range() const
{
return m_type == BufferRange or m_type == ReplacedBufferRange;
}
Type type() const { return m_type; }
private:
friend class DisplayLine;
Type m_type;
BufferIterator m_begin;
BufferIterator m_end;
String m_text;
};
struct DisplayAtom
{
Color fg_color;
Color bg_color;
Attribute attribute;
AtomContent content;
DisplayAtom(AtomContent content)
: content(std::move(content)), attribute(Normal),
fg_color(Color::Default), bg_color(Color::Default) {}
};
class DisplayLine
{
public:
using AtomList = std::vector<DisplayAtom>;
using iterator = AtomList::iterator;
using const_iterator = AtomList::const_iterator;
explicit DisplayLine(LineCount buffer_line) : m_buffer_line(buffer_line) {}
DisplayLine(LineCount buffer_line, AtomList atoms)
: m_buffer_line(buffer_line), m_atoms(std::move(atoms)) {}
LineCount buffer_line() const { return m_buffer_line; }
iterator begin() { return m_atoms.begin(); }
iterator end() { return m_atoms.end(); }
const_iterator begin() const { return m_atoms.begin(); }
const_iterator end() const { return m_atoms.end(); }
const AtomList& atoms() const { return m_atoms; }
// Split atom pointed by it at pos, returns an iterator to the first atom
iterator split(iterator it, BufferIterator pos);
iterator insert(iterator it, DisplayAtom atom) { return m_atoms.insert(it, std::move(atom)); }
void push_back(DisplayAtom atom) { m_atoms.push_back(std::move(atom)); }
void optimize();
private:
LineCount m_buffer_line;
AtomList m_atoms;
};
using BufferRange = std::pair<BufferIterator, BufferIterator>;
class DisplayBuffer
{
public:
using LineList = std::vector<DisplayLine>;
DisplayBuffer() {}
LineList& lines() { return m_lines; }
const LineList& lines() const { return m_lines; }
// returns the smallest BufferIterator range which contains every DisplayAtoms
const BufferRange& range() const { return m_range; }
void optimize();
void compute_range();
private:
LineList m_lines;
BufferRange m_range;
};
}
#endif // display_buffer_hh_INCLUDED
|