diff --git a/src/input/input.odin b/src/input/input.odin index 29a38eb..cf717b8 100644 --- a/src/input/input.odin +++ b/src/input/input.odin @@ -1,5 +1,6 @@ package input +import "base:runtime" import "core:log" import "vendor:sdl2" @@ -220,11 +221,16 @@ register_default_text_input_actions :: proc(input_map: ^core.InputActions) { // TODO: add shift+o to insert newline above current one core.register_key_action(input_map, .O, proc(state: ^State) { - core.move_cursor_end_of_line(core.current_buffer(state), false); - core.insert_content(core.current_buffer(state), []u8{'\n'}); - state.mode = .Insert; + if buffer := core.current_buffer(state); buffer != nil { + core.move_cursor_end_of_line(buffer, false); + runtime.clear(&buffer.input_buffer) - sdl2.StartTextInput(); + append(&buffer.input_buffer, '\n') + + state.mode = .Insert; + + sdl2.StartTextInput(); + } }, "insert mode on newline"); // Copy-Paste diff --git a/src/tests/tests.odin b/src/tests/tests.odin index c0cd4bd..223b244 100644 --- a/src/tests/tests.odin +++ b/src/tests/tests.odin @@ -322,24 +322,6 @@ delete_across_slices :: proc(t: ^testing.T) { testing.expectf(t, contents == expected_text, "got '%v', expected '%v'", contents, expected_text) } -// @(test) -// insert_line_under_current :: proc(t: ^testing.T) { -// e := new_test_editor() -// setup_empty_buffer(&e) - -// buffer := &e.buffers[0] - -// inputted_text := "Hello, world!\nThis is a new line" -// expected_text := fmt.aprintf("%v\n", inputted_text) -// run_text_insertion(&e, inputted_text) - -// expect_line_col(t, buffer.cursor, 1, 17) -// expect_cursor_index(t, buffer.cursor, 0, 31) - -// contents := buffer_to_string(core.current_buffer(&e)) -// testing.expectf(t, contents == expected_text, "got '%v', expected '%v'", contents, expected_text) -// } - @(test) move_left_at_beginning_of_file :: proc(t: ^testing.T) { e := new_test_editor() @@ -478,6 +460,42 @@ move_to_beginning_of_line_from_start :: proc(t: ^testing.T) { expect_cursor_index(t, buffer.cursor, 0, 0) } +@(test) +insert_line_under_current :: proc(t: ^testing.T) { + e := new_test_editor() + setup_empty_buffer(&e) + + buffer := &e.buffers[0] + + initial_text := "Hello, world!\nThis is a new line" + run_text_insertion(&e, initial_text) + + expected_text := "Hello, world!\nThis is the second line\nThis is a new line\n" + // ------------- ---------------------- + // ------------------------- + // 0 1 3 + + // Move cursor up onto the end of "Hello, world!" + run_input_multiple(&e, press_key(.K), 1) + + // Insert line below and enter insert mode + run_input_multiple(&e, press_key(.O), 1) + + // Technically the cursor is still on the first line, because the `input_buffer` + // has been modified but not the actual contents of the filebuffer + expect_line_col(t, buffer.cursor, 0, 13) + expect_cursor_index(t, buffer.cursor, 0, 13) + + run_text_insertion(&e, "This is the second line") + + expect_line_col(t, buffer.cursor, 1, 22) + expect_cursor_index(t, buffer.cursor, 1, 23) + + contents := buffer_to_string(core.current_buffer(&e)) + testing.expectf(t, contents == expected_text, "got '%v', expected '%v'", contents, expected_text) +} + + run_editor_frame :: proc(state: ^core.State, input: ArtificialInput, is_ctrl_pressed: ^bool) { log.infof("running input: %v", input)