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
23 // Provide efficient and easy access to text and lines so the rope String does not
24 // need to be constantly recalculated
25 // These variables are updated in the update() function
29 // Syntax highlighting rules
31 // The buffer's filetype
35 // NewBuffer creates a new buffer from `txt` with path and name `path`
36 func NewBuffer(txt, path string) *Buffer {
52 // UpdateRules updates the syntax rules and filetype for this buffer
53 // This is called when the colorscheme changes
54 func (b *Buffer) UpdateRules() {
55 b.rules, b.FileType = GetRules(b)
58 func (b *Buffer) String() string {
65 // Update fetches the string from the rope and updates the `text` and `lines` in the buffer
66 func (b *Buffer) Update() {
67 b.Lines = strings.Split(b.String(), "\n")
68 b.NumLines = len(b.Lines)
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 data := []byte(b.String())
80 err := ioutil.WriteFile(filename, data, 0644)
87 // Insert a string into the rope
88 func (b *Buffer) Insert(idx int, value string) {
90 b.r = b.r.Insert(idx, value)
94 // Remove a slice of the rope from start to end (exclusive)
95 // Returns the string that was removed
96 func (b *Buffer) Remove(start, end int) string {
107 removed := b.Substr(start, end)
108 // The rope implenentation I am using wants indicies starting at 1 instead of 0
111 b.r = b.r.Delete(start, end-start)
116 // Substr returns the substring of the rope from start to end
117 func (b *Buffer) Substr(start, end int) string {
118 return b.r.Substr(start+1, end-start).String()
121 // Len gives the length of the buffer
122 func (b *Buffer) Len() int {