// 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
}
func (b *Buffer) String() string {
- text := ""
- if b.r.Len() != 0 {
- text = b.r.String()
- }
- return text
+ return b.Text
}
// Update fetches the string from the rope and updates the `text` and `lines` in the buffer
func (b *Buffer) Update() {
- b.Lines = strings.Split(b.String(), "\n")
+ if b.r.Len() != 0 {
+ b.Text = b.r.String()
+ } else {
+ b.Text = ""
+ }
+ b.Lines = strings.Split(b.Text, "\n")
b.NumLines = len(b.Lines)
}
if end > b.Len() {
end = b.Len()
}
- removed := b.r.Report(start+1, end-start)
+ removed := b.Text[start:end]
// The rope implenentation I am using wants indicies starting at 1 instead of 0
start++
end++
// GetSelection returns the cursor's selection
func (c *Cursor) GetSelection() string {
if c.curSelection[0] > c.curSelection[1] {
- return c.v.Buf.r.Report(c.curSelection[1]+1, c.curSelection[0]-c.curSelection[1])
+ return string([]rune(c.v.Buf.Text)[c.curSelection[1]:c.curSelection[0]])
}
- return c.v.Buf.r.Report(c.curSelection[0]+1, c.curSelection[1]-c.curSelection[0])
+ return string([]rune(c.v.Buf.Text)[c.curSelection[0]:c.curSelection[1]])
}
// SelectLine selects the current line