Gim/internal/motion/basic.go
Hayden Hargreaves e362c9f118
All checks were successful
Run Test Suite / test (push) Successful in 56s
feat: gap buffer is implemented, tested
Not sure if this is perfect, but it seems to be working
2026-04-02 12:39:30 -07:00

95 lines
2.3 KiB
Go

package motion
import (
"git.gophernest.net/azpect/TextEditor/internal/action"
"git.gophernest.net/azpect/TextEditor/internal/core"
tea "github.com/charmbracelet/bubbletea"
)
// MoveDown implements Motion (j) - linewise
type MoveDown struct {
Count int
}
// MoveDown.Execute: Moves the cursor down by Count lines without going past
// the last line of the buffer.
func (a MoveDown) Execute(m action.Model) tea.Cmd {
win := m.ActiveWindow()
buf := m.ActiveBuffer()
for i := 0; i < a.Count && win.Cursor.Line < buf.LineCount()-1; i++ {
win.SetCursorLine(win.Cursor.Line + 1)
}
return nil
}
func (a MoveDown) Type() core.MotionType { return core.Linewise }
func (a MoveDown) WithCount(n int) action.Action {
return MoveDown{Count: n}
}
// MoveUp implements Motion (k) - linewise
type MoveUp struct {
Count int
}
// MoveUp.Execute: Moves the cursor up by Count lines without going above
// line 0.
func (a MoveUp) Execute(m action.Model) tea.Cmd {
win := m.ActiveWindow()
for i := 0; i < a.Count && win.Cursor.Line > 0; i++ {
win.SetCursorLine(win.Cursor.Line - 1)
}
return nil
}
func (a MoveUp) Type() core.MotionType { return core.Linewise }
func (a MoveUp) WithCount(n int) action.Action {
return MoveUp{Count: n}
}
// MoveLeft implements Motion (h) - charwise
type MoveLeft struct {
Count int
}
// MoveLeft.Execute: Moves the cursor left by Count columns without going
// past column 0.
func (a MoveLeft) Execute(m action.Model) tea.Cmd {
win := m.ActiveWindow()
for i := 0; i < a.Count && win.Cursor.Col > 0; i++ {
win.SetCursorCol(win.Cursor.Col - 1)
}
return nil
}
func (a MoveLeft) Type() core.MotionType { return core.CharwiseExclusive }
func (a MoveLeft) WithCount(n int) action.Action {
return MoveLeft{Count: n}
}
// MoveRight implements Motion (l) - charwise
type MoveRight struct {
Count int
}
// MoveRight.Execute: Moves the cursor right by Count columns without going
// past the end of the line.
func (a MoveRight) Execute(m action.Model) tea.Cmd {
win := m.ActiveWindow()
buf := m.ActiveBuffer()
lineLen := buf.Lines[win.Cursor.Line].Len()
for i := 0; i < a.Count && win.Cursor.Col <= lineLen; i++ {
win.SetCursorCol(win.Cursor.Col + 1)
}
return nil
}
func (a MoveRight) Type() core.MotionType { return core.CharwiseExclusive }
func (a MoveRight) WithCount(n int) action.Action {
return MoveRight{Count: n}
}