]> git.lizzy.rs Git - micro.git/blob - cmd/micro/eventhandler.go
Small fix to redraw location
[micro.git] / cmd / micro / eventhandler.go
1 package main
2
3 import (
4         "time"
5
6         dmp "github.com/sergi/go-diff/diffmatchpatch"
7 )
8
9 const (
10         // Opposite and undoing events must have opposite values
11
12         // TextEventInsert repreasents an insertion event
13         TextEventInsert = 1
14         // TextEventRemove represents a deletion event
15         TextEventRemove = -1
16 )
17
18 // TextEvent holds data for a manipulation on some text that can be undone
19 type TextEvent struct {
20         C Cursor
21
22         EventType int
23         Text      string
24         Start     Loc
25         End       Loc
26         Time      time.Time
27 }
28
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)
35         }
36 }
37
38 // UndoTextEvent undoes a text event
39 func UndoTextEvent(t *TextEvent, buf *Buffer) {
40         t.EventType = -t.EventType
41         ExecuteTextEvent(t, buf)
42 }
43
44 // EventHandler executes text manipulations and allows undoing and redoing
45 type EventHandler struct {
46         buf       *Buffer
47         UndoStack *Stack
48         RedoStack *Stack
49 }
50
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)
56         eh.buf = buf
57         return eh
58 }
59
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) {
65         differ := dmp.New()
66         diff := differ.DiffMain(eh.buf.String(), new, false)
67         loc := eh.buf.Start()
68         for _, d := range diff {
69                 if d.Type == dmp.DiffDelete {
70                         eh.Remove(loc, loc.Move(Count(d.Text), eh.buf))
71                 } else {
72                         if d.Type == dmp.DiffInsert {
73                                 eh.Insert(loc, d.Text)
74                         }
75                         loc = loc.Move(Count(d.Text), eh.buf)
76                 }
77         }
78 }
79
80 // Insert creates an insert text event and executes it
81 func (eh *EventHandler) Insert(start Loc, text string) {
82         e := &TextEvent{
83                 C:         eh.buf.Cursor,
84                 EventType: TextEventInsert,
85                 Text:      text,
86                 Start:     start,
87                 Time:      time.Now(),
88         }
89         eh.Execute(e)
90         e.End = start.Move(Count(text), eh.buf)
91 }
92
93 // Remove creates a remove text event and executes it
94 func (eh *EventHandler) Remove(start, end Loc) {
95         e := &TextEvent{
96                 C:         eh.buf.Cursor,
97                 EventType: TextEventRemove,
98                 Start:     start,
99                 End:       end,
100                 Time:      time.Now(),
101         }
102         eh.Execute(e)
103 }
104
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)
109 }
110
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)
115         }
116         eh.UndoStack.Push(t)
117         ExecuteTextEvent(t, eh.buf)
118 }
119
120 // Undo the first event in the undo stack
121 func (eh *EventHandler) Undo() {
122         t := eh.UndoStack.Peek()
123         if t == nil {
124                 return
125         }
126
127         startTime := t.Time.UnixNano() / int64(time.Millisecond)
128
129         eh.UndoOneEvent()
130
131         for {
132                 t = eh.UndoStack.Peek()
133                 if t == nil {
134                         return
135                 }
136
137                 if startTime-(t.Time.UnixNano()/int64(time.Millisecond)) > undoThreshold {
138                         return
139                 }
140                 startTime = t.Time.UnixNano() / int64(time.Millisecond)
141
142                 eh.UndoOneEvent()
143         }
144 }
145
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()
151         if t == nil {
152                 return
153         }
154
155         // Undo it
156         // Modifies the text event
157         UndoTextEvent(t, eh.buf)
158
159         // Set the cursor in the right place
160         teCursor := t.C
161         t.C = eh.buf.Cursor
162         eh.buf.Cursor.Goto(teCursor)
163
164         // Push it to the redo stack
165         eh.RedoStack.Push(t)
166 }
167
168 // Redo the first event in the redo stack
169 func (eh *EventHandler) Redo() {
170         t := eh.RedoStack.Peek()
171         if t == nil {
172                 return
173         }
174
175         startTime := t.Time.UnixNano() / int64(time.Millisecond)
176
177         eh.RedoOneEvent()
178
179         for {
180                 t = eh.RedoStack.Peek()
181                 if t == nil {
182                         return
183                 }
184
185                 if (t.Time.UnixNano()/int64(time.Millisecond))-startTime > undoThreshold {
186                         return
187                 }
188
189                 eh.RedoOneEvent()
190         }
191 }
192
193 // RedoOneEvent redoes one event
194 func (eh *EventHandler) RedoOneEvent() {
195         t := eh.RedoStack.Pop()
196         if t == nil {
197                 return
198         }
199
200         // Modifies the text event
201         UndoTextEvent(t, eh.buf)
202
203         teCursor := t.C
204         t.C = eh.buf.Cursor
205         eh.buf.Cursor.Goto(teCursor)
206
207         eh.UndoStack.Push(t)
208 }