From 9336e095325bf85d74b182b57c450e9105716edd Mon Sep 17 00:00:00 2001 From: Zachary Yedidia Date: Wed, 16 Jan 2019 22:32:33 -0500 Subject: [PATCH] Revert "Use byte slice for insert" This reverts commit 0c844c2f5b83fda87711cbdefcad47f810d673b3. --- cmd/micro/action/actions.go | 10 +++++----- cmd/micro/action/bufhandler.go | 4 ++-- cmd/micro/action/command.go | 3 ++- cmd/micro/buffer/buffer.go | 16 +++++++++------- cmd/micro/buffer/eventhandler.go | 11 ++++++----- cmd/micro/buffer/save.go | 2 +- cmd/micro/info/history.go | 4 ++-- cmd/micro/info/infobuffer.go | 4 ++-- 8 files changed, 29 insertions(+), 25 deletions(-) diff --git a/cmd/micro/action/actions.go b/cmd/micro/action/actions.go index dc7d32d7..782962c3 100644 --- a/cmd/micro/action/actions.go +++ b/cmd/micro/action/actions.go @@ -361,14 +361,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, []byte{'\n'}) + h.Buf.Insert(h.Cursor.Loc, "\n") // h.Cursor.Right() if h.Buf.Settings["autoindent"].(bool) { if cx < len(ws) { ws = ws[0:cx] } - h.Buf.Insert(h.Cursor.Loc, ws) + h.Buf.Insert(h.Cursor.Loc, string(ws)) // for i := 0; i < len(ws); i++ { // h.Cursor.Right() // } @@ -755,10 +755,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], h.Cursor.GetSelection()) + h.Buf.Insert(h.Cursor.CurSelection[1], string(h.Cursor.GetSelection())) } else { h.Cursor.End() - h.Buf.Insert(h.Cursor.Loc, append([]byte{'\n'}, h.Buf.LineBytes(h.Cursor.Y)...)) + h.Buf.Insert(h.Cursor.Loc, "\n"+string(h.Buf.LineBytes(h.Cursor.Y))) // h.Cursor.Right() } @@ -869,7 +869,7 @@ func (h *BufHandler) paste(clip string) { h.Cursor.ResetSelection() } - h.Buf.Insert(h.Cursor.Loc, []byte(clip)) + h.Buf.Insert(h.Cursor.Loc, clip) // h.Cursor.Loc = h.Cursor.Loc.Move(Count(clip), h.Buf) h.freshClip = false InfoBar.Message("Pasted clipboard") diff --git a/cmd/micro/action/bufhandler.go b/cmd/micro/action/bufhandler.go index ede74b5f..c072a522 100644 --- a/cmd/micro/action/bufhandler.go +++ b/cmd/micro/action/bufhandler.go @@ -262,9 +262,9 @@ func (h *BufHandler) DoRuneInsert(r rune) { if h.isOverwriteMode { next := c.Loc next.X++ - h.Buf.Replace(c.Loc, next, []byte{byte(r)}) + h.Buf.Replace(c.Loc, next, string(r)) } else { - h.Buf.Insert(c.Loc, []byte{byte(r)}) + h.Buf.Insert(c.Loc, string(r)) } } } diff --git a/cmd/micro/action/command.go b/cmd/micro/action/command.go index 6f85c70c..f0503e90 100644 --- a/cmd/micro/action/command.go +++ b/cmd/micro/action/command.go @@ -571,6 +571,7 @@ func (h *BufHandler) ReplaceCmd(args []string) { } replace := []byte(args[1]) + replaceStr := args[1] var regex *regexp.Regexp var err error @@ -619,7 +620,7 @@ func (h *BufHandler) ReplaceCmd(args []string) { InfoBar.YNPrompt("Perform replacement (y,n,esc)", func(yes, canceled bool) { if !canceled && yes { - h.Buf.Replace(locs[0], locs[1], replace) + h.Buf.Replace(locs[0], locs[1], replaceStr) searchLoc = locs[0] searchLoc.X += utf8.RuneCount(replace) h.Cursor.Loc = searchLoc diff --git a/cmd/micro/buffer/buffer.go b/cmd/micro/buffer/buffer.go index f6d91a89..d02a6191 100644 --- a/cmd/micro/buffer/buffer.go +++ b/cmd/micro/buffer/buffer.go @@ -240,7 +240,7 @@ func (b *Buffer) SetName(s string) { b.name = s } -func (b *Buffer) Insert(start Loc, text []byte) { +func (b *Buffer) Insert(start Loc, text string) { b.EventHandler.cursors = b.cursors b.EventHandler.active = b.curCursor b.EventHandler.Insert(start, text) @@ -419,11 +419,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) []byte { +func (b *Buffer) IndentString(tabsize int) string { if b.Settings["tabstospaces"].(bool) { - return bytes.Repeat([]byte{' '}, tabsize) + return Spaces(tabsize) } - return []byte{'\t'} + return "\t" } // SetCursors resets this buffer's cursors to a new list @@ -529,18 +529,19 @@ func (b *Buffer) MoveLinesUp(start int, end int) { if start < 1 || start >= end || end > len(b.lines) { return } + l := string(b.LineBytes(start - 1)) if end == len(b.lines) { b.Insert( Loc{ utf8.RuneCount(b.lines[end-1].data), end - 1, }, - append([]byte{'\n'}, b.LineBytes(start-1)...), + "\n"+l, ) } else { b.Insert( Loc{0, end}, - append(b.LineBytes(start-1), '\n'), + l+"\n", ) } b.Remove( @@ -554,9 +555,10 @@ func (b *Buffer) MoveLinesDown(start int, end int) { if start < 0 || start >= end || end >= len(b.lines)-1 { return } + l := string(b.LineBytes(end)) b.Insert( Loc{0, start}, - append(b.LineBytes(end), '\n'), + l+"\n", ) end++ b.Remove( diff --git a/cmd/micro/buffer/eventhandler.go b/cmd/micro/buffer/eventhandler.go index 22a4806c..eeb7b812 100644 --- a/cmd/micro/buffer/eventhandler.go +++ b/cmd/micro/buffer/eventhandler.go @@ -88,16 +88,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(str string) { +func (eh *EventHandler) ApplyDiff(new string) { differ := dmp.New() - diff := differ.DiffMain(string(eh.buf.Bytes()), str, false) + diff := differ.DiffMain(string(eh.buf.Bytes()), new, 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, []byte(d.Text)) + eh.Insert(loc, d.Text) } loc = loc.MoveLA(utf8.RuneCountInString(d.Text), eh.buf.LineArray) } @@ -105,7 +105,8 @@ func (eh *EventHandler) ApplyDiff(str string) { } // Insert creates an insert text event and executes it -func (eh *EventHandler) Insert(start Loc, text []byte) { +func (eh *EventHandler) Insert(start Loc, textStr string) { + text := []byte(textStr) e := &TextEvent{ C: *eh.cursors[eh.active], EventType: TextEventInsert, @@ -174,7 +175,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 []byte) { +func (eh *EventHandler) Replace(start, end Loc, replace string) { eh.Remove(start, end) eh.Insert(start, replace) } diff --git a/cmd/micro/buffer/save.go b/cmd/micro/buffer/save.go index d8318499..4acf484e 100644 --- a/cmd/micro/buffer/save.go +++ b/cmd/micro/buffer/save.go @@ -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, []byte{'\n'}) + b.Insert(end, "\n") } } diff --git a/cmd/micro/info/history.go b/cmd/micro/info/history.go index 84a81e53..18e6bbba 100644 --- a/cmd/micro/info/history.go +++ b/cmd/micro/info/history.go @@ -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(), []byte(history[i.HistoryNum])) + i.Replace(i.Start(), i.End(), 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(), []byte(history[i.HistoryNum])) + i.Replace(i.Start(), i.End(), history[i.HistoryNum]) i.Buffer.GetActiveCursor().GotoLoc(i.End()) } } diff --git a/cmd/micro/info/infobuffer.go b/cmd/micro/info/infobuffer.go index 26cada21..aebdd347 100644 --- a/cmd/micro/info/infobuffer.go +++ b/cmd/micro/info/infobuffer.go @@ -110,7 +110,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(), []byte(msg)) + i.Buffer.Insert(i.Buffer.Start(), msg) } func (i *InfoBuf) YNPrompt(prompt string, donecb func(bool, bool)) { @@ -146,7 +146,7 @@ func (i *InfoBuf) DonePrompt(canceled bool) { } i.PromptCallback = nil } - i.Replace(i.Start(), i.End(), []byte{}) + i.Replace(i.Start(), i.End(), "") } if i.YNCallback != nil && hadYN { i.YNCallback(i.YNResp, canceled)