From ca9d102267f34e893c5cf7653e789e57c200c776 Mon Sep 17 00:00:00 2001 From: Zachary Yedidia Date: Sun, 9 Feb 2020 14:30:20 -0500 Subject: [PATCH] Start insert performance improvements --- internal/buffer/eventhandler.go | 21 ++++++++++++++++++++- internal/buffer/loc.go | 10 ++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/internal/buffer/eventhandler.go b/internal/buffer/eventhandler.go index 6eb546bd..c271195d 100644 --- a/internal/buffer/eventhandler.go +++ b/internal/buffer/eventhandler.go @@ -1,6 +1,8 @@ package buffer import ( + "bytes" + "log" "time" "unicode/utf8" @@ -122,21 +124,38 @@ func (eh *EventHandler) InsertBytes(start Loc, text []byte) { Deltas: []Delta{{text, start, Loc{0, 0}}}, Time: time.Now(), } + // oldl := eh.buf.LinesNum() eh.Execute(e) + // linecount := eh.buf.LinesNum() - oldl textcount := utf8.RuneCount(text) + lastnl := bytes.LastIndex(text, []byte{'\n'}) + var endX int + var textX int + if lastnl >= 0 { + endX = utf8.RuneCount(text[lastnl:]) + textX = endX + } else { + // endX = start.X + textcount + textX = textcount + } + e.Deltas[0].End = start.MoveLA(textcount, eh.buf.LineArray) + // e.Deltas[0].End = clamp(Loc{endX, start.Y + linecount}, eh.buf.LineArray) end := e.Deltas[0].End for _, c := range eh.cursors { move := func(loc Loc) Loc { + log.Println("move", loc) if start.Y != end.Y && loc.GreaterThan(start) { loc.Y += end.Y - start.Y } else if loc.Y == start.Y && loc.GreaterEqual(start) { - loc = loc.MoveLA(textcount, eh.buf.LineArray) + loc.Y += end.Y - start.Y + loc.X += textX } return loc } c.Loc = move(c.Loc) + c.Relocate() c.CurSelection[0] = move(c.CurSelection[0]) c.CurSelection[1] = move(c.CurSelection[1]) c.OrigSelection[0] = move(c.OrigSelection[0]) diff --git a/internal/buffer/loc.go b/internal/buffer/loc.go index 89db7b89..4fd66329 100644 --- a/internal/buffer/loc.go +++ b/internal/buffer/loc.go @@ -135,3 +135,13 @@ func ByteOffset(pos Loc, buf *Buffer) int { loc += len(buf.Line(y)[:x]) return loc } + +// clamps a loc within a buffer +func clamp(pos Loc, la *LineArray) Loc { + if pos.GreaterEqual(la.End()) { + return la.End().MoveLA(-1, la) + } else if pos.LessThan(la.Start()) { + return la.Start() + } + return pos +}