feat: insert new lines with o/O

This commit is contained in:
Maas Lalani 2022-08-11 17:52:41 -04:00
parent e691e35a6d
commit 9eea57c1e8
No known key found for this signature in database
GPG Key ID: 5A6ED5CBF1A0A000

View File

@ -241,53 +241,7 @@ func (m *Model) normalUpdate(msg tea.Msg) tea.Cmd {
switch msg := msg.(type) { switch msg := msg.(type) {
case tea.KeyMsg: case tea.KeyMsg:
if m.command.IsSeeking() { if m.command.IsSeeking() {
if len(msg.Runes) <= 0 { return m.seekingUpdate(msg)
m.command = &NormalCommand{}
}
if msg.Runes[0] == 'w' || msg.Runes[0] == 'W' {
m.command.Range = m.findPairRange(msg.Runes[0])
return executeCmd(*m.command)
}
switch m.command.Buffer {
case "a":
m.command.Range = m.findPairRange(msg.Runes[0])
return executeCmd(*m.command)
case "i":
pr := m.findPairRange(msg.Runes[0])
pr.Start.Col++
pr.End.Col--
m.command.Range = pr
return executeCmd(*m.command)
case "f":
end := m.findCharRight(msg.Runes[0])
m.command.Range = Range{
Start: Position{m.row, m.col},
End: end,
}
case "F":
start := m.findCharLeft(msg.Runes[0])
m.command.Range = Range{
Start: start,
End: Position{m.row, m.col},
}
case "t":
end := m.findCharRight(msg.Runes[0])
end.Col--
m.command.Range = Range{
Start: Position{m.row, m.col},
End: end,
}
case "T":
start := m.findCharLeft(msg.Runes[0])
start.Col++
m.command.Range = Range{
Start: start,
End: Position{m.row, m.col},
}
}
return executeCmd(*m.command)
} }
if m.command.Action == ActionReplace { if m.command.Action == ActionReplace {
@ -328,6 +282,15 @@ func (m *Model) normalUpdate(msg tea.Msg) tea.Cmd {
m.command.Buffer = v m.command.Buffer = v
m.command.Count = count m.command.Count = count
} }
case "o":
m.CursorEnd()
m.splitLine(m.row, m.col)
return m.SetMode(ModeInsert)
case "O":
m.CursorStart()
m.splitLine(m.row, m.col)
m.CursorUp()
return m.SetMode(ModeInsert)
case "G": case "G":
var row int var row int
if m.command.Count > 0 { if m.command.Count > 0 {
@ -335,14 +298,20 @@ func (m *Model) normalUpdate(msg tea.Msg) tea.Cmd {
} else { } else {
row = len(m.value) - 1 row = len(m.value) - 1
} }
m.row = clamp(row, 0, len(m.value)-1) m.command.Range = Range{
Start: Position{Row: row, Col: m.col},
End: Position{Row: clamp(row, 0, len(m.value)-1), Col: m.col},
}
return executeCmd(*m.command) return executeCmd(*m.command)
case "g": case "g":
if m.command.Buffer == "g" { if m.command.Buffer == "g" {
m.row = clamp(m.command.Count-1, 0, len(m.value)-1) m.command.Range = Range{
Start: Position{Row: 0, Col: m.col},
End: Position{Row: 0, Col: clamp(m.col, 0, len(m.value[0]))},
}
return executeCmd(*m.command) return executeCmd(*m.command)
} }
m.command = &NormalCommand{Buffer: "g"} m.command.Buffer = "g"
case "x": case "x":
m.command.Action = ActionDelete m.command.Action = ActionDelete
m.command.Range = Range{ m.command.Range = Range{
@ -527,8 +496,9 @@ func (m *Model) normalUpdate(msg tea.Msg) tea.Cmd {
m.deleteRange(m.command.Range) m.deleteRange(m.command.Range)
return m.SetMode(ModeInsert) return m.SetMode(ModeInsert)
case ActionMove: case ActionMove:
m.row = clamp(m.command.Range.End.Row, 0, len(m.value)-1) row := clamp(m.command.Range.End.Row, 0, len(m.value)-1)
m.col = clamp(m.command.Range.End.Col, 0, len(m.value[m.row])) m.row = row
m.col = clamp(m.command.Range.End.Col, 0, len(m.value[row]))
} }
m.command = &NormalCommand{} m.command = &NormalCommand{}
@ -539,6 +509,56 @@ func (m *Model) normalUpdate(msg tea.Msg) tea.Cmd {
return nil return nil
} }
func (m *Model) seekingUpdate(msg tea.KeyMsg) tea.Cmd {
if len(msg.Runes) <= 0 {
m.command = &NormalCommand{}
}
if msg.Runes[0] == 'w' || msg.Runes[0] == 'W' {
m.command.Range = m.findPairRange(msg.Runes[0])
return executeCmd(*m.command)
}
switch m.command.Buffer {
case "a":
m.command.Range = m.findPairRange(msg.Runes[0])
return executeCmd(*m.command)
case "i":
pr := m.findPairRange(msg.Runes[0])
pr.Start.Col++
pr.End.Col--
m.command.Range = pr
return executeCmd(*m.command)
case "f":
end := m.findCharRight(msg.Runes[0])
m.command.Range = Range{
Start: Position{m.row, m.col},
End: end,
}
case "F":
start := m.findCharLeft(msg.Runes[0])
m.command.Range = Range{
Start: start,
End: Position{m.row, m.col},
}
case "t":
end := m.findCharRight(msg.Runes[0])
end.Col--
m.command.Range = Range{
Start: Position{m.row, m.col},
End: end,
}
case "T":
start := m.findCharLeft(msg.Runes[0])
start.Col++
m.command.Range = Range{
Start: start,
End: Position{m.row, m.col},
}
}
return executeCmd(*m.command)
}
func (m *Model) findCharLeft(r rune) Position { func (m *Model) findCharLeft(r rune) Position {
col := m.col col := m.col