From 29ae8404138c471d49431c31dc7f1b9b2acff92e Mon Sep 17 00:00:00 2001 From: Zachary Yedidia Date: Sat, 7 May 2016 10:57:40 -0400 Subject: [PATCH] Use rope substring instead of report --- cmd/micro/buffer.go | 19 ++++++++++--------- cmd/micro/command.go | 2 +- cmd/micro/cursor.go | 4 ++-- cmd/micro/search.go | 2 +- 4 files changed, 14 insertions(+), 13 deletions(-) diff --git a/cmd/micro/buffer.go b/cmd/micro/buffer.go index 8fa620f0..517896a8 100644 --- a/cmd/micro/buffer.go +++ b/cmd/micro/buffer.go @@ -27,7 +27,6 @@ type Buffer struct { // Provide efficient and easy access to text and lines so the rope String does not // need to be constantly recalculated // These variables are updated in the update() function - Text string Lines []string NumLines int @@ -62,17 +61,15 @@ func (b *Buffer) UpdateRules() { } func (b *Buffer) String() string { - return b.Text + if b.r.Len() != 0 { + return b.r.String() + } + return "" } // Update fetches the string from the rope and updates the `text` and `lines` in the buffer func (b *Buffer) Update() { - if b.r.Len() != 0 { - b.Text = b.r.String() - } else { - b.Text = "" - } - b.Lines = strings.Split(b.Text, "\n") + b.Lines = strings.Split(b.String(), "\n") b.NumLines = len(b.Lines) } @@ -125,7 +122,7 @@ func (b *Buffer) Remove(start, end int) string { if end > b.Len() { end = b.Len() } - removed := b.Text[start:end] + removed := b.Substr(start, end) // The rope implenentation I am using wants indicies starting at 1 instead of 0 start++ end++ @@ -134,6 +131,10 @@ func (b *Buffer) Remove(start, end int) string { return removed } +func (b *Buffer) Substr(start, end int) string { + return b.r.Substr(start+1, end-start).String() +} + // Len gives the length of the buffer func (b *Buffer) Len() int { return b.r.Len() diff --git a/cmd/micro/command.go b/cmd/micro/command.go index e5565a37..1e41a7b2 100644 --- a/cmd/micro/command.go +++ b/cmd/micro/command.go @@ -141,7 +141,7 @@ func HandleCommand(input string, view *View) { found := false for { - match := regex.FindStringIndex(view.Buf.Text) + match := regex.FindStringIndex(view.Buf.String()) if match == nil { break } diff --git a/cmd/micro/cursor.go b/cmd/micro/cursor.go index bba0f2ff..5eb43b50 100644 --- a/cmd/micro/cursor.go +++ b/cmd/micro/cursor.go @@ -102,9 +102,9 @@ func (c *Cursor) DeleteSelection() { // GetSelection returns the cursor's selection func (c *Cursor) GetSelection() string { if c.curSelection[0] > c.curSelection[1] { - return string([]rune(c.v.Buf.Text)[c.curSelection[1]:c.curSelection[0]]) + return c.v.Buf.Substr(c.curSelection[1], c.curSelection[0]) } - return string([]rune(c.v.Buf.Text)[c.curSelection[0]:c.curSelection[1]]) + return c.v.Buf.Substr(c.curSelection[0], c.curSelection[1]) } // SelectLine selects the current line diff --git a/cmd/micro/search.go b/cmd/micro/search.go index 041718ac..4253a316 100644 --- a/cmd/micro/search.go +++ b/cmd/micro/search.go @@ -76,7 +76,7 @@ func Search(searchStr string, v *View, down bool) { } var str string var charPos int - text := v.Buf.Text + text := v.Buf.String() if down { str = text[searchStart:] charPos = searchStart