forked from google/ced
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheditor.h
217 lines (206 loc) · 6.98 KB
/
editor.h
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
// Copyright 2017 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#pragma once
#include <numeric>
#include <string>
#include "absl/strings/str_join.h"
#include "buffer.h"
#include "log.h"
#include "render.h"
#include "theme.h"
struct AppEnv {
std::string clipboard;
};
class Editor : public std::enable_shared_from_this<Editor> {
public:
static std::shared_ptr<Editor> Make(Site* site) {
return std::shared_ptr<Editor>(new Editor(site));
}
private:
Editor(Site* site) : site_(site), ed_(site) {}
public:
// state management
void UpdateState(LogTimer* tmr, const EditNotification& state);
const EditNotification& CurrentState() { return state_; }
bool HasCommands() {
return state_.shutdown || !unpublished_commands_.commands().empty() ||
cursor_reported_ != cursor_;
}
EditResponse MakeResponse();
// editor commands
void SelectLeft();
void MoveLeft();
void SelectRight();
void MoveRight();
void MoveStartOfLine();
void MoveEndOfLine();
void MoveDownN(int n);
void MoveUpN(int n);
void MoveDown() { MoveDownN(1); }
void MoveUp() { MoveUpN(1); }
void SelectDownN(int n);
void SelectUpN(int n);
void SelectDown() { SelectDownN(1); }
void SelectUp() { SelectUpN(1); }
void Backspace();
void Copy(AppEnv* env);
void Cut(AppEnv* env);
void Paste(AppEnv* env);
void InsNewLine() { InsChar('\n'); }
void InsChar(char c);
template <class RC>
std::function<void(RC* ctx)> PrepareRender(bool has_focus) {
auto content = state_.content;
std::shared_ptr<Editor> self = shared_from_this();
return [self, content, has_focus](RC* ctx) {
if (self->cursor_row_ < 0) {
self->cursor_row_ = 0;
} else if (self->cursor_row_ >= ctx->window->height()) {
self->cursor_row_ = ctx->window->height() - 1;
}
self->cursor_ = AnnotatedString::Iterator(content, self->cursor_).id();
AnnotatedString::LineIterator line_cr(content, self->cursor_);
AnnotatedString::LineIterator line_bk = line_cr;
AnnotatedString::LineIterator line_fw = line_cr;
for (int i = 0; i < ctx->window->height(); i++) {
line_bk.MovePrev();
line_fw.MoveNext();
}
AnnotatedString::AllIterator it = line_bk.AsAllIterator();
int nrow = 0;
int ncol = 0;
uint32_t base_flags = 0;
AnnotatedString::AllIterator start_of_line = it;
std::vector<std::string> gutter_annotations;
auto add_gutter = [&gutter_annotations](const std::string& s) {
for (const auto& g : gutter_annotations) {
if (g == s) return;
}
gutter_annotations.push_back(s);
};
while (it.id() != line_fw.id()) {
if (it.is_visible()) {
uint32_t chr_flags = base_flags;
std::vector<std::string> tags;
bool move_cursor = false;
bool has_diagnostic = false;
it.ForEachAttrValue([&](const Attribute& attr) {
switch (attr.data_case()) {
case Attribute::kCursor:
Log() << "found cursor " << nrow << " " << ncol;
move_cursor = true;
break;
case Attribute::kSelection:
chr_flags |= Theme::SELECTED;
break;
case Attribute::kDiagnostic:
has_diagnostic = true;
break;
case Attribute::kTags:
for (auto t : attr.tags().tags()) {
tags.push_back(t);
}
break;
case Attribute::kSize:
switch (attr.size().type()) {
case SizeAnnotation::OFFSET_INTO_PARENT:
if (attr.size().bits()) {
add_gutter(absl::StrCat("@", attr.size().size(), ".",
attr.size().bits()));
} else {
add_gutter(absl::StrCat("@", attr.size().size()));
}
break;
case SizeAnnotation::SIZEOF_SELF:
if (attr.size().bits()) {
add_gutter(absl::StrCat(
attr.size().size() * 8 + attr.size().bits(), "b"));
} else {
add_gutter(absl::StrCat(attr.size().size(), "B"));
}
break;
default:
break;
}
break;
default:
break;
}
});
if (has_diagnostic) {
tags.push_back("invalid");
}
if (it.value() == '\n') {
auto fill_attr = ctx->color->Theme(::Theme::Tag(), base_flags);
while (ncol < ctx->window->width()) {
ctx->Put(nrow, ncol, ' ', fill_attr);
ncol++;
}
std::string gutter = absl::StrJoin(gutter_annotations, ",");
ncol = ctx->window->width() - gutter.length();
fill_attr =
ctx->color->Theme(::Theme::Tag{"comment.gutter"}, base_flags);
for (auto c : gutter) {
ctx->Put(nrow, ncol, c, fill_attr);
ncol++;
}
gutter_annotations.clear();
nrow++;
ncol = 0;
start_of_line = it;
base_flags = 0;
} else {
ctx->Put(nrow, ncol, it.value(),
ctx->color->Theme(tags, chr_flags));
ncol++;
}
if (move_cursor) {
if (has_focus) ctx->Move(nrow, ncol);
if ((base_flags & Theme::HIGHLIGHT_LINE) == 0) {
ncol = 0;
it = start_of_line;
base_flags |= Theme::HIGHLIGHT_LINE;
}
}
}
it.MoveNext();
}
};
}
private:
void CursorLeft();
void CursorRight();
void CursorDown();
void CursorUp();
void CursorStartOfLine();
void CursorEndOfLine();
void PublishCursor();
void SetSelectMode(bool sel);
bool SelectMode() const { return selection_anchor_ != ID(); }
void DeleteSelection();
Site* const site_;
int cursor_row_ = 0; // cursor row as an offset into the view buffer
ID cursor_ = AnnotatedString::Begin();
ID cursor_reported_ = AnnotatedString::End();
ID selection_anchor_ = ID();
EditNotification state_;
CommandSet unpublished_commands_;
CommandSet unacknowledged_commands_;
AnnotationEditor ed_;
struct BufferInfo {
std::unique_ptr<Buffer> buffer;
AnnotationEditor ed;
};
std::map<ID, BufferInfo> buffers_;
};