Revert "Use byte slice for insert"

This reverts commit 0c844c2f5b.
This commit is contained in:
Zachary Yedidia
2019-01-16 22:32:33 -05:00
parent 069f7d20bc
commit 9336e09532
8 changed files with 29 additions and 25 deletions

View File

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

View File

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

View File

@@ -571,6 +571,7 @@ func (h *BufHandler) ReplaceCmd(args []string) {
} }
replace := []byte(args[1]) replace := []byte(args[1])
replaceStr := args[1]
var regex *regexp.Regexp var regex *regexp.Regexp
var err error var err error
@@ -619,7 +620,7 @@ func (h *BufHandler) ReplaceCmd(args []string) {
InfoBar.YNPrompt("Perform replacement (y,n,esc)", func(yes, canceled bool) { InfoBar.YNPrompt("Perform replacement (y,n,esc)", func(yes, canceled bool) {
if !canceled && yes { if !canceled && yes {
h.Buf.Replace(locs[0], locs[1], replace) h.Buf.Replace(locs[0], locs[1], replaceStr)
searchLoc = locs[0] searchLoc = locs[0]
searchLoc.X += utf8.RuneCount(replace) searchLoc.X += utf8.RuneCount(replace)
h.Cursor.Loc = searchLoc h.Cursor.Loc = searchLoc

View File

@@ -240,7 +240,7 @@ func (b *Buffer) SetName(s string) {
b.name = s 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.cursors = b.cursors
b.EventHandler.active = b.curCursor b.EventHandler.active = b.curCursor
b.EventHandler.Insert(start, text) 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 // IndentString returns this buffer's indent method (a tabstop or n spaces
// depending on the settings) // depending on the settings)
func (b *Buffer) IndentString(tabsize int) []byte { func (b *Buffer) IndentString(tabsize int) string {
if b.Settings["tabstospaces"].(bool) { 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 // 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) { if start < 1 || start >= end || end > len(b.lines) {
return return
} }
l := string(b.LineBytes(start - 1))
if end == len(b.lines) { if end == len(b.lines) {
b.Insert( b.Insert(
Loc{ Loc{
utf8.RuneCount(b.lines[end-1].data), utf8.RuneCount(b.lines[end-1].data),
end - 1, end - 1,
}, },
append([]byte{'\n'}, b.LineBytes(start-1)...), "\n"+l,
) )
} else { } else {
b.Insert( b.Insert(
Loc{0, end}, Loc{0, end},
append(b.LineBytes(start-1), '\n'), l+"\n",
) )
} }
b.Remove( b.Remove(
@@ -554,9 +555,10 @@ func (b *Buffer) MoveLinesDown(start int, end int) {
if start < 0 || start >= end || end >= len(b.lines)-1 { if start < 0 || start >= end || end >= len(b.lines)-1 {
return return
} }
l := string(b.LineBytes(end))
b.Insert( b.Insert(
Loc{0, start}, Loc{0, start},
append(b.LineBytes(end), '\n'), l+"\n",
) )
end++ end++
b.Remove( b.Remove(

View File

@@ -88,16 +88,16 @@ func NewEventHandler(buf *SharedBuffer, cursors []*Cursor) *EventHandler {
// the buffer equal to that string // the buffer equal to that string
// This means that we can transform the buffer into any string and still preserve undo/redo // This means that we can transform the buffer into any string and still preserve undo/redo
// through insert and delete events // through insert and delete events
func (eh *EventHandler) ApplyDiff(str string) { func (eh *EventHandler) ApplyDiff(new string) {
differ := dmp.New() 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() loc := eh.buf.Start()
for _, d := range diff { for _, d := range diff {
if d.Type == dmp.DiffDelete { if d.Type == dmp.DiffDelete {
eh.Remove(loc, loc.MoveLA(utf8.RuneCountInString(d.Text), eh.buf.LineArray)) eh.Remove(loc, loc.MoveLA(utf8.RuneCountInString(d.Text), eh.buf.LineArray))
} else { } else {
if d.Type == dmp.DiffInsert { 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) 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 // 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{ e := &TextEvent{
C: *eh.cursors[eh.active], C: *eh.cursors[eh.active],
EventType: TextEventInsert, 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 // 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.Remove(start, end)
eh.Insert(start, replace) eh.Insert(start, replace)
} }

View File

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

View File

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

View File

@@ -110,7 +110,7 @@ func (i *InfoBuf) Prompt(prompt string, msg string, ptype string, eventcb func(s
i.HasGutter = false i.HasGutter = false
i.PromptCallback = donecb i.PromptCallback = donecb
i.EventCallback = eventcb 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)) { func (i *InfoBuf) YNPrompt(prompt string, donecb func(bool, bool)) {
@@ -146,7 +146,7 @@ func (i *InfoBuf) DonePrompt(canceled bool) {
} }
i.PromptCallback = nil i.PromptCallback = nil
} }
i.Replace(i.Start(), i.End(), []byte{}) i.Replace(i.Start(), i.End(), "")
} }
if i.YNCallback != nil && hadYN { if i.YNCallback != nil && hadYN {
i.YNCallback(i.YNResp, canceled) i.YNCallback(i.YNResp, canceled)