]> git.lizzy.rs Git - micro.git/blob - cmd/micro/eventhandler.go
Merge pull request #168 from onodera-punpun/syntax_css
[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.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))
73                 }
74                 loc = loc.Move(Count(d.Text), eh.buf)
75         }
76 }
77
78 // Insert creates an insert text event and executes it
79 func (eh *EventHandler) Insert(start Loc, text string) {
80         e := &TextEvent{
81                 C:         eh.buf.Cursor,
82                 EventType: TextEventInsert,
83                 Text:      text,
84                 Start:     start,
85                 Time:      time.Now(),
86         }
87         eh.Execute(e)
88         e.End = start.Move(Count(text), eh.buf)
89 }
90
91 // Remove creates a remove text event and executes it
92 func (eh *EventHandler) Remove(start, end Loc) {
93         e := &TextEvent{
94                 C:         eh.buf.Cursor,
95                 EventType: TextEventRemove,
96                 Start:     start,
97                 End:       end,
98                 Time:      time.Now(),
99         }
100         eh.Execute(e)
101 }
102
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)
107 }
108
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)
113         }
114         eh.UndoStack.Push(t)
115         ExecuteTextEvent(t, eh.buf)
116 }
117
118 // Undo the first event in the undo stack
119 func (eh *EventHandler) Undo() {
120         t := eh.UndoStack.Peek()
121         if t == nil {
122                 return
123         }
124
125         startTime := t.Time.UnixNano() / int64(time.Millisecond)
126
127         eh.UndoOneEvent()
128
129         for {
130                 t = eh.UndoStack.Peek()
131                 if t == nil {
132                         return
133                 }
134
135                 if startTime-(t.Time.UnixNano()/int64(time.Millisecond)) > undoThreshold {
136                         return
137                 }
138                 startTime = t.Time.UnixNano() / int64(time.Millisecond)
139
140                 eh.UndoOneEvent()
141         }
142 }
143
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()
149         if t == nil {
150                 return
151         }
152
153         // Undo it
154         // Modifies the text event
155         UndoTextEvent(t, eh.buf)
156
157         // Set the cursor in the right place
158         teCursor := t.C
159         t.C = eh.buf.Cursor
160         eh.buf.Cursor.Goto(teCursor)
161
162         // Push it to the redo stack
163         eh.RedoStack.Push(t)
164 }
165
166 // Redo the first event in the redo stack
167 func (eh *EventHandler) Redo() {
168         t := eh.RedoStack.Peek()
169         if t == nil {
170                 return
171         }
172
173         startTime := t.Time.UnixNano() / int64(time.Millisecond)
174
175         eh.RedoOneEvent()
176
177         for {
178                 t = eh.RedoStack.Peek()
179                 if t == nil {
180                         return
181                 }
182
183                 if (t.Time.UnixNano()/int64(time.Millisecond))-startTime > undoThreshold {
184                         return
185                 }
186
187                 eh.RedoOneEvent()
188         }
189 }
190
191 // RedoOneEvent redoes one event
192 func (eh *EventHandler) RedoOneEvent() {
193         t := eh.RedoStack.Pop()
194         if t == nil {
195                 return
196         }
197
198         // Modifies the text event
199         UndoTextEvent(t, eh.buf)
200
201         teCursor := t.C
202         t.C = eh.buf.Cursor
203         eh.buf.Cursor.Goto(teCursor)
204
205         eh.UndoStack.Push(t)
206 }