Optimization for IsDirty()

This commit is contained in:
Zachary Yedidia 2016-04-18 14:20:40 -04:00
parent 2d3789c462
commit 273401d911

View file

@ -19,7 +19,8 @@ type Buffer struct {
name string
// This is the text stored every time the buffer is saved to check if the buffer is modified
savedText string
savedText string
netInsertions int
// Provide efficient and easy access to text and lines so the rope String does not
// need to be constantly recalculated
@ -84,11 +85,15 @@ func (b *Buffer) SaveAs(filename string) error {
// IsDirty returns whether or not the buffer has been modified compared to the one on disk
func (b *Buffer) IsDirty() bool {
return b.savedText != b.text
if b.netInsertions == 0 {
return b.savedText != b.text
}
return true
}
// Insert a string into the rope
func (b *Buffer) Insert(idx int, value string) {
b.netInsertions += len(value)
b.r = b.r.Insert(idx, value)
b.Update()
}
@ -96,6 +101,7 @@ func (b *Buffer) Insert(idx int, value string) {
// Remove a slice of the rope from start to end (exclusive)
// Returns the string that was removed
func (b *Buffer) Remove(start, end int) string {
b.netInsertions -= end - start
if start < 0 {
start = 0
}