4 "github.com/vinzmay/go-rope"
9 // Buffer stores the text for files that are loaded into the text editor
10 // It uses a rope to efficiently store the string and contains some
11 // simple functions for saving and wrapper functions for modifying the rope
13 // Stores the text of the buffer
16 // Path to the file on disk
18 // Name of the buffer on the status line
21 // This is the text stored every time the buffer is saved to check if the buffer is modified
25 // Provide efficient and easy access to text and lines so the rope String does not
26 // need to be constantly recalculated
27 // These variables are updated in the update() function
31 // Syntax highlighting rules
33 // The buffer's filetype
37 // NewBuffer creates a new buffer from `txt` with path and name `path`
38 func NewBuffer(txt, path string) *Buffer {
55 // UpdateRules updates the syntax rules and filetype for this buffer
56 // This is called when the colorscheme changes
57 func (b *Buffer) UpdateRules() {
58 b.rules, b.filetype = GetRules(b)
61 // Update fetches the string from the rope and updates the `text` and `lines` in the buffer
62 func (b *Buffer) Update() {
68 b.lines = strings.Split(b.text, "\n")
71 // Save saves the buffer to its default path
72 func (b *Buffer) Save() error {
73 return b.SaveAs(b.path)
76 // SaveAs saves the buffer to a specified path (filename), creating the file if it does not exist
77 func (b *Buffer) SaveAs(filename string) error {
79 err := ioutil.WriteFile(filename, []byte(b.text), 0644)
86 // IsDirty returns whether or not the buffer has been modified compared to the one on disk
87 func (b *Buffer) IsDirty() bool {
88 if b.netInsertions == 0 {
89 return b.savedText != b.text
94 // Insert a string into the rope
95 func (b *Buffer) Insert(idx int, value string) {
96 b.netInsertions += len(value)
97 b.r = b.r.Insert(idx, value)
101 // Remove a slice of the rope from start to end (exclusive)
102 // Returns the string that was removed
103 func (b *Buffer) Remove(start, end int) string {
104 b.netInsertions -= end - start
111 removed := b.text[start:end]
112 // The rope implenentation I am using wants indicies starting at 1 instead of 0
115 b.r = b.r.Delete(start, end-start)
120 // Len gives the length of the buffer
121 func (b *Buffer) Len() int {