Compare commits

...

3 Commits

Author SHA1 Message Date
fd70a40a49 Update preffered cursor column for edits properly 2025-12-12 20:47:28 +00:00
39542bac33 Undo editor size to fullscreen 2025-12-12 20:31:00 +00:00
ce74e75f04 Major bug fixes 2025-12-12 20:29:34 +00:00
4 changed files with 271 additions and 76 deletions

View File

@@ -6,25 +6,23 @@ A TUI IDE.
# TODO # TODO
- [ ] Fix both scrolling functions / they are absolutely messed up. - [ ] Make function to get selected text. (selection itself is done)
- [ ] Change `next_line` to set `out_len` and also re-add `line` to the iterator.
- [ ] Add `hooks` in files that can be set/unset/jumped to.
- [ ] Add support for text selection (slect range / all).
- [ ] Add support for delete key.
- [ ] Add support for ctrl + arrow key.
- [ ] Add support for ctrl + backspace.
- [ ] Add underline highlight for current word and all occurences.
- [ ] Add bg highlight for current line.
- [ ] Add line numbers.
- [ ] Add support for copy/cut/paste.
- [ ] Add mouse support.
- [ ] Add modes for editing - insert, select, normal, etc. - [ ] Add modes for editing - insert, select, normal, etc.
- [ ] Add folding support. - [ ] Add line numbers.
- [ ] Add bg highlight for current line.
- [ ] Add support for copy/cut/paste.
- [ ] Add support for ctrl + arrow key for moving words.
- [ ] Add support for ctrl + backspace / delete for deleting words.
- [ ] Add underline highlight for current word and all occurences.
- [ ] Add mouse support.
- [ ] Add `hooks` in files that can be set/unset/jumped to.
- [ ] Add folding support at tree-sitter level (basic folding is done).
- [ ] Add feature where doing enter uses tree-sitter to add newline with indentation. - [ ] Add feature where doing enter uses tree-sitter to add newline with indentation.
1. it should also put stuff like `}` on the next line. - it should also put stuff like `}` on the next line.
- [ ] Add support for brackets/quotes to auto-close. - [ ] Add support for brackets/quotes to auto-close.
- [ ] Add support for LSP.
- [ ] Add support for virtual cursor where edits apply at all the places. - [ ] Add support for virtual cursor where edits apply at all the places.
- [ ] Add search / replace along with search / virtual cursors - [ ] Add search / replace along with search / virtual cursors are searched pos.
- [ ] Add scm files for all the supported languages. (2/14) Done. - [ ] Add support for undo/redo.
- [ ] Add `.scm` files for all the supported languages. (2/14) Done.
- [ ] Add support for LSP & autocomplete / snippets.
- [ ] Add codeium/copilot support. - [ ] Add codeium/copilot support.

View File

@@ -113,7 +113,7 @@ void cursor_left(Editor *editor, uint32_t number);
void cursor_right(Editor *editor, uint32_t number); void cursor_right(Editor *editor, uint32_t number);
void ensure_scroll(Editor *editor); void ensure_scroll(Editor *editor);
void apply_edit(std::vector<Span> &spans, uint32_t x, int64_t y); void apply_edit(std::vector<Span> &spans, uint32_t x, int64_t y);
void edit_erase(Editor *editor, uint32_t pos, int64_t len); void edit_erase(Editor *editor, Coord pos, int64_t len);
void edit_insert(Editor *editor, uint32_t pos, char *data, uint32_t len); void edit_insert(Editor *editor, Coord pos, char *data, uint32_t len);
#endif #endif

View File

@@ -28,7 +28,7 @@ Editor *new_editor(const char *filename, Coord position, Coord size) {
editor->root = load(str, len, optimal_chunk_size(len)); editor->root = load(str, len, optimal_chunk_size(len));
free(str); free(str);
editor->folded.resize(editor->root->line_count + 2); editor->folded.resize(editor->root->line_count + 2);
if (len < (1024 * 64)) { if (len < (1024 * 128)) {
editor->parser = ts_parser_new(); editor->parser = ts_parser_new();
Language language = language_for_file(filename); Language language = language_for_file(filename);
editor->language = language.fn(); editor->language = language.fn();
@@ -234,6 +234,111 @@ void cursor_left(Editor *editor, uint32_t number) {
free(line); free(line);
} }
Coord simulate_cursor_right(Editor *editor, Coord cursor, uint32_t number) {
Coord result = cursor;
if (!editor || !editor->root || number == 0)
return result;
uint32_t row = result.row;
uint32_t col = result.col;
uint32_t line_len = 0;
LineIterator *it = begin_l_iter(editor->root, row);
char *line = next_line(it, &line_len);
free(it);
if (!line)
return result;
if (line_len > 0 && line[line_len - 1] == '\n')
--line_len;
while (number > 0) {
if (col >= line_len) {
free(line);
line = nullptr;
uint32_t next_row = row + 1;
while (next_row < editor->root->line_count &&
editor->folded[next_row] != 0)
next_row++;
if (next_row >= editor->root->line_count) {
col = line_len;
break;
}
row = next_row;
col = 0;
it = begin_l_iter(editor->root, row);
line = next_line(it, &line_len);
free(it);
if (!line)
break;
if (line_len > 0 && line[line_len - 1] == '\n')
--line_len;
} else {
uint32_t inc =
grapheme_next_character_break_utf8(line + col, line_len - col);
if (inc == 0)
break;
col += inc;
}
number--;
}
if (line)
free(line);
result.row = row;
result.col = col;
return result;
}
Coord simulate_cursor_left(Editor *editor, Coord cursor, uint32_t number) {
Coord result = cursor;
if (!editor || !editor->root || number == 0)
return result;
uint32_t row = result.row;
uint32_t col = result.col;
uint32_t len = 0;
LineIterator *it = begin_l_iter(editor->root, row);
char *line = next_line(it, &len);
free(it);
if (!line)
return result;
if (len > 0 && line[len - 1] == '\n')
--len;
while (number > 0) {
if (col == 0) {
free(line);
line = nullptr;
if (row == 0)
break;
int32_t prev_row = row - 1;
while (prev_row >= 0 && editor->folded[prev_row] != 0)
prev_row--;
if (prev_row < 0)
break;
row = prev_row;
it = begin_l_iter(editor->root, row);
line = next_line(it, &len);
free(it);
if (!line)
break;
if (len > 0 && line[len - 1] == '\n')
--len;
col = len;
} else {
uint32_t new_col = 0;
while (new_col < col) {
uint32_t inc =
grapheme_next_character_break_utf8(line + new_col, len - new_col);
if (new_col + inc >= col)
break;
new_col += inc;
}
col = new_col;
}
number--;
}
if (line)
free(line);
result.row = row;
result.col = col;
return result;
}
void ensure_scroll(Editor *editor) { void ensure_scroll(Editor *editor) {
std::shared_lock knot_lock(editor->knot_mtx); std::shared_lock knot_lock(editor->knot_mtx);
if (editor->cursor.row < editor->scroll.row || if (editor->cursor.row < editor->scroll.row ||
@@ -417,72 +522,144 @@ void apply_edit(std::vector<Span> &spans, uint32_t x, int64_t y) {
} }
} }
// Reverse erase will update the cursor position void edit_erase(Editor *editor, Coord pos, int64_t len) {
void edit_erase(Editor *editor, uint32_t pos, int64_t len) {
if (len == 0) if (len == 0)
return; return;
if (len < 0) { if (len < 0) {
std::shared_lock lock_1(editor->knot_mtx); std::shared_lock lock_1(editor->knot_mtx);
TSPoint old_point = {editor->cursor.row, editor->cursor.col}; uint32_t cursor_original =
cursor_left(editor, -len); line_to_byte(editor->root, editor->cursor.row, nullptr) +
uint32_t start = line_to_byte(editor->root, editor->cursor.row, nullptr) + editor->cursor.col;
editor->cursor.col; TSPoint old_point = {pos.row, pos.col};
uint32_t byte_pos = line_to_byte(editor->root, pos.row, nullptr) + pos.col;
Coord point = simulate_cursor_left(editor, pos, -len);
uint32_t start = line_to_byte(editor->root, point.row, nullptr) + point.col;
if (cursor_original > start && cursor_original <= byte_pos) {
editor->cursor = point;
LineIterator *it = begin_l_iter(editor->root, point.row);
if (!it)
return;
uint32_t line_len;
char *line = next_line(it, &line_len);
free(it);
if (!line)
return;
editor->cursor_preffered =
get_visual_col_from_bytes(line, line_len, point.col);
free(line);
} else if (cursor_original > byte_pos) {
uint32_t cursor_new = cursor_original - (byte_pos - start);
uint32_t new_col;
uint32_t new_row = byte_to_line(editor->root, cursor_new, &new_col);
editor->cursor = {new_row, new_col};
LineIterator *it = begin_l_iter(editor->root, new_row);
if (!it)
return;
uint32_t line_len;
char *line = next_line(it, &line_len);
free(it);
if (!line)
return;
editor->cursor_preffered =
get_visual_col_from_bytes(line, line_len, new_col);
free(line);
}
lock_1.unlock(); lock_1.unlock();
std::unique_lock lock_2(editor->knot_mtx); std::unique_lock lock_2(editor->knot_mtx);
editor->root = erase(editor->root, start, pos - start); editor->root = erase(editor->root, start, byte_pos - start);
lock_2.unlock(); lock_2.unlock();
if (editor->tree) { if (editor->tree) {
TSInputEdit edit = { TSInputEdit edit = {
.start_byte = start, .start_byte = start,
.old_end_byte = pos, .old_end_byte = byte_pos,
.new_end_byte = start, .new_end_byte = start,
.start_point = {editor->cursor.row, editor->cursor.col}, .start_point = {point.row, point.col},
.old_end_point = old_point, .old_end_point = old_point,
.new_end_point = {editor->cursor.row, editor->cursor.col}, .new_end_point = {point.row, point.col},
}; };
editor->edit_queue.push(edit); editor->edit_queue.push(edit);
} }
std::unique_lock lock_3(editor->spans.mtx); std::unique_lock lock_3(editor->spans.mtx);
apply_edit(editor->spans.spans, start, start - pos); apply_edit(editor->spans.spans, start, start - byte_pos);
if (editor->spans.mid_parse) if (editor->spans.mid_parse)
editor->spans.edits.push({start, start - pos}); editor->spans.edits.push({start, start - byte_pos});
} else { } else {
std::shared_lock lock_1(editor->knot_mtx); std::shared_lock lock_1(editor->knot_mtx);
uint32_t col; uint32_t cursor_original =
uint32_t row = byte_to_line(editor->root, pos, &col); line_to_byte(editor->root, editor->cursor.row, nullptr) +
TSPoint start_point = {row, col}; editor->cursor.col;
row = byte_to_line(editor->root, pos + len, &col); TSPoint old_point = {pos.row, pos.col};
TSPoint old_end_point = {row, col}; uint32_t byte_pos = line_to_byte(editor->root, pos.row, nullptr) + pos.col;
Coord point = simulate_cursor_right(editor, pos, len);
uint32_t end = line_to_byte(editor->root, point.row, nullptr) + point.col;
if (cursor_original > byte_pos && cursor_original <= end) {
editor->cursor = pos;
LineIterator *it = begin_l_iter(editor->root, pos.row);
if (!it)
return;
uint32_t line_len;
char *line = next_line(it, &line_len);
free(it);
if (!line)
return;
editor->cursor_preffered =
get_visual_col_from_bytes(line, line_len, pos.col);
free(line);
} else if (cursor_original > end) {
uint32_t cursor_new = cursor_original - (end - byte_pos);
uint32_t new_col;
uint32_t new_row = byte_to_line(editor->root, cursor_new, &new_col);
editor->cursor = {new_row, new_col};
LineIterator *it = begin_l_iter(editor->root, new_row);
if (!it)
return;
uint32_t line_len;
char *line = next_line(it, &line_len);
free(it);
if (!line)
return;
editor->cursor_preffered =
get_visual_col_from_bytes(line, line_len, new_col);
free(line);
}
lock_1.unlock(); lock_1.unlock();
std::unique_lock lock_2(editor->knot_mtx); std::unique_lock lock_2(editor->knot_mtx);
editor->root = erase(editor->root, pos, len); editor->root = erase(editor->root, byte_pos, end - byte_pos);
lock_2.unlock(); lock_2.unlock();
if (editor->tree) { if (editor->tree) {
TSInputEdit edit = { TSInputEdit edit = {
.start_byte = pos, .start_byte = byte_pos,
.old_end_byte = pos + (uint32_t)len, .old_end_byte = end,
.new_end_byte = pos, .new_end_byte = byte_pos,
.start_point = start_point, .start_point = old_point,
.old_end_point = old_end_point, .old_end_point = {point.row, point.col},
.new_end_point = start_point, .new_end_point = old_point,
}; };
editor->edit_queue.push(edit); editor->edit_queue.push(edit);
} }
std::unique_lock lock_3(editor->spans.mtx); std::unique_lock lock_3(editor->spans.mtx);
apply_edit(editor->spans.spans, pos, -(int32_t)len); apply_edit(editor->spans.spans, byte_pos, byte_pos - end);
if (editor->spans.mid_parse) if (editor->spans.mid_parse)
editor->spans.edits.push({pos, -(int32_t)len}); editor->spans.edits.push({byte_pos, byte_pos - end});
} }
} }
void edit_insert(Editor *editor, uint32_t pos, char *data, uint32_t len) { void edit_insert(Editor *editor, Coord pos, char *data, uint32_t len) {
std::shared_lock lock_1(editor->knot_mtx); std::shared_lock lock_1(editor->knot_mtx);
uint32_t col; uint32_t cursor_original =
uint32_t row = byte_to_line(editor->root, pos, &col); line_to_byte(editor->root, editor->cursor.row, nullptr) +
TSPoint start_point = {row, col}; editor->cursor.col;
uint32_t byte_pos = line_to_byte(editor->root, pos.row, nullptr) + pos.col;
TSPoint start_point = {pos.row, pos.col};
if (cursor_original > byte_pos) {
uint32_t cursor_new = cursor_original + len;
uint32_t new_col;
uint32_t new_row = byte_to_line(editor->root, cursor_new, &new_col);
editor->cursor = {new_row, new_col};
}
lock_1.unlock(); lock_1.unlock();
std::unique_lock lock_2(editor->knot_mtx); std::unique_lock lock_2(editor->knot_mtx);
editor->root = insert(editor->root, pos, data, len); editor->root = insert(editor->root, byte_pos, data, len);
if (memchr(data, '\n', len)) if (memchr(data, '\n', len))
editor->folded.resize(editor->root->line_count + 2); editor->folded.resize(editor->root->line_count + 2);
lock_2.unlock(); lock_2.unlock();
@@ -498,9 +675,9 @@ void edit_insert(Editor *editor, uint32_t pos, char *data, uint32_t len) {
} }
if (editor->tree) { if (editor->tree) {
TSInputEdit edit = { TSInputEdit edit = {
.start_byte = pos, .start_byte = byte_pos,
.old_end_byte = pos, .old_end_byte = byte_pos,
.new_end_byte = pos + len, .new_end_byte = byte_pos + len,
.start_point = start_point, .start_point = start_point,
.old_end_point = start_point, .old_end_point = start_point,
.new_end_point = {start_point.row + rows, .new_end_point = {start_point.row + rows,
@@ -509,12 +686,26 @@ void edit_insert(Editor *editor, uint32_t pos, char *data, uint32_t len) {
editor->edit_queue.push(edit); editor->edit_queue.push(edit);
} }
std::unique_lock lock_3(editor->spans.mtx); std::unique_lock lock_3(editor->spans.mtx);
apply_edit(editor->spans.spans, pos, len); apply_edit(editor->spans.spans, byte_pos, len);
if (editor->spans.mid_parse) if (editor->spans.mid_parse)
editor->spans.edits.push({pos, len}); editor->spans.edits.push({byte_pos, len});
} }
void render_editor(Editor *editor) { void render_editor(Editor *editor) {
uint32_t sel_start = 0, sel_end = 0;
if (editor->selection_active) {
uint32_t sel1 = line_to_byte(editor->root, editor->cursor.row, nullptr) +
editor->cursor.col;
uint32_t sel2 = line_to_byte(editor->root, editor->selection.row, nullptr) +
editor->selection.col;
if (sel1 <= sel2) {
sel_start = sel1;
sel_end = sel2;
} else {
sel_start = sel2;
sel_end = sel1;
}
}
Coord cursor = {UINT32_MAX, UINT32_MAX}; Coord cursor = {UINT32_MAX, UINT32_MAX};
uint32_t line_index = editor->scroll.row; uint32_t line_index = editor->scroll.row;
SpanCursor span_cursor(editor->spans); SpanCursor span_cursor(editor->spans);
@@ -571,6 +762,9 @@ void render_editor(Editor *editor) {
uint32_t fg = hl ? hl->fg : 0xFFFFFF; uint32_t fg = hl ? hl->fg : 0xFFFFFF;
uint32_t bg = hl ? hl->bg : 0; uint32_t bg = hl ? hl->bg : 0;
uint8_t fl = hl ? hl->flags : 0; uint8_t fl = hl ? hl->flags : 0;
if (editor->selection_active && absolute_byte_pos >= sel_start &&
absolute_byte_pos < sel_end)
bg = 0x555555;
uint32_t cluster_len = grapheme_next_character_break_utf8( uint32_t cluster_len = grapheme_next_character_break_utf8(
line + current_byte_offset + local_render_offset, line_left); line + current_byte_offset + local_render_offset, line_left);
std::string cluster(line + current_byte_offset + local_render_offset, std::string cluster(line + current_byte_offset + local_render_offset,
@@ -592,6 +786,14 @@ void render_editor(Editor *editor) {
cursor.row = editor->position.row + rendered_rows; cursor.row = editor->position.row + rendered_rows;
cursor.col = editor->position.col + col; cursor.col = editor->position.col + col;
} }
if (editor->selection_active &&
global_byte_offset + line_len + 1 >= sel_start &&
global_byte_offset + line_len + 1 <= sel_end &&
col < editor->size.col) {
update(editor->position.row + rendered_rows, editor->position.col + col,
" ", 0, 0x555555, 0);
col++;
}
while (col < editor->size.col) { while (col < editor->size.col) {
update(editor->position.row + rendered_rows, editor->position.col + col, update(editor->position.row + rendered_rows, editor->position.col + col,
" ", 0xFFFFFF, 0, 0); " ", 0xFFFFFF, 0, 0);
@@ -607,6 +809,13 @@ void render_editor(Editor *editor) {
cursor.col = editor->position.col; cursor.col = editor->position.col;
} }
uint32_t col = 0; uint32_t col = 0;
if (editor->selection_active &&
global_byte_offset + line_len + 1 >= sel_start &&
global_byte_offset + line_len + 1 <= sel_end) {
update(editor->position.row + rendered_rows, editor->position.col + col,
" ", 0, 0x555555, 0);
col++;
}
while (col < editor->size.col) { while (col < editor->size.col) {
update(editor->position.row + rendered_rows, editor->position.col + col, update(editor->position.row + rendered_rows, editor->position.col + col,
" ", 0xFFFFFF, 0, 0); " ", 0xFFFFFF, 0, 0);

View File

@@ -49,32 +49,21 @@ void handle_editor_event(Editor *editor, KeyEvent event) {
event.c == ':' || event.c == '\'' || event.c == '"' || event.c == ',' || event.c == ':' || event.c == '\'' || event.c == '"' || event.c == ',' ||
event.c == '.' || event.c == '<' || event.c == '>' || event.c == '/' || event.c == '.' || event.c == '<' || event.c == '>' || event.c == '/' ||
event.c == '?' || event.c == '`' || event.c == '~')) { event.c == '?' || event.c == '`' || event.c == '~')) {
edit_insert(editor, edit_insert(editor, editor->cursor, &event.c, 1);
line_to_byte(editor->root, editor->cursor.row, nullptr) +
editor->cursor.col,
&event.c, 1);
cursor_right(editor, 1); cursor_right(editor, 1);
} }
if (event.key_type == KEY_CHAR && event.c == '\t') { if (event.key_type == KEY_CHAR && event.c == '\t') {
edit_insert(editor, edit_insert(editor, editor->cursor, (char *)"\t", 1);
line_to_byte(editor->root, editor->cursor.row, nullptr) +
editor->cursor.col,
(char *)"\t", 1);
cursor_right(editor, 2); cursor_right(editor, 2);
} }
if (event.key_type == KEY_CHAR && (event.c == '\n' || event.c == '\r')) { if (event.key_type == KEY_CHAR && (event.c == '\n' || event.c == '\r')) {
edit_insert(editor, edit_insert(editor, editor->cursor, (char *)"\n", 1);
line_to_byte(editor->root, editor->cursor.row, nullptr) +
editor->cursor.col,
(char *)"\n", 1);
cursor_right(editor, 1); cursor_right(editor, 1);
} }
if (event.key_type == KEY_CHAR && event.c == 0x7F) { if (event.key_type == KEY_CHAR && event.c == 0x7F)
edit_erase(editor, edit_erase(editor, editor->cursor, -1);
line_to_byte(editor->root, editor->cursor.row, nullptr) + if (event.key_type == KEY_SPECIAL && event.special_key == KEY_DELETE)
editor->cursor.col, edit_erase(editor, editor->cursor, 1);
-1);
}
ensure_scroll(editor); ensure_scroll(editor);
} }
@@ -82,8 +71,7 @@ int main(int argc, char *argv[]) {
Coord screen = start_screen(); Coord screen = start_screen();
const char *filename = (argc > 1) ? argv[1] : ""; const char *filename = (argc > 1) ? argv[1] : "";
Editor *editor = Editor *editor = new_editor(filename, {0, 0}, screen);
new_editor(filename, {10, 10}, {screen.row - 20, screen.col - 20});
if (!editor) { if (!editor) {
end_screen(); end_screen();
fprintf(stderr, "Failed to load editor\n"); fprintf(stderr, "Failed to load editor\n");