Optimization for IsDirty()

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

View File

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