Imitate general ControlW behavior
This commit is contained in:
parent
a38e62febe
commit
d9cc7150e6
27
document.go
27
document.go
|
@ -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")
|
||||
|
|
|
@ -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",
|
||||
|
|
Loading…
Reference in New Issue