diff options
-rw-r--r-- | up.go | 20 | ||||
-rw-r--r-- | up_test.go | 83 |
2 files changed, 90 insertions, 13 deletions
@@ -350,7 +350,7 @@ func (e *Editor) HandleKey(ev *tcell.EventKey) bool { e.kill() case key(tcell.KeyCtrlY), ctrlKey(tcell.KeyCtrlY): - e.yank() + e.insert(e.killspace...) default: // Unknown key/combination, not handled return false @@ -358,12 +358,12 @@ func (e *Editor) HandleKey(ev *tcell.EventKey) bool { return true } -func (e *Editor) insert(ch rune) { - // Insert character into value (https://github.com/golang/go/wiki/SliceTricks#insert) - e.value = append(e.value, 0) - copy(e.value[e.cursor+1:], e.value[e.cursor:]) - e.value[e.cursor] = ch - e.cursor++ +func (e *Editor) insert(ch ...rune) { + // Based on https://github.com/golang/go/wiki/SliceTricks#insert + e.value = append(e.value, ch...) // = PREFIX + SUFFIX + (filler) + copy(e.value[e.cursor+len(ch):], e.value[e.cursor:]) // = PREFIX + (filler) + SUFFIX + copy(e.value[e.cursor:], ch) // = PREFIX + ch + SUFFIX + e.cursor += len(ch) } func (e *Editor) delete(dx int) { @@ -382,12 +382,6 @@ func (e *Editor) kill() { e.value = e.value[:e.cursor] } -func (e *Editor) yank() { - for _, r := range e.killspace { - e.insert(r) - } -} - type BufView struct { // TODO: Wrap bool Y int // Y of the view in the Buf, for down/up scrolling diff --git a/up_test.go b/up_test.go new file mode 100644 index 0000000..de31c59 --- /dev/null +++ b/up_test.go @@ -0,0 +1,83 @@ +package main + +import "testing" + +func Test_Editor_insert(test *testing.T) { + cases := []struct { + comment string + e Editor + insert []rune + wantValue []rune + }{ + { + comment: "prepend ASCII char", + e: Editor{ + value: []rune(`abc`), + cursor: 0, + }, + insert: []rune{'X'}, + wantValue: []rune(`Xabc`), + }, + { + comment: "prepend UTF char", + e: Editor{ + value: []rune(`abc`), + cursor: 0, + }, + insert: []rune{'☃'}, + wantValue: []rune(`☃abc`), + }, + { + comment: "insert ASCII char", + e: Editor{ + value: []rune(`abc`), + cursor: 1, + }, + insert: []rune{'X'}, + wantValue: []rune(`aXbc`), + }, + { + comment: "insert UTF char", + e: Editor{ + value: []rune(`abc`), + cursor: 1, + }, + insert: []rune{'☃'}, + wantValue: []rune(`a☃bc`), + }, + { + comment: "append ASCII char", + e: Editor{ + value: []rune(`abc`), + cursor: 3, + }, + insert: []rune{'X'}, + wantValue: []rune(`abcX`), + }, + { + comment: "append UTF char", + e: Editor{ + value: []rune(`abc`), + cursor: 3, + }, + insert: []rune{'☃'}, + wantValue: []rune(`abc☃`), + }, + { + comment: "insert 2 ASCII chars", + e: Editor{ + value: []rune(`abc`), + cursor: 1, + }, + insert: []rune{'X', 'Y'}, + wantValue: []rune(`aXYbc`), + }, + } + + for _, c := range cases { + c.e.insert(c.insert...) + if string(c.e.value) != string(c.wantValue) { + test.Errorf("%q: bad value\nwant: %q\nhave: %q", c.comment, c.wantValue, c.e.value) + } + } +} |