From cf21773e6fe81ea27c958d127153205edc4ea75c Mon Sep 17 00:00:00 2001 From: Patrick Cleaveliln Date: Sat, 12 Jul 2025 03:49:06 +0000 Subject: [PATCH] fix cursor bug + fix crash --- src/core/file_buffer.odin | 18 ++++++++++-------- src/panels/panels.odin | 12 +++++++----- src/tests/tests.odin | 22 ++++++++++++++++++++++ 3 files changed, 39 insertions(+), 13 deletions(-) diff --git a/src/core/file_buffer.odin b/src/core/file_buffer.odin index 1a8ca90..0dff5ce 100644 --- a/src/core/file_buffer.odin +++ b/src/core/file_buffer.odin @@ -88,16 +88,18 @@ file_buffer_end :: proc(buffer: ^FileBuffer) -> Cursor { iterate_file_buffer :: proc(it: ^FileBufferIter) -> (character: u8, idx: PieceTableIndex, cond: bool) { character, idx, cond = iterate_piece_table_iter(&it.piter) - if character == '\n' { - it.cursor.col = 0 - it.cursor.line += 1 - } else { - it.cursor.col += 1 - } - it.cursor.index = it.piter.index it.hit_end = it.piter.hit_end + if cond && !it.hit_end { + if character == '\n' { + it.cursor.col = 0 + it.cursor.line += 1 + } else { + it.cursor.col += 1 + } + } + return } @@ -108,7 +110,7 @@ iterate_file_buffer_reverse :: proc(it: ^FileBufferIter) -> (character: u8, idx: it.cursor.index = it.piter.index it.hit_end = it.piter.hit_end - if cond { + if cond && !it.hit_end { if it.cursor.col > 0 { it.cursor.col -= 1 } else if it.cursor.line > 0 { diff --git a/src/panels/panels.odin b/src/panels/panels.odin index 6f8cfd7..c056aab 100644 --- a/src/panels/panels.odin +++ b/src/panels/panels.odin @@ -301,11 +301,13 @@ make_grep_panel :: proc(state: ^core.State) -> core.Panel { free_grep_results(rs_results) panel_state.selected_result = 0 - core.update_glyph_buffer_from_bytes( - &panel_state.glyphs, - transmute([]u8)panel_state.query_results[panel_state.selected_result].file_context, - panel_state.query_results[panel_state.selected_result].line, - ) + if len(panel_state.query_results) > 0 { + core.update_glyph_buffer_from_bytes( + &panel_state.glyphs, + transmute([]u8)panel_state.query_results[panel_state.selected_result].file_context, + panel_state.query_results[panel_state.selected_result].line, + ) + } } core.register_key_action(&input_map.mode[.Normal], .ENTER, proc(state: ^core.State) { diff --git a/src/tests/tests.odin b/src/tests/tests.odin index 6c0a03a..7d5ac0e 100644 --- a/src/tests/tests.odin +++ b/src/tests/tests.odin @@ -536,6 +536,28 @@ move_to_beginning_of_line_from_start :: proc(t: ^testing.T) { expect_cursor_index(t, buffer.cursor, 0, 0) } +@(test) +append_end_of_line :: proc(t: ^testing.T) { + e := new_test_editor() + setup_empty_buffer(&e) + + buffer := &e.buffers[0] + + run_text_insertion(&e, "hello") + + run_input_multiple(&e, press_key(.A), 1) + run_input_multiple(&e, press_key(.ESCAPE), 1) + + expect_line_col(t, buffer.cursor, 0, 5) + expect_cursor_index(t, buffer.cursor, 1, 0) + + run_input_multiple(&e, press_key(.A), 1) + run_input_multiple(&e, press_key(.ESCAPE), 1) + + expect_line_col(t, buffer.cursor, 0, 5) + expect_cursor_index(t, buffer.cursor, 1, 0) +} + @(test) insert_line_under_current :: proc(t: ^testing.T) { e := new_test_editor()