Don't store buffer in text event

This commit is contained in:
Zachary Yedidia
2016-05-01 16:45:23 -04:00
parent 012668146c
commit efb4b5e899

View File

@@ -21,23 +21,22 @@ type TextEvent struct {
text string text string
start int start int
end int end int
buf *Buffer
time time.Time time time.Time
} }
// ExecuteTextEvent runs a text event // ExecuteTextEvent runs a text event
func ExecuteTextEvent(t *TextEvent) { func ExecuteTextEvent(t *TextEvent, buf *Buffer) {
if t.eventType == TextEventInsert { if t.eventType == TextEventInsert {
t.buf.Insert(t.start, t.text) buf.Insert(t.start, t.text)
} else if t.eventType == TextEventRemove { } 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 // UndoTextEvent undoes a text event
func UndoTextEvent(t *TextEvent) { func UndoTextEvent(t *TextEvent, buf *Buffer) {
t.eventType = -t.eventType t.eventType = -t.eventType
ExecuteTextEvent(t) ExecuteTextEvent(t, buf)
} }
// EventHandler executes text manipulations and allows undoing and redoing // EventHandler executes text manipulations and allows undoing and redoing
@@ -64,7 +63,6 @@ func (eh *EventHandler) Insert(start int, text string) {
text: text, text: text,
start: start, start: start,
end: start + Count(text), end: start + Count(text),
buf: eh.v.buf,
time: time.Now(), time: time.Now(),
} }
eh.Execute(e) eh.Execute(e)
@@ -77,7 +75,6 @@ func (eh *EventHandler) Remove(start, end int) {
eventType: TextEventRemove, eventType: TextEventRemove,
start: start, start: start,
end: end, end: end,
buf: eh.v.buf,
time: time.Now(), time: time.Now(),
} }
eh.Execute(e) eh.Execute(e)
@@ -95,7 +92,7 @@ func (eh *EventHandler) Execute(t *TextEvent) {
eh.redo = new(Stack) eh.redo = new(Stack)
} }
eh.undo.Push(t) eh.undo.Push(t)
ExecuteTextEvent(t) ExecuteTextEvent(t, eh.v.buf)
} }
// Undo the first event in the undo stack // Undo the first event in the undo stack
@@ -138,7 +135,7 @@ func (eh *EventHandler) UndoOneEvent() {
te := t.(*TextEvent) te := t.(*TextEvent)
// Undo it // Undo it
// Modifies the text event // Modifies the text event
UndoTextEvent(te) UndoTextEvent(te, eh.v.buf)
// Set the cursor in the right place // Set the cursor in the right place
teCursor := te.c teCursor := te.c
@@ -186,7 +183,7 @@ func (eh *EventHandler) RedoOneEvent() {
te := t.(*TextEvent) te := t.(*TextEvent)
// Modifies the text event // Modifies the text event
UndoTextEvent(te) UndoTextEvent(te, eh.v.buf)
teCursor := te.c teCursor := te.c
te.c = eh.v.cursor te.c = eh.v.cursor