]> git.lizzy.rs Git - micro.git/blobdiff - cmd/micro/buffer.go
Small optimization
[micro.git] / cmd / micro / buffer.go
index 0879c0ec1f39c6b27c06b47d26b380c9a4fdc037..34ef3d1a2fc19cf148ed64ee439ae7b2ce2a1047 100644 (file)
@@ -27,8 +27,8 @@ type Buffer struct {
        // Provide efficient and easy access to text and lines so the rope String does not
        // need to be constantly recalculated
        // These variables are updated in the update() function
-       text  string
-       lines []string
+       lines    []string
+       numLines int
 
        // Syntax highlighting rules
        rules []SyntaxRule
@@ -60,14 +60,18 @@ func (b *Buffer) UpdateRules() {
        b.rules, b.filetype = GetRules(b)
 }
 
+func (b *Buffer) String() string {
+       text := ""
+       if b.r.Len() != 0 {
+               text = b.r.String()
+       }
+       return text
+}
+
 // Update fetches the string from the rope and updates the `text` and `lines` in the buffer
 func (b *Buffer) Update() {
-       if b.r.Len() == 0 {
-               b.text = ""
-       } else {
-               b.text = b.r.String()
-       }
-       b.lines = strings.Split(b.text, "\n")
+       b.lines = strings.Split(b.String(), "\n")
+       b.numLines = len(b.lines)
 }
 
 // Save saves the buffer to its default path
@@ -78,7 +82,7 @@ func (b *Buffer) Save() error {
 // SaveAs saves the buffer to a specified path (filename), creating the file if it does not exist
 func (b *Buffer) SaveAs(filename string) error {
        b.UpdateRules()
-       data := []byte(b.text)
+       data := []byte(b.String())
        err := ioutil.WriteFile(filename, data, 0644)
        if err == nil {
                b.savedText = md5.Sum(data)
@@ -93,17 +97,13 @@ func (b *Buffer) IsDirty() bool {
                return false
        }
        if b.netInsertions == 0 {
-               isDirty := b.savedText != md5.Sum([]byte(b.text))
+               isDirty := b.savedText != md5.Sum([]byte(b.String()))
                b.dirtySinceLastCheck = isDirty
                return isDirty
        }
        return true
 }
 
-func (b *Buffer) Lines() []string {
-       return strings.Split(b.text, "\n")
-}
-
 // Insert a string into the rope
 func (b *Buffer) Insert(idx int, value string) {
        b.dirtySinceLastCheck = true
@@ -123,7 +123,7 @@ func (b *Buffer) Remove(start, end int) string {
        if end > b.Len() {
                end = b.Len()
        }
-       removed := b.text[start:end]
+       removed := b.r.Report(start+1, end-start)
        // The rope implenentation I am using wants indicies starting at 1 instead of 0
        start++
        end++