116 lines
2.7 KiB
Go
116 lines
2.7 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}
|
|
}
|
|
|
|
// DeletePrevChar implements Action (x)
|
|
type DeletePrevChar struct {
|
|
Count int
|
|
}
|
|
|
|
// DeletePrevChar.Execute: Deletes Count characters before the cursor position (x key).
|
|
func (a DeletePrevChar) 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++ {
|
|
if pos > 0 {
|
|
line = line[:pos-1] + line[pos:]
|
|
buf.SetLine(win.Cursor.Line, line)
|
|
pos--
|
|
win.SetCursorCol(pos)
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// DeletePrevChar.WithCount: Returns a new DeletePrevChar with the given count.
|
|
func (a DeletePrevChar) WithCount(n int) Action {
|
|
return DeletePrevChar{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}
|
|
}
|