Compare commits

..

2 Commits

Author SHA1 Message Date
04311187af fix backspace bug 2025-12-09 12:03:35 -05:00
3ad4176eee factor out find previous space logic 2025-12-09 11:37:29 -05:00
3 changed files with 65 additions and 17 deletions

2
.gitignore vendored
View File

@@ -3,3 +3,5 @@ bin
.DS_Store
src/src
scratch
src.bin*
*.sublime-workspace

26
odit.sublime-project Normal file
View File

@@ -0,0 +1,26 @@
{
"debugger_configurations":
[
{
"type": "lldb",
"request": "launch",
"name": "Launch",
"program": "${project_path}/src.bin",
"args": [],
"cwd": "${project_path}"
},
],
"build_systems": [
{
"cmd": ["odin", "build", "src", "-debug"],
"name": "odit",
"selector": "source.odin",
"working_dir": "${project_path}"
}
],
"folders": [
{
"path": "."
}
]
}

View File

@@ -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)
}
@@ -180,9 +174,10 @@ main :: proc() {
fmt.println("Wrote file!")
}
if repeatable_key_pressed(r.KeyboardKey.BACKSPACE) {
if cursor.line == 0 && cursor.char == 0 do continue
skip_backspace: if repeatable_key_pressed(r.KeyboardKey.BACKSPACE) {
if cursor.line == 0 && cursor.char == 0 do break skip_backspace
if cursor.char == 0 {
// join lines
old_len := len(lines[cursor.line - 1])
append(&lines[cursor.line - 1], string(current_line()[:]))
line_to_remove := current_line()^
@@ -191,8 +186,21 @@ main :: proc() {
cursor.line -= 1
cursor.char = old_len
} else {
ordered_remove(&lines[cursor.line], cursor.char - 1)
cursor.char -= 1
if r.IsKeyDown(r.KeyboardKey.LEFT_ALT) {
// delete by word
delete_to, found := find_previous_space(cursor.char, current_line()[:])
if found {
remove_range(current_line(), delete_to, cursor.char)
cursor.char = delete_to
} else {
remove_range(current_line(), 0, cursor.char)
cursor.char = 0
}
} else {
// delete single char
ordered_remove(&lines[cursor.line], cursor.char - 1)
cursor.char -= 1
}
}
}
@@ -268,3 +276,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
}