diff --git a/edit.jai b/edit.jai index 9c8eda4..ad7d533 100644 --- a/edit.jai +++ b/edit.jai @@ -245,7 +245,7 @@ handle_arrow :: (state: *State, key_code: Input.Key_Code) { handle_arrow :: (using state: *State, using event: Input.Event) { visible_lines := get_visible_lines(state); - + if visible_lines.count == 0 return; if key_code == { case .ARROW_UP; @@ -259,7 +259,8 @@ handle_arrow :: (using state: *State, using event: Input.Event) { case .ARROW_DOWN; if cursor_y == visible_lines.count - 1 { first_line = min(first_line + 1, lines.count); - } else { + } else if cursor_y + first_line + visible_lines.count >= lines.count { + } else { cursor_y += 1; } @@ -287,6 +288,7 @@ handle_arrow :: (using state: *State, using event: Input.Event) { } get_visible_lines :: (using state: State) -> [][..]u8 { + if line_height <= 1 || window_height == 0 return .[]; num_lines_in_screen := window_height / line_height - 1; return array_view(lines, first_line, num_lines_in_screen); } diff --git a/test.jai b/test.jai index e6a1165..6c2a0d5 100644 --- a/test.jai +++ b/test.jai @@ -1,5 +1,73 @@ main :: () { + print("\nBeginning tests...\n"); + // test_handle_arrow_empty_lines(); + // test_handle_arrow_one_line(); + x := 1; + y := 2; + assert_test(x == y); } -#load "edit.jai"; +test_handle_arrow_empty_lines :: () { + state: State; + state.cursor_y = 0; + state.cursor_x = 0; + + codes := Input.Key_Code.[.ARROW_DOWN, .ARROW_UP, .ARROW_LEFT, .ARROW_RIGHT]; + for codes { + handle_arrow(*state, .{key_code=it}); + assert(state.cursor_x == 0 && state.cursor_y == 0, "%: cursor_x % & cursor_y %", it, state.cursor_x, state.cursor_y); + } + + print("test_handle_arrow_empty_lines completed successfully\n"); +} + +test_handle_arrow_one_line :: () { + state: State; + state.cursor_y = 0; + state.cursor_x = 0; + + line: [..]u8; + line_str := "foo bar"; + + array_add(*line, .{data=line_str.data, count=line_str.count}); + array_add(*state.lines, line); + + codes := Input.Key_Code.[.ARROW_DOWN, .ARROW_UP, .ARROW_LEFT]; + for codes { + handle_arrow(*state, .{key_code=it}); + assert(state.cursor_x == 0 && state.cursor_y == 0); + } + + handle_arrow(*state, .{key_code=.ARROW_RIGHT}); + assert(state.cursor_x == 1 && state.cursor_y == 0); + + print("test_handle_arrow_one_line completed successfully\n"); +} + +assert_test :: (value: bool, $code := #caller_code) { + builder: String_Builder; + root, expressions := compiler_get_nodes(code); + loc := #location(code); + + print_to_builder(*builder, "%:%: Test: ", loc.fully_pathed_filename, loc.line_number); + print_expression(*builder, root); + append(*builder, "\n"); + `result := #insert code; + if !result { + for expressions { + if it == root continue; + print_expression(*builder, it); + append(*builder, "\n"); + } + append(*builder, "\n"); + `expression := builder_to_string(*builder); + `print("%\n", expression); + } else { + `print("Passed!\n"); + } +} + +#import,file "edit.jai"; #import "Basic"; +#import "Compiler"; +#import "Program_Print";