Skip to content

Commit

Permalink
Make wordLeft/wordRight find words on the prev/next line
Browse files Browse the repository at this point in the history
... if there's no word on the current line to find any more.
  • Loading branch information
knz committed Aug 14, 2022
1 parent b3daf40 commit 4e9f9fa
Showing 1 changed file with 20 additions and 33 deletions.
53 changes: 20 additions & 33 deletions textarea/textarea.go
Original file line number Diff line number Diff line change
Expand Up @@ -560,11 +560,15 @@ func (m *Model) characterRight() {
}

// characterLeft moves the cursor one character to the left.
func (m *Model) characterLeft() {
// If insideLine is set, the cursor is moved to the last
// character in the previous line, instead of one past that.
func (m *Model) characterLeft(insideLine bool) {
if m.col == 0 && m.row != 0 {
m.row--
m.CursorEnd()
return
if !insideLine {
return
}
}
if m.col > 0 {
m.SetCursor(m.col - 1)
Expand All @@ -575,55 +579,38 @@ func (m *Model) characterLeft() {
// cursor blink should be reset. If input is masked, move input to the start
// so as not to reveal word breaks in the masked input.
func (m *Model) wordLeft() {
if m.col == 0 || len(m.value[m.row]) == 0 {
return
}

i := m.col - 1
for i >= 0 {
if unicode.IsSpace(m.value[m.row][min(i, len(m.value[m.row])-1)]) {
m.SetCursor(m.col - 1)
i--
} else {
for {
m.characterLeft(true /* insideLine */)
if m.col < len(m.value[m.row]) && !unicode.IsSpace(m.value[m.row][m.col]) {
break
}
}

for i >= 0 {
if !unicode.IsSpace(m.value[m.row][min(i, len(m.value[m.row])-1)]) {
m.SetCursor(m.col - 1)
i--
} else {
for m.col > 0 {
if unicode.IsSpace(m.value[m.row][m.col-1]) {
break
}
m.SetCursor(m.col - 1)
}
}

// wordRight moves the cursor one word to the right. Returns whether or not the
// cursor blink should be reset. If the input is masked, move input to the end
// so as not to reveal word breaks in the masked input.
func (m *Model) wordRight() {
if m.col >= len(m.value[m.row]) || len(m.value[m.row]) == 0 {
return
}

i := m.col
for i < len(m.value[m.row]) {
if unicode.IsSpace(m.value[m.row][i]) {
m.SetCursor(m.col + 1)
i++
} else {
// Skip spaces forward.
for {
if m.col < len(m.value[m.row]) && !unicode.IsSpace(m.value[m.row][m.col]) {
break
}
m.characterRight()
}

for i < len(m.value[m.row]) {
if !unicode.IsSpace(m.value[m.row][i]) {
m.SetCursor(m.col + 1)
i++
} else {
for m.col < len(m.value[m.row]) {
if unicode.IsSpace(m.value[m.row][m.col]) {
break
}
m.SetCursor(m.col + 1)
}
}

Expand Down Expand Up @@ -806,7 +793,7 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
case key.Matches(msg, m.KeyMap.Paste):
return m, Paste
case key.Matches(msg, m.KeyMap.CharacterBackward):
m.characterLeft()
m.characterLeft(false /* insideLine */)
case key.Matches(msg, m.KeyMap.LinePrevious):
m.CursorUp()
case key.Matches(msg, m.KeyMap.WordBackward):
Expand Down

0 comments on commit 4e9f9fa

Please sign in to comment.