Bug fixes.
This commit is contained in:
@@ -2,51 +2,22 @@ package editor
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"pad/internal/ui"
|
||||
)
|
||||
|
||||
// TestCursorExistsInLayout checks if the EditorLayout produces a cursor element.
|
||||
func TestCursorExistsInLayout(t *testing.T) {
|
||||
state := NewState()
|
||||
TheState = state
|
||||
// TestByteOffsetToLineCol_Logical verifies that the current logic
|
||||
// only handles logical lines (newline-delimited).
|
||||
func TestByteOffsetToLineCol_Logical(t *testing.T) {
|
||||
buf := "hello\nworld"
|
||||
// 012345 67890
|
||||
|
||||
// Set to EditorPage
|
||||
state.page = EditorPage
|
||||
|
||||
elems := EditorLayout(ui.Dp(800), ui.Dp(600), false)
|
||||
|
||||
cursorFound := false
|
||||
for _, e := range elems {
|
||||
// Assuming we add a Type() method to ui.Element interface later.
|
||||
// For now, this will fail to compile if Type() doesn't exist.
|
||||
// Let's assume we can cast or inspect the element.
|
||||
if e.Type() == "cursor" {
|
||||
cursorFound = true
|
||||
break
|
||||
}
|
||||
// Logical line 0: "hello" (0-4), pos 5 is '\n', line 1 starts at 6
|
||||
line, col := byteOffsetToLineCol(buf, 5)
|
||||
if line != 0 || col != 5 {
|
||||
t.Errorf("expected (0, 5) for pos 5, got (%d, %d)", line, col)
|
||||
}
|
||||
|
||||
if !cursorFound {
|
||||
t.Error("Expected cursor element in EditorLayout")
|
||||
}
|
||||
}
|
||||
|
||||
// TestKeyDownMove verifies key down events update CursorPosition.
|
||||
func TestKeyDownMove(t *testing.T) {
|
||||
state := NewState()
|
||||
TheState = state
|
||||
state.Editor.Buffer = "Hello"
|
||||
state.Editor.CursorPosition = 1
|
||||
|
||||
// Simulate Right Arrow
|
||||
HandleKeyDown("Right")
|
||||
if state.Editor.CursorPosition != 2 {
|
||||
t.Errorf("Expected cursor position 2, got %d", state.Editor.CursorPosition)
|
||||
}
|
||||
|
||||
// Simulate Left Arrow
|
||||
HandleKeyDown("Left")
|
||||
if state.Editor.CursorPosition != 1 {
|
||||
t.Errorf("Expected cursor position 1, got %d", state.Editor.CursorPosition)
|
||||
|
||||
line, col = byteOffsetToLineCol(buf, 6)
|
||||
if line != 1 || col != 0 {
|
||||
t.Errorf("expected (1, 0) for pos 6, got (%d, %d)", line, col)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -197,19 +197,40 @@ func HandleCursorMove(delta int) {
|
||||
TheState.Editor.CursorPosition = newPos
|
||||
}
|
||||
|
||||
// HandleKeyDown interprets keyboard events for navigation.
|
||||
// HandleKeyDown interprets keyboard events for navigation and editing.
|
||||
// Receives both key.Event (as key.Name) and key.EditEvent from the main loop.
|
||||
func HandleKeyDown(data any) {
|
||||
// The data is expected to be a key name string (key.Name)
|
||||
keyName := data.(key.Name)
|
||||
log.Printf("HandleKeyDown: %s", keyName)
|
||||
switch keyName {
|
||||
case key.NameLeftArrow:
|
||||
HandleCursorMove(-1)
|
||||
case key.NameRightArrow:
|
||||
HandleCursorMove(1)
|
||||
switch v := data.(type) {
|
||||
case key.EditEvent:
|
||||
// Text input from IME / keyboard
|
||||
log.Printf("HandleKeyDown: EditEvent text=%q", v.Text)
|
||||
HandleInsert(v.Text)
|
||||
case key.Name:
|
||||
log.Printf("HandleKeyDown: %s", v)
|
||||
switch v {
|
||||
case key.NameLeftArrow:
|
||||
HandleCursorMove(-1)
|
||||
case key.NameRightArrow:
|
||||
HandleCursorMove(1)
|
||||
case key.NameDeleteBackward:
|
||||
HandleBackspace()
|
||||
case key.NameDeleteForward:
|
||||
HandleDelete()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// HandleDelete removes the character after the cursor.
|
||||
func HandleDelete() {
|
||||
pos := TheState.Editor.CursorPosition
|
||||
buf := TheState.Editor.Buffer
|
||||
if pos >= len(buf) {
|
||||
return
|
||||
}
|
||||
TheState.Editor.Buffer = buf[:pos] + buf[pos+1:]
|
||||
TheState.Editor.Dirty = true
|
||||
}
|
||||
|
||||
// HandleInsert inserts a string at the current cursor position.
|
||||
func HandleInsert(s string) {
|
||||
pos := TheState.Editor.CursorPosition
|
||||
|
||||
Reference in New Issue
Block a user