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.DiffDelete {
70 eh.Remove(loc, loc.Move(Count(d.Text), eh.buf))
72 if d.Type == dmp.DiffInsert {
73 eh.Insert(loc, d.Text)
75 loc = loc.Move(Count(d.Text), eh.buf)
80 // Insert creates an insert text event and executes it
81 func (eh *EventHandler) Insert(start Loc, text string) {
84 EventType: TextEventInsert,
90 e.End = start.Move(Count(text), eh.buf)
93 // Remove creates a remove text event and executes it
94 func (eh *EventHandler) Remove(start, end Loc) {
97 EventType: TextEventRemove,
105 // Replace deletes from start to end and replaces it with the given string
106 func (eh *EventHandler) Replace(start, end Loc, replace string) {
107 eh.Remove(start, end)
108 eh.Insert(start, replace)
111 // Execute a textevent and add it to the undo stack
112 func (eh *EventHandler) Execute(t *TextEvent) {
113 if eh.RedoStack.Len() > 0 {
114 eh.RedoStack = new(Stack)
117 ExecuteTextEvent(t, eh.buf)
120 // Undo the first event in the undo stack
121 func (eh *EventHandler) Undo() {
122 t := eh.UndoStack.Peek()
127 startTime := t.Time.UnixNano() / int64(time.Millisecond)
132 t = eh.UndoStack.Peek()
137 if startTime-(t.Time.UnixNano()/int64(time.Millisecond)) > undoThreshold {
140 startTime = t.Time.UnixNano() / int64(time.Millisecond)
146 // UndoOneEvent undoes one event
147 func (eh *EventHandler) UndoOneEvent() {
148 // This event should be undone
149 // Pop it off the stack
150 t := eh.UndoStack.Pop()
156 // Modifies the text event
157 UndoTextEvent(t, eh.buf)
159 // Set the cursor in the right place
162 eh.buf.Cursor.Goto(teCursor)
164 // Push it to the redo stack
168 // Redo the first event in the redo stack
169 func (eh *EventHandler) Redo() {
170 t := eh.RedoStack.Peek()
175 startTime := t.Time.UnixNano() / int64(time.Millisecond)
180 t = eh.RedoStack.Peek()
185 if (t.Time.UnixNano()/int64(time.Millisecond))-startTime > undoThreshold {
193 // RedoOneEvent redoes one event
194 func (eh *EventHandler) RedoOneEvent() {
195 t := eh.RedoStack.Pop()
200 // Modifies the text event
201 UndoTextEvent(t, eh.buf)
205 eh.buf.Cursor.Goto(teCursor)