]> git.lizzy.rs Git - micro.git/blobdiff - cmd/micro/buffer.go
Remove unused syntax highlighting code and optimize IsDirty()
[micro.git] / cmd / micro / buffer.go
index 66b02afda8dd59bc29696ec50d5a323b0b1e96f9..9ab2ae79d8077e07f7bbcdc0e9695d126768a550 100644 (file)
@@ -19,7 +19,9 @@ 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
+       dirtySinceLastCheck bool
 
        // Provide efficient and easy access to text and lines so the rope String does not
        // need to be constantly recalculated
@@ -78,17 +80,28 @@ func (b *Buffer) SaveAs(filename string) error {
        err := ioutil.WriteFile(filename, []byte(b.text), 0644)
        if err == nil {
                b.savedText = b.text
+               b.netInsertions = 0
        }
        return err
 }
 
 // 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.dirtySinceLastCheck {
+               return false
+       }
+       if b.netInsertions == 0 {
+               isDirty := b.savedText != b.text
+               b.dirtySinceLastCheck = isDirty
+               return isDirty
+       }
+       return true
 }
 
 // Insert a string into the rope
 func (b *Buffer) Insert(idx int, value string) {
+       b.dirtySinceLastCheck = true
+       b.netInsertions += len(value)
        b.r = b.r.Insert(idx, value)
        b.Update()
 }
@@ -96,6 +109,8 @@ 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.dirtySinceLastCheck = true
+       b.netInsertions -= end - start
        if start < 0 {
                start = 0
        }