提交 ca9d1022 编写于 作者: Z Zachary Yedidia

Start insert performance improvements

上级 c3d120cc
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])
......
......@@ -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
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册