4 "github.com/vinzmay/go-rope"
10 // Buffer stores the text for files that are loaded into the text editor
11 // It uses a rope to efficiently store the string and contains some
12 // simple functions for saving and wrapper functions for modifying the rope
14 // Stores the text of the buffer
17 // Path to the file on disk
19 // Name of the buffer on the status line
22 // 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 // File type of the buffer
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 return b.savedText != b.text
91 // Insert a string into the rope
92 func (b *Buffer) Insert(idx int, value string) {
93 b.r = b.r.Insert(idx, value)
97 // Remove a slice of the rope from start to end (exclusive)
98 // Returns the string that was removed
99 func (b *Buffer) Remove(start, end int) string {
106 removed := b.text[start:end]
107 // The rope implenentation I am using wants indicies starting at 1 instead of 0
110 b.r = b.r.Delete(start, end-start)
115 func (b *Buffer) Replace(search, replace string) error {
116 re, err := regexp.Compile(search)
120 text := re.ReplaceAllString(b.text, replace)
130 // Len gives the length of the buffer
131 func (b *Buffer) Len() int {