From d9ab074276f1e6f352c488d571693ba5037bc952 Mon Sep 17 00:00:00 2001 From: Zachary Yedidia Date: Sun, 27 Mar 2016 12:27:26 -0400 Subject: [PATCH] Fix cursor bug --- src/cursor.go | 9 ++++----- src/rope.go | 11 ++++++----- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/cursor.go b/src/cursor.go index 254a02e0..09497d90 100644 --- a/src/cursor.go +++ b/src/cursor.go @@ -1,6 +1,8 @@ package main import ( + "io/ioutil" + "strconv" "strings" ) @@ -9,14 +11,11 @@ func FromCharPos(loc int, buf *Buffer) (int, int) { charNum := 0 x, y := 0, 0 - for charNum+Count(buf.lines[y])+1 < loc { + for charNum+Count(buf.lines[y])+1 <= loc { charNum += Count(buf.lines[y]) + 1 y++ } - - for charNum+x < loc { - x++ - } + x = loc - charNum return x, y } diff --git a/src/rope.go b/src/rope.go index d1cecd48..90fff63c 100644 --- a/src/rope.go +++ b/src/rope.go @@ -5,9 +5,9 @@ import ( ) const ( - // RopeSplitLength defines how large can a string be before it is split into two nodes - RopeSplitLength = 1000 - // RopeJoinLength defines how short can a string be before it is joined + // RopeSplitLength is the threshold used to split a leaf node into two child nodes. + RopeSplitLength = 1000000000 + // RopeJoinLength is the threshold used to join two child nodes into one leaf node. RopeJoinLength = 500 // RopeRebalanceRatio = 1.2 ) @@ -39,8 +39,9 @@ func (r *Rope) Adjust() { if !r.valueNil { if r.len > RopeSplitLength { divide := int(math.Floor(float64(r.len) / 2)) - r.left = NewRope(r.value[:divide]) - r.right = NewRope(r.value[divide:]) + runes := []rune(r.value) + r.left = NewRope(string(runes[:divide])) + r.right = NewRope(string(runes[divide:])) r.valueNil = true } } else {