Skip to content

Commit e82090c

Browse files
committed
Add transpose
1 parent 3070907 commit e82090c

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

textarea/textarea.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ type KeyMap struct {
5050
UppercaseWordForward key.Binding
5151
LowercaseWordForward key.Binding
5252
CapitalizeWordForward key.Binding
53+
54+
TransposeCharacterBackward key.Binding
5355
}
5456

5557
// DefaultKeyMap is the default set of key bindings for navigating and acting
@@ -75,6 +77,8 @@ var DefaultKeyMap = KeyMap{
7577
CapitalizeWordForward: key.NewBinding(key.WithKeys("alt+c")),
7678
LowercaseWordForward: key.NewBinding(key.WithKeys("alt+l")),
7779
UppercaseWordForward: key.NewBinding(key.WithKeys("alt+u")),
80+
81+
TransposeCharacterBackward: key.NewBinding(key.WithKeys("ctrl+t")),
7882
}
7983

8084
// LineInfo is a helper for keeping track of line information regarding
@@ -481,6 +485,24 @@ func (m *Model) deleteAfterCursor() {
481485
m.SetCursor(len(m.value[m.row]))
482486
}
483487

488+
// transposeLeft exchanges the runes at the cursor and immediately
489+
// before. No-op if the cursor is at the beginning of the line. If
490+
// the cursor is not at the end of the line yet, moves the cursor to
491+
// the right.
492+
func (m *Model) transposeLeft() {
493+
if m.col == 0 || len(m.value[m.row]) < 2 {
494+
return
495+
}
496+
if m.col >= len(m.value[m.row]) {
497+
m.SetCursor(m.col - 1)
498+
}
499+
m.value[m.row][m.col-1], m.value[m.row][m.col] =
500+
m.value[m.row][m.col], m.value[m.row][m.col-1]
501+
if m.col < len(m.value[m.row]) {
502+
m.SetCursor(m.col + 1)
503+
}
504+
}
505+
484506
// deleteWordLeft deletes the word left to the cursor. Returns whether or not
485507
// the cursor blink should be reset.
486508
func (m *Model) deleteWordLeft() {
@@ -847,6 +869,8 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
847869
m.uppercaseRight()
848870
case key.Matches(msg, m.KeyMap.CapitalizeWordForward):
849871
m.capitalizeRight()
872+
case key.Matches(msg, m.KeyMap.TransposeCharacterBackward):
873+
m.transposeLeft()
850874

851875
default:
852876
if m.CharLimit > 0 && rw.StringWidth(m.Value()) >= m.CharLimit {

0 commit comments

Comments
 (0)