Switch to OOP style code

This commit is contained in:
2026-02-04 00:38:11 +00:00
parent e3fc5323df
commit a62d4a18a8
50 changed files with 3011 additions and 3078 deletions

View File

@@ -1,25 +1,24 @@
#include "editor/editor.h"
void ensure_cursor(Editor *editor) {
std::shared_lock knot_lock(editor->knot_mtx);
if (editor->cursor < editor->scroll) {
editor->cursor.row = editor->scroll.row;
editor->cursor.col = editor->scroll.col;
editor->cursor_preffered = UINT32_MAX;
void Editor::ensure_cursor() {
if (this->cursor < this->scroll) {
this->cursor.row = this->scroll.row;
this->cursor.col = this->scroll.col;
this->cursor_preffered = UINT32_MAX;
return;
}
uint32_t numlen =
EXTRA_META + static_cast<int>(std::log10(editor->root->line_count + 1));
uint32_t render_width = editor->size.col - numlen;
EXTRA_META + static_cast<int>(std::log10(this->root->line_count + 1));
uint32_t render_width = this->size.col - numlen;
uint32_t visual_rows = 0;
uint32_t line_index = editor->scroll.row;
uint32_t line_index = this->scroll.row;
bool first_visual_line = true;
LineIterator *it = begin_l_iter(editor->root, line_index);
LineIterator *it = begin_l_iter(this->root, line_index);
if (!it)
return;
Coord last_visible = editor->scroll;
Coord last_visible = this->scroll;
while (true) {
if (visual_rows >= editor->size.row)
if (visual_rows >= this->size.row)
break;
uint32_t line_len;
char *line = next_line(it, &line_len);
@@ -27,13 +26,13 @@ void ensure_cursor(Editor *editor) {
break;
if (line_len > 0 && line[line_len - 1] == '\n')
line_len--;
uint32_t offset = first_visual_line ? editor->scroll.col : 0;
uint32_t offset = first_visual_line ? this->scroll.col : 0;
first_visual_line = false;
while (offset < line_len || (line_len == 0 && offset == 0)) {
Coord current = {line_index, offset};
last_visible = current;
visual_rows++;
if (visual_rows >= editor->size.row)
if (visual_rows >= this->size.row)
break;
uint32_t col = 0;
uint32_t advance = 0;
@@ -48,9 +47,9 @@ void ensure_cursor(Editor *editor) {
left -= g;
col += w;
}
if (line_index == editor->cursor.row) {
if (editor->cursor.col >= offset &&
editor->cursor.col <= offset + advance) {
if (line_index == this->cursor.row) {
if (this->cursor.col >= offset &&
this->cursor.col <= offset + advance) {
free(it->buffer);
free(it);
return;
@@ -64,20 +63,19 @@ void ensure_cursor(Editor *editor) {
}
line_index++;
}
editor->cursor.row = last_visible.row;
editor->cursor.col = last_visible.col;
editor->cursor_preffered = UINT32_MAX;
this->cursor.row = last_visible.row;
this->cursor.col = last_visible.col;
this->cursor_preffered = UINT32_MAX;
free(it->buffer);
free(it);
}
void ensure_scroll(Editor *editor) {
std::shared_lock knot_lock(editor->knot_mtx);
void Editor::ensure_scroll() {
uint32_t numlen =
EXTRA_META + static_cast<int>(std::log10(editor->root->line_count + 1));
uint32_t render_width = editor->size.col - numlen;
if (editor->cursor < editor->scroll) {
LineIterator *it = begin_l_iter(editor->root, editor->cursor.row);
EXTRA_META + static_cast<int>(std::log10(this->root->line_count + 1));
uint32_t render_width = this->size.col - numlen;
if (this->cursor < this->scroll) {
LineIterator *it = begin_l_iter(this->root, this->cursor.row);
if (!it)
return;
uint32_t len;
@@ -98,9 +96,9 @@ void ensure_scroll(Editor *editor) {
int width = display_width(line + offset, inc);
if (cols + width > render_width) {
cols = 0;
if (editor->cursor.col > old_offset && editor->cursor.col <= offset) {
editor->scroll.row = editor->cursor.row;
editor->scroll.col = old_offset;
if (this->cursor.col > old_offset && this->cursor.col <= offset) {
this->scroll.row = this->cursor.row;
this->scroll.col = old_offset;
free(it->buffer);
free(it);
return;
@@ -112,14 +110,14 @@ void ensure_scroll(Editor *editor) {
}
free(it->buffer);
free(it);
editor->scroll.row = editor->cursor.row;
editor->scroll.col = (editor->cursor.col == 0) ? 0 : old_offset;
} else if (editor->cursor.row - editor->scroll.row < editor->size.row * 2) {
uint32_t line_index = editor->scroll.row;
LineIterator *it = begin_l_iter(editor->root, line_index);
this->scroll.row = this->cursor.row;
this->scroll.col = (this->cursor.col == 0) ? 0 : old_offset;
} else if (this->cursor.row - this->scroll.row < this->size.row * 2) {
uint32_t line_index = this->scroll.row;
LineIterator *it = begin_l_iter(this->root, line_index);
if (!it)
return;
uint32_t max_visual_lines = editor->size.row;
uint32_t max_visual_lines = this->size.row;
Coord *scroll_queue = (Coord *)malloc(sizeof(Coord) * max_visual_lines);
uint32_t q_head = 0;
uint32_t q_size = 0;
@@ -133,7 +131,7 @@ void ensure_scroll(Editor *editor) {
line_len--;
uint32_t current_byte_offset = 0;
if (first_visual_line) {
current_byte_offset += editor->scroll.col;
current_byte_offset += this->scroll.col;
first_visual_line = false;
}
while (current_byte_offset < line_len ||
@@ -160,16 +158,16 @@ void ensure_scroll(Editor *editor) {
line_left -= cluster_len;
col += width;
}
if (line_index == editor->cursor.row) {
if (line_index == this->cursor.row) {
bool cursor_found = false;
if (editor->cursor.col >= current_byte_offset &&
editor->cursor.col < current_byte_offset + local_render_offset)
if (this->cursor.col >= current_byte_offset &&
this->cursor.col < current_byte_offset + local_render_offset)
cursor_found = true;
else if (editor->cursor.col == line_len &&
else if (this->cursor.col == line_len &&
current_byte_offset + local_render_offset == line_len)
cursor_found = true;
if (cursor_found) {
editor->scroll = scroll_queue[q_head];
this->scroll = scroll_queue[q_head];
free(scroll_queue);
free(it->buffer);
free(it);
@@ -186,10 +184,10 @@ void ensure_scroll(Editor *editor) {
free(it->buffer);
free(it);
} else {
editor->scroll.row = (editor->cursor.row > editor->size.row * 1.5)
? editor->cursor.row - editor->size.row * 1.5
: 0;
editor->scroll.col = 0;
ensure_scroll(editor);
this->scroll.row = (this->cursor.row > this->size.row * 1.5)
? this->cursor.row - this->size.row * 1.5
: 0;
this->scroll.col = 0;
this->ensure_scroll();
}
}