factor out find previous space logic
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user