Skip to content

Commit

Permalink
Fix alt-left alt-right navigation
Browse files Browse the repository at this point in the history
  • Loading branch information
Verseth committed Jul 11, 2023
1 parent aaad8c4 commit dce9de4
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 29 deletions.
19 changes: 9 additions & 10 deletions document.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (

"github.com/elk-language/go-prompt/internal/bisect"
istrings "github.com/elk-language/go-prompt/internal/strings"
runewidth "github.com/mattn/go-runewidth"
"golang.org/x/exp/utf8string"
)

Expand Down Expand Up @@ -133,11 +132,11 @@ func (d *Document) FindStartOfPreviousWord() istrings.ByteIndex {
return 0
}

// Returns the string width (as visible in the terminal)
// Returns the rune count
// of the text before the cursor until the start of the previous word.
func (d *Document) FindStringWidthUntilStartOfPreviousWord() int {
func (d *Document) FindRuneCountUntilStartOfPreviousWord() istrings.RuneCount {
x := d.TextBeforeCursor()
return runewidth.StringWidth(x[d.FindStartOfPreviousWordWithSpace():])
return istrings.RuneLen(x[d.FindStartOfPreviousWordWithSpace():])
}

// FindStartOfPreviousWordWithSpace is almost the same as FindStartOfPreviousWord.
Expand Down Expand Up @@ -219,15 +218,15 @@ func (d *Document) FindEndOfCurrentWordWithSpace() istrings.ByteIndex {
return start + end
}

// Returns the string width (as visible in the terminal)
// Returns the number of runes
// of the text after the cursor until the end of the current word.
func (d *Document) FindStringWidthUntilEndOfCurrentWord() istrings.StringWidth {
func (d *Document) FindRuneCountUntilEndOfCurrentWord() istrings.RuneCount {
t := d.TextAfterCursor()
var width istrings.StringWidth
var count istrings.RuneCount
nonSpaceCharSeen := false
for _, char := range t {
if !nonSpaceCharSeen && char == ' ' {
width += 1
count += 1
continue
}

Expand All @@ -236,10 +235,10 @@ func (d *Document) FindStringWidthUntilEndOfCurrentWord() istrings.StringWidth {
}

nonSpaceCharSeen = true
width += istrings.StringWidth(runewidth.RuneWidth(char))
count += 1
}

return width
return count
}

// FindEndOfCurrentWordUntilSeparator is almost the same as FindEndOfCurrentWord.
Expand Down
8 changes: 4 additions & 4 deletions emacs.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ var emacsKeyBindings = []KeyBind{
{
Key: AltRight,
Fn: func(buf *Buffer) {
buf.CursorRight(istrings.RuneIndex(buf.Document().FindStringWidthUntilEndOfCurrentWord())) // WARN
buf.CursorRight(buf.Document().FindRuneCountUntilEndOfCurrentWord())
},
},
// Left allow: Backward one character
Expand All @@ -115,20 +115,20 @@ var emacsKeyBindings = []KeyBind{
{
Key: AltLeft,
Fn: func(buf *Buffer) {
buf.CursorLeft(istrings.RuneIndex(buf.Document().FindStringWidthUntilStartOfPreviousWord())) // WARN
buf.CursorLeft(buf.Document().FindRuneCountUntilStartOfPreviousWord())
},
},
// Cut the Word before the cursor.
{
Key: ControlW,
Fn: func(buf *Buffer) {
buf.DeleteBeforeCursor(istrings.RuneIndex(len([]rune(buf.Document().GetWordBeforeCursorWithSpace()))))
buf.DeleteBeforeCursor(istrings.RuneLen(buf.Document().GetWordBeforeCursorWithSpace()))
},
},
{
Key: AltBackspace,
Fn: func(buf *Buffer) {
buf.DeleteBeforeCursor(istrings.RuneIndex(len([]rune(buf.Document().GetWordBeforeCursorWithSpace()))))
buf.DeleteBeforeCursor(istrings.RuneLen(buf.Document().GetWordBeforeCursorWithSpace()))
},
},
// Clear the Screen, similar to the clear command
Expand Down
15 changes: 0 additions & 15 deletions key_bind_func.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,6 @@ func DeleteChar(buf *Buffer) {
buf.Delete(1)
}

// DeleteWord Delete word before the cursor
func DeleteWord(buf *Buffer) {
buf.DeleteBeforeCursor(istrings.RuneCount(len([]rune(buf.Document().TextBeforeCursor()))) - istrings.RuneCount(buf.Document().FindStartOfPreviousWordWithSpace())) // WARN
}

// DeleteBeforeChar Go to Backspace
func DeleteBeforeChar(buf *Buffer) {
buf.DeleteBeforeCursor(1)
Expand All @@ -40,13 +35,3 @@ func GoRightChar(buf *Buffer) {
func GoLeftChar(buf *Buffer) {
buf.CursorLeft(1)
}

// GoRightWord Forward one word
func GoRightWord(buf *Buffer) {
buf.CursorRight(istrings.RuneCount(buf.Document().FindEndOfCurrentWordWithSpace())) // WARN
}

// GoLeftWord Backward one word
func GoLeftWord(buf *Buffer) {
buf.CursorLeft(istrings.RuneCount(len([]rune(buf.Document().TextBeforeCursor()))) - istrings.RuneCount(buf.Document().FindStartOfPreviousWordWithSpace())) // WARN
}

0 comments on commit dce9de4

Please sign in to comment.