Start insert performance improvements

This commit is contained in:
Zachary Yedidia 2020-02-09 14:30:20 -05:00
parent c3d120ccdf
commit ca9d102267
2 changed files with 30 additions and 1 deletions

View file

@ -1,6 +1,8 @@
package buffer package buffer
import ( import (
"bytes"
"log"
"time" "time"
"unicode/utf8" "unicode/utf8"
@ -122,21 +124,38 @@ func (eh *EventHandler) InsertBytes(start Loc, text []byte) {
Deltas: []Delta{{text, start, Loc{0, 0}}}, Deltas: []Delta{{text, start, Loc{0, 0}}},
Time: time.Now(), Time: time.Now(),
} }
// oldl := eh.buf.LinesNum()
eh.Execute(e) eh.Execute(e)
// linecount := eh.buf.LinesNum() - oldl
textcount := utf8.RuneCount(text) 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 = start.MoveLA(textcount, eh.buf.LineArray)
// e.Deltas[0].End = clamp(Loc{endX, start.Y + linecount}, eh.buf.LineArray)
end := e.Deltas[0].End end := e.Deltas[0].End
for _, c := range eh.cursors { for _, c := range eh.cursors {
move := func(loc Loc) Loc { move := func(loc Loc) Loc {
log.Println("move", loc)
if start.Y != end.Y && loc.GreaterThan(start) { if start.Y != end.Y && loc.GreaterThan(start) {
loc.Y += end.Y - start.Y loc.Y += end.Y - start.Y
} else if loc.Y == start.Y && loc.GreaterEqual(start) { } 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 return loc
} }
c.Loc = move(c.Loc) c.Loc = move(c.Loc)
c.Relocate()
c.CurSelection[0] = move(c.CurSelection[0]) c.CurSelection[0] = move(c.CurSelection[0])
c.CurSelection[1] = move(c.CurSelection[1]) c.CurSelection[1] = move(c.CurSelection[1])
c.OrigSelection[0] = move(c.OrigSelection[0]) c.OrigSelection[0] = move(c.OrigSelection[0])

View file

@ -135,3 +135,13 @@ func ByteOffset(pos Loc, buf *Buffer) int {
loc += len(buf.Line(y)[:x]) loc += len(buf.Line(y)[:x])
return loc 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
}