From f6d7dc0db07931348e515d368d322b9c51cbbb05 Mon Sep 17 00:00:00 2001 From: Patrick Cleaveliln Date: Fri, 11 Jul 2025 03:48:19 +0000 Subject: [PATCH] fix cursor drift + tests --- src/core/file_buffer.odin | 5 ++++- src/tests/tests.odin | 43 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/src/core/file_buffer.odin b/src/core/file_buffer.odin index 4782796..88c10f5 100644 --- a/src/core/file_buffer.odin +++ b/src/core/file_buffer.odin @@ -527,9 +527,12 @@ move_cursor_down :: proc(buffer: ^FileBuffer, amount: int = 1, cursor: Maybe(^Cu break; } } + if it.hit_end { + return + } line_length := file_buffer_line_length(buffer, it.cursor.index); - if it.cursor.col < line_length && it.cursor.col < current_col { + if it.cursor.col < line_length-1 && it.cursor.col < current_col { for _ in iterate_file_buffer(&it) { if it.cursor.col >= line_length-1 || it.cursor.col >= current_col { break; diff --git a/src/tests/tests.odin b/src/tests/tests.odin index 223b244..f5e8fda 100644 --- a/src/tests/tests.odin +++ b/src/tests/tests.odin @@ -322,6 +322,49 @@ delete_across_slices :: proc(t: ^testing.T) { testing.expectf(t, contents == expected_text, "got '%v', expected '%v'", contents, expected_text) } +@(test) +move_down_next_line_has_shorter_length :: proc(t: ^testing.T) { + e := new_test_editor() + setup_empty_buffer(&e) + + is_ctrl_pressed := false + + buffer := &e.buffers[0] + + run_text_insertion(&e, "012345678\n0") + + // Move up to the first line + run_input_multiple(&e, press_key(.K), 1) + + // Move to the end of the line + run_inputs(&e, []ArtificialInput{ press_key(.G), press_key(.L)}) + + // Move down to the second line + run_input_multiple(&e, press_key(.J), 1) + + expect_line_col(t, buffer.cursor, 1, 0) + expect_cursor_index(t, buffer.cursor, 0, 10) +} + +@(test) +move_down_on_last_line :: proc(t: ^testing.T) { + e := new_test_editor() + setup_empty_buffer(&e) + + is_ctrl_pressed := false + + buffer := &e.buffers[0] + + run_text_insertion(&e, "012345678") + + // Try to move down + run_input_multiple(&e, press_key(.J), 1) + + // Cursor should stay where it is + expect_line_col(t, buffer.cursor, 0, 8) + expect_cursor_index(t, buffer.cursor, 0, 8) +} + @(test) move_left_at_beginning_of_file :: proc(t: ^testing.T) { e := new_test_editor()