MCPcopy
hub / github.com/micro-editor/micro / ApplyDiff

Method ApplyDiff

internal/buffer/eventhandler.go:167–181  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

165// This means that we can transform the buffer into any string and still preserve undo/redo
166// through insert and delete events
167func (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
184func (eh *EventHandler) Insert(start Loc, textStr string) {

Callers 1

ReOpenMethod · 0.80

Calls 6

RemoveMethod · 0.95
InsertMethod · 0.95
CharacterCountInStringFunction · 0.92
BytesMethod · 0.80
MoveLAMethod · 0.80
StartMethod · 0.45

Tested by

no test coverage detected