Gim/internal/action/delete.go
Hayden Hargreaves ccb061989a refactor: huge refactor, this looks amazing.
Lots of comments from the AI. Some tests are not passing though
2026-03-04 21:45:47 -07:00

87 lines
2.0 KiB
Go

package action
import tea "github.com/charmbracelet/bubbletea"
// DeleteChar implements Action (x)
type DeleteChar struct {
Count int
}
// DeleteChar.Execute: Deletes Count characters at the cursor position (x key).
func (a DeleteChar) Execute(m Model) tea.Cmd {
win := m.ActiveWindow()
buf := m.ActiveBuffer()
pos := win.Cursor.Col
line := buf.Lines[win.Cursor.Line]
for i := 0; i < a.Count && pos < len(line); i++ {
line = line[:pos] + line[pos+1:]
buf.SetLine(win.Cursor.Line, line)
}
return nil
}
// DeleteChar.WithCount: Returns a new DeleteChar with the given count.
func (a DeleteChar) WithCount(n int) Action {
return DeleteChar{Count: n}
}
// DeleteToEndOfLine implements Action (D) - deletes from cursor to end of line
// and optionally Count-1 additional lines below.
type DeleteToEndOfLine struct {
Count int
}
// DeleteToEndOfLine.Execute: Deletes from cursor to end of line and Count-1 lines below (D key).
func (a DeleteToEndOfLine) Execute(m Model) tea.Cmd {
win := m.ActiveWindow()
buf := m.ActiveBuffer()
// Delete to end of line
pos := win.Cursor.Col
line := buf.Lines[win.Cursor.Line]
buf.SetLine(win.Cursor.Line, line[:pos])
win.SetCursorCol(pos - 1)
// If count is greater, than we will delete the next N - 1 lines below
initY := win.Cursor.Line
if a.Count > 1 {
// Copied from `internal/operator/delete.go`
opCount := min(a.Count-1, buf.LineCount()-win.Cursor.Line)
// Down one
win.SetCursorLine(initY + 1)
for range opCount {
y := win.Cursor.Line // Changed from the copied code
// Stop if were on the starting line
if y == initY {
break
}
buf.DeleteLine(y)
if buf.LineCount() == 0 {
buf.InsertLine(0, "")
}
if y >= buf.LineCount() {
y = buf.LineCount() - 1
}
win.SetCursorLine(y)
}
}
win.SetCursorLine(initY)
return nil
}
// DeleteToEndOfLine.WithCount: Returns a new DeleteToEndOfLine with the given count.
func (a DeleteToEndOfLine) WithCount(n int) Action {
return DeleteToEndOfLine{Count: n}
}