summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMateusz Czapliński <czapkofan@gmail.com>2018-12-04 21:38:58 +0100
committerMateusz Czapliński <czapkofan@gmail.com>2018-12-04 21:43:06 +0100
commit6f8d6d1da1c11c21aa7472309ce8dd9f9009ac51 (patch)
treeba6042e6d4ab2fdfc9a6e621f1737dc65f267ee7
parent748ed0c619d2e74e39580965820a05acf4e538c1 (diff)
replace yank with better insert
Previous prototype of yank was O(n*m). The new code is more effective O(n+m), and also removes the need for yank function, by extending the insert function to accept more than 1 rune at once.
-rw-r--r--up.go20
-rw-r--r--up_test.go83
2 files changed, 90 insertions, 13 deletions
diff --git a/up.go b/up.go
index cc06d1a..0e815f3 100644
--- a/up.go
+++ b/up.go
@@ -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)
+ }
+ }
+}