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

@@ -386,14 +386,14 @@ func (h *BufHandler) InsertNewline() bool {
ws := util.GetLeadingWhitespace(h.Buf.LineBytes(h.Cursor.Y))
cx := h.Cursor.X
h.Buf.Insert(h.Cursor.Loc, "\n")
h.Buf.Insert(h.Cursor.Loc, []byte{'\n'})
// h.Cursor.Right()
if h.Buf.Settings["autoindent"].(bool) {
if cx < len(ws) {
ws = ws[0:cx]
}
h.Buf.Insert(h.Cursor.Loc, string(ws))
h.Buf.Insert(h.Cursor.Loc, ws)
// for i := 0; i < len(ws); i++ {
// h.Cursor.Right()
// }
@@ -733,10 +733,10 @@ func (h *BufHandler) Cut() bool {
// DuplicateLine duplicates the current line or selection
func (h *BufHandler) DuplicateLine() bool {
if h.Cursor.HasSelection() {
h.Buf.Insert(h.Cursor.CurSelection[1], string(h.Cursor.GetSelection()))
h.Buf.Insert(h.Cursor.CurSelection[1], h.Cursor.GetSelection())
} else {
h.Cursor.End()
h.Buf.Insert(h.Cursor.Loc, "\n"+string(h.Buf.LineBytes(h.Cursor.Y)))
h.Buf.Insert(h.Cursor.Loc, append([]byte{'\n'}, h.Buf.LineBytes(h.Cursor.Y)...))
// h.Cursor.Right()
}
@@ -794,7 +794,7 @@ func (h *BufHandler) paste(clip string) {
h.Cursor.ResetSelection()
}
h.Buf.Insert(h.Cursor.Loc, clip)
h.Buf.Insert(h.Cursor.Loc, []byte(clip))
// h.Cursor.Loc = h.Cursor.Loc.Move(Count(clip), h.Buf)
h.freshClip = false
InfoBar.Message("Pasted clipboard")

View File

@@ -261,9 +261,9 @@ func (h *BufHandler) DoRuneInsert(r rune) {
if h.isOverwriteMode {
next := c.Loc
next.X++
h.Buf.Replace(c.Loc, next, string(r))
h.Buf.Replace(c.Loc, next, []byte{byte(r)})
} else {
h.Buf.Insert(c.Loc, string(r))
h.Buf.Insert(c.Loc, []byte{byte(r)})
}
}
}

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'})
}
}

View File

@@ -64,7 +64,7 @@ func (i *InfoBuf) SaveHistory() {
func (i *InfoBuf) UpHistory(history []string) {
if i.HistoryNum > 0 {
i.HistoryNum--
i.Replace(i.Start(), i.End(), history[i.HistoryNum])
i.Replace(i.Start(), i.End(), []byte(history[i.HistoryNum]))
i.Buffer.GetActiveCursor().GotoLoc(i.End())
}
}
@@ -73,7 +73,7 @@ func (i *InfoBuf) UpHistory(history []string) {
func (i *InfoBuf) DownHistory(history []string) {
if i.HistoryNum < len(history)-1 {
i.HistoryNum++
i.Replace(i.Start(), i.End(), history[i.HistoryNum])
i.Replace(i.Start(), i.End(), []byte(history[i.HistoryNum]))
i.Buffer.GetActiveCursor().GotoLoc(i.End())
}
}

View File

@@ -111,7 +111,7 @@ func (i *InfoBuf) Prompt(prompt string, msg string, ptype string, eventcb func(s
i.HasGutter = false
i.PromptCallback = donecb
i.EventCallback = eventcb
i.Buffer.Insert(i.Buffer.Start(), msg)
i.Buffer.Insert(i.Buffer.Start(), []byte(msg))
}
func (i *InfoBuf) YNPrompt(prompt string, donecb func(bool, bool)) {
@@ -151,7 +151,7 @@ func (i *InfoBuf) DonePrompt(canceled bool) {
i.PromptCallback = nil
i.EventCallback = nil
i.YNCallback = nil
i.Replace(i.Start(), i.End(), "")
i.Replace(i.Start(), i.End(), []byte{})
}
// Reset resets the infobuffer's msg and info