Imitate general ControlW behavior

This commit is contained in:
b4b4r07 2017-08-16 18:15:37 +09:00
parent a38e62febe
commit d9cc7150e6
3 changed files with 85 additions and 2 deletions

View File

@ -54,7 +54,14 @@ func (d *Document) GetWordBeforeCursor() string {
return x[d.FindStartOfPreviousWord():]
}
// FindStartOfPreviousWord return an index relative to the cursor position
// GetWordBeforeCursorWithSpace returns the word before the cursor.
// Unlike GetWordBeforeCursor, it returns string containing space
func (d *Document) GetWordBeforeCursorWithSpace() string {
x := d.TextBeforeCursor()
return x[d.FindStartOfPreviousWordWithSpace():]
}
// FindStartOfPreviousWord returns an index relative to the cursor position
// pointing to the start of the previous word. Return `None` if nothing was found.
func (d *Document) FindStartOfPreviousWord() int {
// Reverse the text before the cursor, in order to do an efficient backwards search.
@ -68,6 +75,24 @@ func (d *Document) FindStartOfPreviousWord() int {
return 0
}
// FindStartOfPreviousWordWithSpace is almost the same as FindStartOfPreviousWord.
// The only difference is to ignore contiguous spaces.
func (d *Document) FindStartOfPreviousWordWithSpace() int {
// Reverse the text before the cursor, in order to do an efficient backwards search.
x := d.TextBeforeCursor()
l := len(x)
appear := false
for i := l; i > 0; i-- {
if x[i-1:i] != " " {
appear = true
}
if x[i-1:i] == " " && appear {
return i
}
}
return 0
}
// CurrentLineBeforeCursor returns the text from the start of the line until the cursor.
func (d *Document) CurrentLineBeforeCursor() string {
s := strings.Split(d.TextBeforeCursor(), "\n")

View File

@ -88,6 +88,35 @@ func TestDocument_GetWordBeforeCursor(t *testing.T) {
}
}
func TestDocument_GetWordBeforeCursorWithSpace(t *testing.T) {
pattern := []struct {
document *Document
expected string
}{
{
document: &Document{
Text: "apple bana ",
CursorPosition: len("apple bana "),
},
expected: "bana ",
},
{
document: &Document{
Text: "apple ",
CursorPosition: len("apple "),
},
expected: "apple ",
},
}
for _, p := range pattern {
ac := p.document.GetWordBeforeCursorWithSpace()
if ac != p.expected {
t.Errorf("Should be %#v, got %#v", p.expected, ac)
}
}
}
func TestDocument_FindStartOfPreviousWord(t *testing.T) {
pattern := []struct {
document *Document
@ -117,6 +146,35 @@ func TestDocument_FindStartOfPreviousWord(t *testing.T) {
}
}
func TestDocument_FindStartOfPreviousWordWithSpace(t *testing.T) {
pattern := []struct {
document *Document
expected int
}{
{
document: &Document{
Text: "apple bana ",
CursorPosition: len("apple bana "),
},
expected: len("apple "),
},
{
document: &Document{
Text: "apple ",
CursorPosition: len("apple "),
},
expected: len(""),
},
}
for _, p := range pattern {
ac := p.document.FindStartOfPreviousWordWithSpace()
if ac != p.expected {
t.Errorf("Should be %#v, got %#v", p.expected, ac)
}
}
}
func TestDocument_CurrentLineBeforeCursor(t *testing.T) {
d := &Document{
Text: "line 1\nline 2\nline 3\nline 4\n",

View File

@ -103,7 +103,7 @@ var emacsKeyBindings = []KeyBind{
{
Key: ControlW,
Fn: func(buf *Buffer) {
buf.DeleteBeforeCursor(len([]rune(buf.Document().GetWordBeforeCursor())))
buf.DeleteBeforeCursor(len([]rune(buf.Document().GetWordBeforeCursorWithSpace())))
},
},
}