Add scrolling support and other minor fixes
This commit is contained in:
165
src/editor.cc
165
src/editor.cc
@@ -48,148 +48,6 @@ void free_editor(Editor *editor) {
|
||||
delete editor;
|
||||
}
|
||||
|
||||
void ensure_scroll(Editor *editor) {
|
||||
std::shared_lock knot_lock(editor->knot_mtx);
|
||||
if (editor->cursor.row < editor->scroll.row ||
|
||||
(editor->cursor.row == editor->scroll.row &&
|
||||
editor->cursor.col <= editor->scroll.col)) {
|
||||
LineIterator *it = begin_l_iter(editor->root, editor->cursor.row);
|
||||
if (!it)
|
||||
return;
|
||||
uint32_t len;
|
||||
char *line = next_line(it, &len);
|
||||
if (!line) {
|
||||
free(it);
|
||||
return;
|
||||
}
|
||||
if (len > 0 && line[len - 1] == '\n')
|
||||
--len;
|
||||
uint32_t rows = 1;
|
||||
uint32_t cols = 0;
|
||||
uint32_t offset = 0;
|
||||
uint32_t old_offset = 0;
|
||||
while (offset < len) {
|
||||
uint32_t inc =
|
||||
grapheme_next_character_break_utf8(line + offset, len - offset);
|
||||
int width = display_width(line + offset, inc);
|
||||
if (cols + width > editor->size.col) {
|
||||
rows++;
|
||||
cols = 0;
|
||||
if (editor->cursor.col > old_offset && editor->cursor.col <= offset) {
|
||||
editor->scroll.row = editor->cursor.row;
|
||||
editor->scroll.col = old_offset;
|
||||
free(line);
|
||||
free(it);
|
||||
return;
|
||||
}
|
||||
old_offset = offset;
|
||||
}
|
||||
cols += width;
|
||||
offset += inc;
|
||||
}
|
||||
free(line);
|
||||
free(it);
|
||||
editor->scroll.row = editor->cursor.row;
|
||||
editor->scroll.col = (editor->cursor.col == 0) ? 0 : old_offset;
|
||||
} else {
|
||||
uint32_t line_index = editor->scroll.row;
|
||||
LineIterator *it = begin_l_iter(editor->root, line_index);
|
||||
if (!it)
|
||||
return;
|
||||
uint32_t max_visual_lines = editor->size.row;
|
||||
Coord *scroll_queue = (Coord *)malloc(sizeof(Coord) * max_visual_lines);
|
||||
uint32_t q_head = 0;
|
||||
uint32_t q_size = 0;
|
||||
bool first_visual_line = true;
|
||||
while (true) {
|
||||
if (editor->folded[line_index]) {
|
||||
if (editor->folded[line_index] == 2) {
|
||||
Coord fold_coord = {line_index, 0};
|
||||
if (q_size < max_visual_lines) {
|
||||
scroll_queue[(q_head + q_size) % max_visual_lines] = fold_coord;
|
||||
q_size++;
|
||||
} else {
|
||||
scroll_queue[q_head] = fold_coord;
|
||||
q_head = (q_head + 1) % max_visual_lines;
|
||||
}
|
||||
if (line_index == editor->cursor.row) {
|
||||
editor->scroll = scroll_queue[q_head];
|
||||
break;
|
||||
}
|
||||
}
|
||||
do {
|
||||
char *line = next_line(it, nullptr);
|
||||
if (!line)
|
||||
break;
|
||||
free(line);
|
||||
line_index++;
|
||||
} while (line_index < editor->size.row &&
|
||||
editor->folded[line_index] == 1);
|
||||
continue;
|
||||
}
|
||||
uint32_t line_len;
|
||||
char *line = next_line(it, &line_len);
|
||||
if (!line)
|
||||
break;
|
||||
if (line_len > 0 && line[line_len - 1] == '\n')
|
||||
line_len--;
|
||||
uint32_t current_byte_offset = 0;
|
||||
if (first_visual_line) {
|
||||
current_byte_offset += editor->scroll.col;
|
||||
first_visual_line = false;
|
||||
}
|
||||
while (current_byte_offset < line_len ||
|
||||
(line_len == 0 && current_byte_offset == 0)) {
|
||||
Coord current_coord = {line_index, current_byte_offset};
|
||||
if (q_size < max_visual_lines) {
|
||||
scroll_queue[(q_head + q_size) % max_visual_lines] = current_coord;
|
||||
q_size++;
|
||||
} else {
|
||||
scroll_queue[q_head] = current_coord;
|
||||
q_head = (q_head + 1) % max_visual_lines;
|
||||
}
|
||||
uint32_t col = 0;
|
||||
uint32_t local_render_offset = 0;
|
||||
uint32_t line_left = line_len - current_byte_offset;
|
||||
while (line_left > 0 && col < editor->size.col) {
|
||||
uint32_t cluster_len = grapheme_next_character_break_utf8(
|
||||
line + current_byte_offset + local_render_offset, line_left);
|
||||
int width = display_width(
|
||||
line + current_byte_offset + local_render_offset, cluster_len);
|
||||
if (col + width > editor->size.col)
|
||||
break;
|
||||
local_render_offset += cluster_len;
|
||||
line_left -= cluster_len;
|
||||
col += width;
|
||||
}
|
||||
if (line_index == editor->cursor.row) {
|
||||
bool cursor_found = false;
|
||||
if (editor->cursor.col >= current_byte_offset &&
|
||||
editor->cursor.col < current_byte_offset + local_render_offset)
|
||||
cursor_found = true;
|
||||
else if (editor->cursor.col == line_len &&
|
||||
current_byte_offset + local_render_offset == line_len)
|
||||
cursor_found = true;
|
||||
if (cursor_found) {
|
||||
editor->scroll = scroll_queue[q_head];
|
||||
free(line);
|
||||
free(scroll_queue);
|
||||
free(it);
|
||||
return;
|
||||
}
|
||||
}
|
||||
current_byte_offset += local_render_offset;
|
||||
if (line_len == 0)
|
||||
break;
|
||||
}
|
||||
line_index++;
|
||||
free(line);
|
||||
}
|
||||
free(scroll_queue);
|
||||
free(it);
|
||||
}
|
||||
}
|
||||
|
||||
void fold(Editor *editor, uint32_t start_line, uint32_t end_line) {
|
||||
if (!editor)
|
||||
return;
|
||||
@@ -208,29 +66,6 @@ void update_render_fold_marker(uint32_t row, uint32_t cols) {
|
||||
update(row, i, " ", 0xc6c6c6, 0, 0);
|
||||
}
|
||||
|
||||
void apply_edit(std::vector<Span> &spans, uint32_t x, int64_t y) {
|
||||
Span key{.start = x, .end = 0, .hl = nullptr};
|
||||
auto it = std::lower_bound(
|
||||
spans.begin(), spans.end(), key,
|
||||
[](const Span &a, const Span &b) { return a.start < b.start; });
|
||||
size_t idx = std::distance(spans.begin(), it);
|
||||
while (idx > 0 && spans.at(idx - 1).end > x)
|
||||
--idx;
|
||||
for (size_t i = idx; i < spans.size();) {
|
||||
Span &s = spans.at(i);
|
||||
if (s.start < x && s.end > x) {
|
||||
s.end += y;
|
||||
} else if (s.start > x) {
|
||||
s.start += y;
|
||||
s.end += y;
|
||||
}
|
||||
if (s.end <= s.start)
|
||||
spans.erase(spans.begin() + i);
|
||||
else
|
||||
++i;
|
||||
}
|
||||
}
|
||||
|
||||
void render_editor(Editor *editor) {
|
||||
uint32_t sel_start = 0, sel_end = 0;
|
||||
if (editor->selection_active) {
|
||||
|
||||
Reference in New Issue
Block a user