forked from mika314/texteditor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
base_text_buffer.cpp
263 lines (233 loc) · 5.67 KB
/
base_text_buffer.cpp
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
#include "base_text_buffer.hpp"
#include "screen.hpp"
BaseTextBuffer::BaseTextBuffer():
isReadOnly_{false},
cursor_{0, 0},
highlighter_{nullptr}
{}
BaseTextBuffer::~BaseTextBuffer()
{
delete highlighter_;
}
const std::wstring &BaseTextBuffer::operator[](int line) const
{
return buffer_[line];
}
std::wstring &BaseTextBuffer::operator[](int line)
{
return buffer_[line];
}
int BaseTextBuffer::size() const
{
return buffer_.size();
}
void BaseTextBuffer::undo(Coord &cursor)
{
undoStack_.undo(cursor);
}
void BaseTextBuffer::redo(Coord &cursor)
{
undoStack_.redo(cursor);
}
void BaseTextBuffer::render(Screen *screen) const
{
if (highlighter_)
highlighter_->update({0, screen->vScroll()}, {0, screen->vScroll() + screen->heightCh()});
for (int y = 0; y < screen->heightCh(); ++y)
{
const auto yy = y + screen->vScroll();
const auto &line = yy < size() ? operator[](yy) : L"";
for (int x = 0; x < screen->widthCh(); ++x)
{
const auto xx = x + screen->hScroll();
Color fgColor = Black;
Color bgColor = White;
if (highlighter_)
{
fgColor = highlighter_->fgColor(xx, yy);
bgColor = highlighter_->bgColor(xx, yy);
}
if (!screen->isSelected({ xx, yy }))
screen->ch(x, y) = { xx < static_cast<int>(line.size()) ? line[xx] : L'\0', fgColor, bgColor };
else
screen->ch(x, y) = { xx < static_cast<int>(line.size()) ? line[xx] : L'\0', Black, Gray90 };
}
}
screen->update();
}
std::wstring BaseTextBuffer::preInsert(Coord &cursor, const std::wstring &value)
{
return value;
}
void BaseTextBuffer::insert(Coord &cursor, const std::wstring &refValue)
{
auto value = preInsert(cursor, refValue);
if (!value.empty())
undoStack_.push(cursor,
[value, this](Coord &c) -> int
{
internalInsert(c, value);
return value.size();
},
[this](Coord &c, int size)
{
internalDelete(c, size);
});
postInsert(cursor, value);
}
void BaseTextBuffer::postInsert(Coord &cursor, const std::wstring &value)
{
}
void BaseTextBuffer::internalInsert(Coord &cursor, const std::wstring &value)
{
if (isReadOnly())
return;
for (wchar_t c: value)
{
auto &line = buffer_[cursor.y];
if (c != L'\n')
{
line.insert(begin(line) + cursor.x, c);
++cursor.x;
}
else
{
std::wstring tmp { begin(line) + cursor.x, end(line) };
line.erase(begin(line) + cursor.x, end(line));
buffer_.insert(begin(buffer_) + cursor.y + 1, tmp);
++cursor.y;
cursor.x = 0;
}
}
}
int BaseTextBuffer::preDel(Coord &, int value)
{
return value;
}
void BaseTextBuffer::del(Coord &cursor, int value)
{
value = preDel(cursor, value);
if (value > 0)
undoStack_.push(cursor,
[value, this](Coord &c) -> std::wstring
{
return internalDelete(c, value);
},
[this](Coord &c, const std::wstring &str)
{
Coord tmp = c;
internalInsert(c, str);
c = tmp;
});
postDel(cursor, value);
}
void BaseTextBuffer::postDel(Coord &, int)
{
}
std::wstring BaseTextBuffer::internalDelete(const Coord cursor, int value)
{
if (isReadOnly())
return L"";
std::wstring result;
for (int i = 0; i < value; ++i)
{
auto &line = buffer_[cursor.y];
if (begin(line) + cursor.x != end(line))
{
result += line[cursor.x];
line.erase(begin(line) + cursor.x);
}
else
{
if (cursor.y < static_cast<int>(buffer_.size()) - 1)
{
result += L"\n";
auto tmp = buffer_[cursor.y + 1];
buffer_.erase(begin(buffer_) + cursor.y + 1);
buffer_[cursor.y] += tmp;
}
}
}
return result;
}
int BaseTextBuffer::preBackspace(Coord &, int value)
{
return value;
}
void BaseTextBuffer::backspace(Coord &cursor, int value)
{
value = preBackspace(cursor, value);
if (value > 0)
undoStack_.push(cursor,
[value, this](Coord &c) -> std::pair<std::wstring, Coord>
{
auto result = internalBackspace(c, value);
return std::make_pair(result, c);
},
[this](Coord &c, const std::pair<std::wstring, Coord> &s)
{
Coord ccc = s.second;
internalInsert(ccc, s.first);
c = ccc;
});
postBackspace(cursor, value);
}
void BaseTextBuffer::postBackspace(Coord &, int)
{
}
std::wstring BaseTextBuffer::internalBackspace(Coord &cursor, int value)
{
if (isReadOnly())
return L"";
std::wstring result;
int count = 0;
for (int i = 0; i < value; ++i)
{
if (cursor.x > 0)
--cursor.x;
else
{
if (cursor.y > 0)
{
--cursor.y;
cursor.x = buffer_[cursor.y].size();
}
else
break;
}
++count;
}
return internalDelete(cursor, count);
}
bool BaseTextBuffer::isReadOnly() const
{
return isReadOnly_;
}
void BaseTextBuffer::setReadOnly(bool value)
{
isReadOnly_ = value;
}
std::wstring BaseTextBuffer::name() const
{
return name_;
}
void BaseTextBuffer::setName(const std::wstring &value)
{
name_ = value;
}
Coord BaseTextBuffer::cursor() const
{
return cursor_;
}
void BaseTextBuffer::setCursor(Coord value)
{
cursor_ = value;
}
bool BaseTextBuffer::isModified() const
{
return undoStack_.isModified();
}
void BaseTextBuffer::clearModified()
{
undoStack_.clearModified();
}