From 7c7813e5e1811223a283347f6314e38640bc6c24 Mon Sep 17 00:00:00 2001 From: c-bata Date: Sun, 9 Jul 2017 23:16:08 +0900 Subject: [PATCH] Fix GetWordBeforeCursor --- main.go | 44 +++++++++---------- prompt/buffer_test.go | 12 ++++-- prompt/document.go | 2 +- prompt/document_test.go | 93 +++++++++++++++++++++++++++++++---------- prompt/vt100_output.go | 4 +- 5 files changed, 107 insertions(+), 48 deletions(-) diff --git a/main.go b/main.go index 8778dc0..accfb5a 100644 --- a/main.go +++ b/main.go @@ -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() } diff --git a/prompt/buffer_test.go b/prompt/buffer_test.go index 63d8ac2..a70f397 100644 --- a/prompt/buffer_test.go +++ b/prompt/buffer_test.go @@ -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()) diff --git a/prompt/document.go b/prompt/document.go index 97c9d9d..f159395 100644 --- a/prompt/document.go +++ b/prompt/document.go @@ -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 } diff --git a/prompt/document_test.go b/prompt/document_test.go index 9b3d7ac..635a610 100644 --- a/prompt/document_test.go +++ b/prompt/document_test.go @@ -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) + } } } diff --git a/prompt/vt100_output.go b/prompt/vt100_output.go index caf4e38..747fc0d 100644 --- a/prompt/vt100_output.go +++ b/prompt/vt100_output.go @@ -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)