Use byte slice for insert

This commit is contained in:
Zachary Yedidia
2019-01-14 22:29:24 -05:00
parent 812c7761dc
commit 1563ab93dd
7 changed files with 22 additions and 22 deletions

View File

@@ -1,6 +1,7 @@
package buffer
import (
"bytes"
"crypto/md5"
"errors"
"io"
@@ -232,7 +233,7 @@ func (b *Buffer) SetName(s string) {
b.name = s
}
func (b *Buffer) Insert(start Loc, text string) {
func (b *Buffer) Insert(start Loc, text []byte) {
b.EventHandler.cursors = b.cursors
b.EventHandler.Insert(start, text)
}
@@ -409,11 +410,11 @@ func (b *Buffer) ClearMatches() {
// IndentString returns this buffer's indent method (a tabstop or n spaces
// depending on the settings)
func (b *Buffer) IndentString(tabsize int) string {
func (b *Buffer) IndentString(tabsize int) []byte {
if b.Settings["tabstospaces"].(bool) {
return Spaces(tabsize)
return bytes.Repeat([]byte{' '}, tabsize)
}
return "\t"
return []byte{'\t'}
}
// SetCursors resets this buffer's cursors to a new list

View File

@@ -87,16 +87,16 @@ func NewEventHandler(buf *SharedBuffer, cursors []*Cursor) *EventHandler {
// the buffer equal to that string
// This means that we can transform the buffer into any string and still preserve undo/redo
// through insert and delete events
func (eh *EventHandler) ApplyDiff(new string) {
func (eh *EventHandler) ApplyDiff(str string) {
differ := dmp.New()
diff := differ.DiffMain(string(eh.buf.Bytes()), new, false)
diff := differ.DiffMain(string(eh.buf.Bytes()), str, false)
loc := eh.buf.Start()
for _, d := range diff {
if d.Type == dmp.DiffDelete {
eh.Remove(loc, loc.MoveLA(utf8.RuneCountInString(d.Text), eh.buf.LineArray))
} else {
if d.Type == dmp.DiffInsert {
eh.Insert(loc, d.Text)
eh.Insert(loc, []byte(d.Text))
}
loc = loc.MoveLA(utf8.RuneCountInString(d.Text), eh.buf.LineArray)
}
@@ -104,8 +104,7 @@ func (eh *EventHandler) ApplyDiff(new string) {
}
// Insert creates an insert text event and executes it
func (eh *EventHandler) Insert(start Loc, textStr string) {
text := []byte(textStr)
func (eh *EventHandler) Insert(start Loc, text []byte) {
e := &TextEvent{
C: *eh.cursors[0],
EventType: TextEventInsert,
@@ -174,7 +173,7 @@ func (eh *EventHandler) MultipleReplace(deltas []Delta) {
}
// Replace deletes from start to end and replaces it with the given string
func (eh *EventHandler) Replace(start, end Loc, replace string) {
func (eh *EventHandler) Replace(start, end Loc, replace []byte) {
eh.Remove(start, end)
eh.Insert(start, replace)
}

View File

@@ -67,7 +67,7 @@ func (b *Buffer) SaveAs(filename string) error {
if b.Settings["eofnewline"].(bool) {
end := b.End()
if b.RuneAt(Loc{end.X - 1, end.Y}) != '\n' {
b.Insert(end, "\n")
b.Insert(end, []byte{'\n'})
}
}