text string
start int
end int
- buf *Buffer
time time.Time
}
// ExecuteTextEvent runs a text event
-func ExecuteTextEvent(t *TextEvent) {
+func ExecuteTextEvent(t *TextEvent, buf *Buffer) {
if t.eventType == TextEventInsert {
- t.buf.Insert(t.start, t.text)
+ buf.insert(t.start, t.text)
} else if t.eventType == TextEventRemove {
- t.text = t.buf.Remove(t.start, t.end)
+ t.text = buf.remove(t.start, t.end)
}
}
// UndoTextEvent undoes a text event
-func UndoTextEvent(t *TextEvent) {
+func UndoTextEvent(t *TextEvent, buf *Buffer) {
t.eventType = -t.eventType
- ExecuteTextEvent(t)
+ ExecuteTextEvent(t, buf)
}
// EventHandler executes text manipulations and allows undoing and redoing
type EventHandler struct {
- v *View
+ buf *Buffer
undo *Stack
redo *Stack
}
// NewEventHandler returns a new EventHandler
-func NewEventHandler(v *View) *EventHandler {
+func NewEventHandler(buf *Buffer) *EventHandler {
eh := new(EventHandler)
eh.undo = new(Stack)
eh.redo = new(Stack)
- eh.v = v
+ eh.buf = buf
return eh
}
// Insert creates an insert text event and executes it
func (eh *EventHandler) Insert(start int, text string) {
e := &TextEvent{
- c: eh.v.cursor,
+ c: eh.buf.Cursor,
eventType: TextEventInsert,
text: text,
start: start,
end: start + Count(text),
- buf: eh.v.buf,
time: time.Now(),
}
eh.Execute(e)
// Remove creates a remove text event and executes it
func (eh *EventHandler) Remove(start, end int) {
e := &TextEvent{
- c: eh.v.cursor,
+ c: eh.buf.Cursor,
eventType: TextEventRemove,
start: start,
end: end,
- buf: eh.v.buf,
time: time.Now(),
}
eh.Execute(e)
eh.redo = new(Stack)
}
eh.undo.Push(t)
- ExecuteTextEvent(t)
+ ExecuteTextEvent(t, eh.buf)
}
// Undo the first event in the undo stack
te := t.(*TextEvent)
// Undo it
// Modifies the text event
- UndoTextEvent(te)
+ UndoTextEvent(te, eh.buf)
// Set the cursor in the right place
teCursor := te.c
- te.c = eh.v.cursor
- eh.v.cursor = teCursor
+ te.c = eh.buf.Cursor
+ eh.buf.Cursor = teCursor
// Push it to the redo stack
eh.redo.Push(te)
te := t.(*TextEvent)
// Modifies the text event
- UndoTextEvent(te)
+ UndoTextEvent(te, eh.buf)
teCursor := te.c
- te.c = eh.v.cursor
- eh.v.cursor = teCursor
+ te.c = eh.buf.Cursor
+ eh.buf.Cursor = teCursor
eh.undo.Push(te)
}