From 3ad4176eee73b6dafb7ca536c3cfca1986eb510d Mon Sep 17 00:00:00 2001 From: Grant Horner Date: Tue, 9 Dec 2025 11:37:29 -0500 Subject: [PATCH] factor out find previous space logic --- src/main.odin | 32 +++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/src/main.odin b/src/main.odin index 5597b1d..e51bfdf 100644 --- a/src/main.odin +++ b/src/main.odin @@ -29,7 +29,7 @@ read_file :: proc(path: string) -> (string, bool) { Viewport :: struct { start: int, - end: int, + end: int, } Cursor :: struct { @@ -44,7 +44,10 @@ text_height: f32 cursor: Cursor lines: [dynamic][dynamic]u8 viewport_size := window_height / int(font_size) -viewport := Viewport{start = 0, end = viewport_size} +viewport := Viewport { + start = 0, + end = viewport_size, +} main :: proc() { r.InitWindow(i32(window_width), i32(window_height), "odit") @@ -104,17 +107,8 @@ main :: proc() { cursor.char = len(current_line()) } if r.IsKeyDown(r.KeyboardKey.LEFT_ALT) && 0 < cursor.char { - seen_space := false - #reverse for c, c_index in current_line()[:cursor.char - 1] { - if is_whitespace(c) { - seen_space = true - preferred_position = c_index + 1 - break - } - } - if !seen_space { - preferred_position = 0 - } + found := false + preferred_position, found = find_previous_space(cursor.char, current_line()[:]) } cursor.char = max(preferred_position, 0) } @@ -268,3 +262,15 @@ all_whitespace :: proc(cs: []u8) -> bool { repeatable_key_pressed :: proc(k: r.KeyboardKey) -> bool { return r.IsKeyPressed(k) || r.IsKeyPressedRepeat(k) } + +find_previous_space :: proc(current_position: int, line: []u8) -> (result: int, found: bool) { + #reverse for c, c_index in line[:current_position - 1] { + if is_whitespace(c) { + found = true + result = c_index + 1 + break + } + } + + return result, found +}