ApplyDiff takes a string and runs the necessary insertion and deletion events to make the buffer equal to that string This means that we can transform the buffer into any string and still preserve undo/redo through insert and delete events
(new string)
| 165 | // This means that we can transform the buffer into any string and still preserve undo/redo |
| 166 | // through insert and delete events |
| 167 | func (eh *EventHandler) ApplyDiff(new string) { |
| 168 | differ := dmp.New() |
| 169 | diff := differ.DiffMain(string(eh.buf.Bytes()), new, false) |
| 170 | loc := eh.buf.Start() |
| 171 | for _, d := range diff { |
| 172 | if d.Type == dmp.DiffDelete { |
| 173 | eh.Remove(loc, loc.MoveLA(util.CharacterCountInString(d.Text), eh.buf.LineArray)) |
| 174 | } else { |
| 175 | if d.Type == dmp.DiffInsert { |
| 176 | eh.Insert(loc, d.Text) |
| 177 | } |
| 178 | loc = loc.MoveLA(util.CharacterCountInString(d.Text), eh.buf.LineArray) |
| 179 | } |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | // Insert creates an insert text event and executes it |
| 184 | func (eh *EventHandler) Insert(start Loc, textStr string) { |