Improve remove performance

This commit is contained in:
Zachary Yedidia
2020-02-09 14:58:37 -05:00
parent ca9d102267
commit 8ddf335e68
3 changed files with 18 additions and 15 deletions

View File

@@ -2,7 +2,6 @@ package buffer
import (
"bytes"
"log"
"time"
"unicode/utf8"
@@ -124,33 +123,35 @@ func (eh *EventHandler) InsertBytes(start Loc, text []byte) {
Deltas: []Delta{{text, start, Loc{0, 0}}},
Time: time.Now(),
}
// oldl := eh.buf.LinesNum()
oldl := eh.buf.LinesNum()
eh.Execute(e)
// linecount := eh.buf.LinesNum() - oldl
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:])
endX = utf8.RuneCount(text[lastnl+1:])
textX = endX
} else {
// endX = start.X + textcount
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)
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.Y += end.Y - start.Y
loc.X += textX
if lastnl >= 0 {
loc.X = textX
} else {
loc.X += textX
}
}
return loc
}