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

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

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

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

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

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