6 dmp "github.com/sergi/go-diff/diffmatchpatch"
10 // Opposite and undoing events must have opposite values
12 // TextEventInsert repreasents an insertion event
14 // TextEventRemove represents a deletion event
18 // TextEvent holds data for a manipulation on some text that can be undone
19 type TextEvent struct {
29 // ExecuteTextEvent runs a text event
30 func ExecuteTextEvent(t *TextEvent, buf *Buffer) {
31 if t.EventType == TextEventInsert {
32 buf.insert(t.Start, []byte(t.Text))
33 } else if t.EventType == TextEventRemove {
34 t.Text = buf.remove(t.Start, t.End)
38 // UndoTextEvent undoes a text event
39 func UndoTextEvent(t *TextEvent, buf *Buffer) {
40 t.EventType = -t.EventType
41 ExecuteTextEvent(t, buf)
44 // EventHandler executes text manipulations and allows undoing and redoing
45 type EventHandler struct {
51 // NewEventHandler returns a new EventHandler
52 func NewEventHandler(buf *Buffer) *EventHandler {
53 eh := new(EventHandler)
54 eh.UndoStack = new(Stack)
55 eh.RedoStack = new(Stack)
60 // ApplyDiff takes a string and runs the necessary insertion and deletion events to make
61 // the buffer equal to that string
62 // This means that we can transform the buffer into any string and still preserve undo/redo
63 // through insert and delete events
64 func (eh *EventHandler) ApplyDiff(new string) {
66 diff := differ.DiffMain(eh.buf.String(), new, false)
68 for _, d := range diff {
69 if d.Type == dmp.DiffInsert {
70 eh.Insert(loc, d.Text)
71 } else if d.Type == dmp.DiffDelete {
72 eh.Remove(loc, loc.Move(Count(d.Text), eh.buf))
74 loc = loc.Move(Count(d.Text), eh.buf)
78 // Insert creates an insert text event and executes it
79 func (eh *EventHandler) Insert(start Loc, text string) {
82 EventType: TextEventInsert,
88 e.End = start.Move(Count(text), eh.buf)
91 // Remove creates a remove text event and executes it
92 func (eh *EventHandler) Remove(start, end Loc) {
95 EventType: TextEventRemove,
103 // Replace deletes from start to end and replaces it with the given string
104 func (eh *EventHandler) Replace(start, end Loc, replace string) {
105 eh.Remove(start, end)
106 eh.Insert(start, replace)
109 // Execute a textevent and add it to the undo stack
110 func (eh *EventHandler) Execute(t *TextEvent) {
111 if eh.RedoStack.Len() > 0 {
112 eh.RedoStack = new(Stack)
115 ExecuteTextEvent(t, eh.buf)
118 // Undo the first event in the undo stack
119 func (eh *EventHandler) Undo() {
120 t := eh.UndoStack.Peek()
125 startTime := t.Time.UnixNano() / int64(time.Millisecond)
130 t = eh.UndoStack.Peek()
135 if startTime-(t.Time.UnixNano()/int64(time.Millisecond)) > undoThreshold {
138 startTime = t.Time.UnixNano() / int64(time.Millisecond)
144 // UndoOneEvent undoes one event
145 func (eh *EventHandler) UndoOneEvent() {
146 // This event should be undone
147 // Pop it off the stack
148 t := eh.UndoStack.Pop()
154 // Modifies the text event
155 UndoTextEvent(t, eh.buf)
157 // Set the cursor in the right place
160 eh.buf.Cursor.Goto(teCursor)
162 // Push it to the redo stack
166 // Redo the first event in the redo stack
167 func (eh *EventHandler) Redo() {
168 t := eh.RedoStack.Peek()
173 startTime := t.Time.UnixNano() / int64(time.Millisecond)
178 t = eh.RedoStack.Peek()
183 if (t.Time.UnixNano()/int64(time.Millisecond))-startTime > undoThreshold {
191 // RedoOneEvent redoes one event
192 func (eh *EventHandler) RedoOneEvent() {
193 t := eh.RedoStack.Pop()
198 // Modifies the text event
199 UndoTextEvent(t, eh.buf)
203 eh.buf.Cursor.Goto(teCursor)