Skip to content

Commit

Permalink
feat: map editor vimlike controls w/e/b
Browse files Browse the repository at this point in the history
for moving at word boundaries
  • Loading branch information
ja-he committed Jul 1, 2024
1 parent f7efaca commit cb07c49
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 23 deletions.
3 changes: 3 additions & 0 deletions internal/control/cli/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ func NewController(
"<right>": "move-cursor-rune-right",
"0": "move-cursor-to-beginning",
"$": "move-cursor-to-end",
"w": "move-cursor-to-next-word-beginning",
"b": "move-cursor-to-prev-word-beginning",
"e": "move-cursor-to-next-word-end",
"<ESC>": "quit",
"D": "delete-to-end",
"d$": "delete-to-end",
Expand Down
49 changes: 26 additions & 23 deletions internal/control/edit/editors/string_editor.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ type StringEditorControl interface {
MoveCursorLeft()
MoveCursorRight()
MoveCursorRightA()
MoveCursorNextWordBeginning()
MoveCursorPrevWordBeginning()
MoveCursorNextWordEnd()
MoveCursorToNextWordBeginning()
MoveCursorToPrevWordBeginning()
MoveCursorToNextWordEnd()
AddRune(newRune rune)
}

Expand Down Expand Up @@ -161,9 +161,9 @@ func (e *StringEditor) MoveCursorRight() {
}
}

// MoveCursorNextWordBeginning moves the cursor one rune to the right, or to
// MoveCursorToNextWordBeginning moves the cursor one rune to the right, or to
// the end of the string if already at the end.
func (e *StringEditor) MoveCursorNextWordBeginning() {
func (e *StringEditor) MoveCursorToNextWordBeginning() {
if len([]rune(e.Content)) == 0 {
e.CursorPos = 0
return
Expand All @@ -185,9 +185,9 @@ func (e *StringEditor) MoveCursorNextWordBeginning() {
}
}

// MoveCursorPrevWordBeginning moves the cursor one rune to the left, or to the
// MoveCursorToPrevWordBeginning moves the cursor one rune to the left, or to the
// beginning of the string if already at the beginning.
func (e *StringEditor) MoveCursorPrevWordBeginning() {
func (e *StringEditor) MoveCursorToPrevWordBeginning() {
nameBeforeCursor := []rune(e.Content)[:e.CursorPos]
if len(nameBeforeCursor) == 0 {
return
Expand All @@ -202,8 +202,8 @@ func (e *StringEditor) MoveCursorPrevWordBeginning() {
e.CursorPos = i
}

// MoveCursorNextWordEnd moves the cursor to the end of the next word.
func (e *StringEditor) MoveCursorNextWordEnd() {
// MoveCursorToNextWordEnd moves the cursor to the end of the next word.
func (e *StringEditor) MoveCursorToNextWordEnd() {
nameAfterCursor := []rune(e.Content)[e.CursorPos:]
if len(nameAfterCursor) == 0 {
return
Expand Down Expand Up @@ -272,20 +272,23 @@ func (e *StringEditor) CreateInputProcessor(cfg input.InputConfig) (input.ModalI
var exitInsertMode func()

actionspecToFunc := map[input.Actionspec]func(){
"move-cursor-rune-left": e.MoveCursorLeft,
"move-cursor-rune-right": e.MoveCursorRight,
"move-cursor-to-beginning": e.MoveCursorToBeginning,
"move-cursor-to-end": e.MoveCursorToEnd,
"write": e.Write,
"quit": e.Quit,
"backspace": e.BackspaceRune,
"backspace-to-beginning": e.BackspaceToBeginning,
"delete-rune": e.DeleteRune,
"delete-rune-and-insert": func() { e.DeleteRune(); enterInsertMode() },
"delete-to-end": e.DeleteToEnd,
"delete-to-end-and-insert": func() { e.DeleteToEnd(); enterInsertMode() },
"swap-mode-insert": func() { enterInsertMode() },
"swap-mode-normal": func() { exitInsertMode() },
"move-cursor-rune-left": e.MoveCursorLeft,
"move-cursor-rune-right": e.MoveCursorRight,
"move-cursor-to-beginning": e.MoveCursorToBeginning,
"move-cursor-to-end": e.MoveCursorToEnd,
"move-cursor-to-next-word-beginning": e.MoveCursorToNextWordBeginning,
"move-cursor-to-prev-word-beginning": e.MoveCursorToPrevWordBeginning,
"move-cursor-to-next-word-end": e.MoveCursorToNextWordEnd,
"write": e.Write,
"quit": e.Quit,
"backspace": e.BackspaceRune,
"backspace-to-beginning": e.BackspaceToBeginning,
"delete-rune": e.DeleteRune,
"delete-rune-and-insert": func() { e.DeleteRune(); enterInsertMode() },
"delete-to-end": e.DeleteToEnd,
"delete-to-end-and-insert": func() { e.DeleteToEnd(); enterInsertMode() },
"swap-mode-insert": func() { enterInsertMode() },
"swap-mode-normal": func() { exitInsertMode() },
}

normalModeMappings := map[input.Keyspec]action.Action{}
Expand Down

0 comments on commit cb07c49

Please sign in to comment.