Refactor ReOpen function

This commit is contained in:
Zachary Yedidia
2016-05-30 17:48:33 -04:00
parent 7adcb13c08
commit b312e7c9ad
2 changed files with 23 additions and 32 deletions

View File

@@ -140,28 +140,33 @@ func (b *Buffer) CheckModTime() {
b.ModTime, _ = GetModTime(b.Path)
} else {
// Load new changes
data, err := ioutil.ReadFile(b.Path)
txt := string(data)
if err != nil {
messenger.Error(err.Error())
return
}
if txt == "" {
b.r = new(rope.Rope)
} else {
b.r = rope.New(txt)
}
b.ModTime, _ = GetModTime(b.Path)
b.Cursor.Relocate()
b.IsModified = false
b.Update()
b.ReOpen()
}
}
}
}
// ReOpen reloads the current buffer from disk
func (b *Buffer) ReOpen() {
data, err := ioutil.ReadFile(b.Path)
txt := string(data)
if err != nil {
messenger.Error(err.Error())
return
}
if txt == "" {
b.r = new(rope.Rope)
} else {
b.r = rope.New(txt)
}
b.ModTime, _ = GetModTime(b.Path)
b.Cursor.Relocate()
b.IsModified = false
b.Update()
}
// Update fetches the string from the rope and updates the `text` and `lines` in the buffer
func (b *Buffer) Update() {
b.Lines = strings.Split(b.String(), "\n")

View File

@@ -1,7 +1,6 @@
package main
import (
"io/ioutil"
"reflect"
"runtime"
"strconv"
@@ -192,20 +191,7 @@ func (v *View) CloseBuffer() {
// ReOpen reloads the current buffer
func (v *View) ReOpen() {
if v.CanClose("Continue? (yes, no, save) ") {
file, err := ioutil.ReadFile(v.Buf.Path)
filename := v.Buf.Name
if err != nil {
messenger.Error(err.Error())
return
}
buf := NewBuffer(string(file), filename)
v.Buf = buf
v.Cursor.Relocate()
buf.Cursor.Goto(*v.Cursor)
v.Cursor = &buf.Cursor
v.matches = Match(v)
v.Relocate()
v.Buf.ReOpen()
}
}