Implement 'del' in insert mode and tests.

This commit is contained in:
Hayden Hargreaves 2026-02-10 12:12:16 -07:00
parent c62bbc89ee
commit 3d3948d7e3
3 changed files with 74 additions and 1 deletions

View File

@ -392,3 +392,67 @@ func TestInsertModeBackspace(t *testing.T) {
}
})
}
func TestInsertModeDelete(t *testing.T) {
t.Run("test delete deletes character", func(t *testing.T) {
lines := []string{"world"}
tm := newTestModelWithCursorPosAndLines(t, lines, action.Position{Col: 3, Line: 0})
sendKeys(tm, "i", "delete", "esc")
m := getFinalModel(t, tm)
if m.lines[0] != "word" {
t.Errorf("lines[0] = %q, want 'word'", m.lines[0])
}
})
t.Run("test delete at end of line joins lines", func(t *testing.T) {
lines := []string{"hello", "world"}
tm := newTestModelWithCursorPosAndLines(t, lines, action.Position{Col: 5, Line: 0})
sendKeys(tm, "i", "delete", "esc")
m := getFinalModel(t, tm)
if len(m.lines) != 1 {
t.Errorf("len(lines) = %d, want 1", len(m.lines))
}
if m.lines[0] != "helloworld" {
t.Errorf("lines[0] = %q, want 'helloworld'", m.lines[0])
}
})
t.Run("test delete at start of empty line joins lines", func(t *testing.T) {
lines := []string{"", "world"}
tm := newTestModelWithLines(t, lines)
sendKeys(tm, "i", "delete", "esc")
m := getFinalModel(t, tm)
if len(m.lines) != 1 {
t.Errorf("len(lines) = %d, want 1", len(m.lines))
}
if m.lines[0] != "world" {
t.Errorf("lines[0] = %q, want 'world'", m.lines[0])
}
})
t.Run("test delete at end of last line does nothing", func(t *testing.T) {
lines := []string{"hello"}
tm := newTestModelWithCursorPosAndLines(t, lines, action.Position{Col: 5, Line: 0})
sendKeys(tm, "i", "delete", "esc")
m := getFinalModel(t, tm)
if m.lines[0] != "hello" {
t.Errorf("lines[0] = %q, want 'hello'", m.lines[0])
}
})
t.Run("test multiple delete", func(t *testing.T) {
lines := []string{"hello"}
tm := newTestModelWithCursorPosAndLines(t, lines, action.Position{Col: 1, Line: 0})
sendKeys(tm, "i", "delete", "delete", "delete", "esc")
m := getFinalModel(t, tm)
if m.lines[0] != "ho" {
t.Errorf("lines[0] = %q, want 'he'", m.lines[0])
}
})
}

View File

@ -185,6 +185,15 @@ func (m *Model) processInsertKey(key string) {
m.SetCursorX(newX)
}
case "delete":
if x == len(l) && y < m.LineCount() {
nextLine := m.Line(y + 1)
m.SetLine(y, l+nextLine)
m.DeleteLine(y + 1)
} else if x >= 0 {
m.SetLine(y, l[:x]+l[x+1:])
}
// Regular character
default:
if x < len(l) {

View File

@ -19,7 +19,7 @@ func NewNormalKeymap() *Keymap {
"h": motion.MoveLeft{Count: 1},
"l": motion.MoveRight{Count: 1},
"G": motion.MoveToBottom{},
"gg": motion.MoveToTop{}, // multi-key example
"gg": motion.MoveToTop{},
"0": motion.MoveToLineStart{},
"$": motion.MoveToLineEnd{},
},