Fix GetWordBeforeCursor

This commit is contained in:
c-bata 2017-07-09 23:16:08 +09:00
parent 68147da8e9
commit 7c7813e5e1
5 changed files with 107 additions and 48 deletions

44
main.go
View File

@ -79,31 +79,33 @@ func main() {
}
// Display completions
out.SetColor("white", "teal")
if w := buffer.Document().GetWordBeforeCursor(); w != "" {
out.SetColor("white", "teal")
out.CursorDown(1)
out.Write([]byte(" Foo "))
out.SetColor("white", "darkGray")
out.Write([]byte(" "))
out.SetColor("white", "teal")
out.CursorBackward(len("foo") + 3)
out.CursorDown(1)
out.Write([]byte(" Foo "))
out.SetColor("white", "darkGray")
out.Write([]byte(" "))
out.SetColor("white", "teal")
out.CursorBackward(len("foo") + 3)
out.CursorDown(1)
out.Write([]byte(" Hello "))
out.SetColor("white", "darkGray")
out.Write([]byte(" "))
out.SetColor("white", "teal")
out.CursorBackward(len("Hello") + 3)
out.CursorDown(1)
out.Write([]byte(" Hello "))
out.SetColor("white", "darkGray")
out.Write([]byte(" "))
out.SetColor("white", "teal")
out.CursorBackward(len("Hello") + 3)
out.CursorDown(1)
out.Write([]byte(" World "))
out.SetColor("white", "darkGray")
out.Write([]byte(" "))
out.SetColor("white", "teal")
out.CursorBackward(len("World") + 3)
out.CursorDown(1)
out.Write([]byte(" World "))
out.SetColor("white", "darkGray")
out.Write([]byte(" "))
out.SetColor("white", "teal")
out.CursorBackward(len("World") + 3)
out.CursorUp(3)
out.SetColor("default", "default")
out.CursorUp(3)
out.SetColor("default", "default")
}
out.Flush()
}

View File

@ -32,9 +32,15 @@ func TestBuffer_CursorMovement(t *testing.T) {
b := NewBuffer()
b.InsertText("some_text", false, true)
b.CursorLeft(1)
b.CursorLeft(2)
b.CursorRight(1)
if l := b.CursorLeft(1); l != 1 {
t.Errorf("Should be 1, but got %d", l)
}
if l := b.CursorLeft(2); l != 2 {
t.Errorf("Should be 2, but got %d", l)
}
if l := b.CursorRight(1); l != 1 {
t.Errorf("Should be 1, but got %d", l)
}
b.InsertText("A", false, true)
if b.Text() != "some_teAxt" {
t.Errorf("Text should be %#v, got %#v", "some_teAxt", b.Text())

View File

@ -51,7 +51,7 @@ func (d *Document) FindStartOfPreviousWord() int {
// Reverse the text before the cursor, in order to do an efficient backwards search.
x := d.TextBeforeCursor()
l := len(x)
for i := l - 1; i > 0; i-- {
for i := l; i > 0; i-- {
if x[i-1:i] == " " {
return i
}

View File

@ -31,38 +31,89 @@ func TestDocument_TextBeforeCursor(t *testing.T) {
}
func TestDocument_TextAfterCursor(t *testing.T) {
d := &Document{
Text: "line 1\nline 2\nline 3\nline 4\n",
CursorPosition: len("line 1\n" + "lin"),
pattern := []struct{
document *Document
expected string
} {
{
document: &Document{
Text: "line 1\nline 2\nline 3\nline 4\n",
CursorPosition: len("line 1\n" + "lin"),
},
expected: "e 2\nline 3\nline 4\n",
},
{
document: &Document{
Text: "",
CursorPosition: 0,
},
expected: "",
},
}
ac := d.TextAfterCursor()
ex := "e 2\nline 3\nline 4\n"
if ac != ex {
t.Errorf("Should be %#v, got %#v", ex, ac)
for _, p := range pattern {
ac := p.document.TextAfterCursor()
if ac != p.expected {
t.Errorf("Should be %#v, got %#v", p.expected, ac)
}
}
}
func TestDocument_GetWordBeforeCursor(t *testing.T) {
d := &Document{
Text: "apple bana",
CursorPosition: len("apple bana"),
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: "",
},
}
ac := d.GetWordBeforeCursor()
ex := "bana"
if ac != ex {
t.Errorf("Should be %#v, got %#v", ex, ac)
for _, p := range pattern {
ac := p.document.GetWordBeforeCursor()
if ac != p.expected {
t.Errorf("Should be %#v, got %#v", p.expected, ac)
}
}
}
func TestDocument_FindStartOfPreviousWord(t *testing.T) {
d := &Document{
Text: "apple bana",
CursorPosition: len("apple bana"),
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("apple "),
},
}
ac := d.FindStartOfPreviousWord()
ex := len("apple ")
if ac != ex {
t.Errorf("Should be %#v, got %#v", ex, ac)
for _, p := range pattern {
ac := p.document.FindStartOfPreviousWord()
if ac != p.expected {
t.Errorf("Should be %#v, got %#v", p.expected, ac)
}
}
}

View File

@ -114,7 +114,7 @@ func (w *VT100Writer) CursorDown(n int) {
func (w *VT100Writer) CursorForward(n int) {
if n < 0 {
w.CursorBackward(n)
w.CursorBackward(-n)
return
}
s := strconv.Itoa(n)
@ -126,7 +126,7 @@ func (w *VT100Writer) CursorForward(n int) {
func (w *VT100Writer) CursorBackward(n int) {
if n < 0 {
w.CursorForward(n)
w.CursorForward(-n)
return
}
s := strconv.Itoa(n)