Minor fixes

This commit is contained in:
2025-12-09 23:18:06 +00:00
parent cacda354c2
commit 9900b83871
7 changed files with 84 additions and 49 deletions

View File

@@ -40,19 +40,20 @@ Editor *new_editor(const char *filename, Coord position, Coord size) {
editor->size = size;
editor->tree = nullptr;
editor->cursor = {0, 0};
editor->cursor_preffered = UINT32_MAX;
editor->selection_active = false;
editor->selection = {0, 0};
editor->scroll = {0, 0};
editor->root = load(str, len, optimal_chunk_size(len));
free(str);
editor->folded.resize(editor->root->line_count + 2);
std::string query = get_exe_dir() + "/../grammar/ruby.scm";
if (!(len > (1024 * 1024))) {
if (len < (1024 * 64)) {
editor->parser = ts_parser_new();
editor->language = tree_sitter_ruby();
ts_parser_set_language(editor->parser, editor->language);
editor->query = load_query(query.c_str(), editor);
}
free(str);
return editor;
}
@@ -183,8 +184,10 @@ void cursor_down(Editor *editor, uint32_t number) {
char *line_content = next_line(it);
if (line_content == nullptr)
return;
uint32_t visual_col =
get_visual_col_from_bytes(line_content, editor->cursor.col);
if (editor->cursor_preffered == UINT32_MAX)
editor->cursor_preffered =
get_visual_col_from_bytes(line_content, editor->cursor.col);
uint32_t visual_col = editor->cursor_preffered;
do {
free(line_content);
line_content = next_line(it);
@@ -212,8 +215,10 @@ void cursor_up(Editor *editor, uint32_t number) {
free(it);
return;
}
uint32_t visual_col =
get_visual_col_from_bytes(line_content, editor->cursor.col);
if (editor->cursor_preffered == UINT32_MAX)
editor->cursor_preffered =
get_visual_col_from_bytes(line_content, editor->cursor.col);
uint32_t visual_col = editor->cursor_preffered;
free(line_content);
while (number > 0 && editor->cursor.row > 0) {
editor->cursor.row--;
@@ -275,6 +280,19 @@ void cursor_right(Editor *editor, uint32_t number) {
}
number--;
}
LineIterator *it2 = begin_l_iter(editor->root, editor->cursor.row);
char *cur_line = next_line(it2);
free(it2);
if (cur_line) {
uint32_t len2 = strlen(cur_line);
if (len2 > 0 && cur_line[len2 - 1] == '\n')
cur_line[--len2] = '\0';
editor->cursor_preffered =
get_visual_col_from_bytes(cur_line, editor->cursor.col);
free(cur_line);
} else {
editor->cursor_preffered = UINT32_MAX;
}
if (line)
free(line);
}
@@ -328,6 +346,19 @@ void cursor_left(Editor *editor, uint32_t number) {
}
number--;
}
LineIterator *it2 = begin_l_iter(editor->root, editor->cursor.row);
char *cur_line = next_line(it2);
free(it2);
if (cur_line) {
uint32_t len2 = strlen(cur_line);
if (len2 > 0 && cur_line[len2 - 1] == '\n')
cur_line[--len2] = '\0';
editor->cursor_preffered =
get_visual_col_from_bytes(cur_line, editor->cursor.col);
free(cur_line);
} else {
editor->cursor_preffered = UINT32_MAX;
}
if (line)
free(line);
}
@@ -376,6 +407,29 @@ 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 screen_rows = editor->size.row;
uint32_t screen_cols = editor->size.col;

View File

@@ -75,10 +75,10 @@ void handle_editor_event(Editor *editor, KeyEvent event) {
};
editor->edit_queue.push(edit);
}
editor->spans.apply(pos, 1);
cursor_right(editor, 1);
apply_edit(editor->spans.spans, pos, 1);
if (editor->spans.mid_parse)
editor->spans.edits.push({pos, 1});
cursor_right(editor, 1);
}
if (event.key_type == KEY_CHAR && event.c == '\t') {
std::shared_lock lock_1(editor->knot_mtx);
@@ -99,10 +99,11 @@ void handle_editor_event(Editor *editor, KeyEvent event) {
};
editor->edit_queue.push(edit);
}
editor->spans.apply(pos, 2);
cursor_right(editor, 2);
std::unique_lock lock_3(editor->spans.mtx);
apply_edit(editor->spans.spans, pos, 2);
if (editor->spans.mid_parse)
editor->spans.edits.push({pos, 2});
cursor_right(editor, 2);
}
if (event.key_type == KEY_CHAR && (event.c == '\n' || event.c == '\r')) {
std::shared_lock lock_1(editor->knot_mtx);
@@ -120,14 +121,15 @@ void handle_editor_event(Editor *editor, KeyEvent event) {
.new_end_byte = pos + 1,
.start_point = {editor->cursor.row, editor->cursor.col},
.old_end_point = {editor->cursor.row, editor->cursor.col},
.new_end_point = {editor->cursor.row, editor->cursor.col + 1},
.new_end_point = {editor->cursor.row + 1, 0},
};
editor->edit_queue.push(edit);
}
editor->spans.apply(pos + 1, 1);
cursor_right(editor, 1);
std::unique_lock lock_3(editor->spans.mtx);
apply_edit(editor->spans.spans, pos + 1, 1);
if (editor->spans.mid_parse)
editor->spans.edits.push({pos + 1, 1});
cursor_right(editor, 1);
}
if (event.key_type == KEY_CHAR && event.c == 0x7F) {
std::shared_lock lock_1(editor->knot_mtx);
@@ -152,7 +154,8 @@ void handle_editor_event(Editor *editor, KeyEvent event) {
};
editor->edit_queue.push(edit);
}
editor->spans.apply(start, start - pos);
std::unique_lock lock_3(editor->spans.mtx);
apply_edit(editor->spans.spans, start, start - pos);
if (editor->spans.mid_parse)
editor->spans.edits.push({start, start - pos});
}

View File

@@ -94,7 +94,7 @@ static inline bool ts_predicate(TSQuery *query, const TSQueryMatch &match,
ts_query_predicates_for_pattern(query, match.pattern_index, &step_count);
if (!steps || step_count != 4)
return true;
if (source->char_count >= (64 * 1024))
if (source->char_count >= (16 * 1024))
return false;
std::string command;
std::string regex_txt;
@@ -233,11 +233,11 @@ void ts_collect_spans(Editor *editor) {
if (!running)
return;
std::sort(new_spans.begin(), new_spans.end());
std::pair<uint32_t, int64_t> span_edit;
while (editor->spans.edits.pop(span_edit))
apply_edit(new_spans, span_edit.first, span_edit.second);
std::unique_lock span_mtx(editor->spans.mtx);
editor->spans.mid_parse = false;
editor->spans.spans.swap(new_spans);
span_mtx.unlock();
std::pair<uint32_t, int64_t> span_edit;
while (editor->spans.edits.pop(span_edit))
editor->spans.apply(span_edit.first, span_edit.second);
}