]> git.lizzy.rs Git - micro.git/blobdiff - cmd/micro/eventhandler.go
Use messenger error instead of termerror
[micro.git] / cmd / micro / eventhandler.go
index c54319eb437662637ca69991b5dfbca519f9765f..1a94813f266695cc8d072507de1bc7e33ccc5046 100644 (file)
@@ -1,7 +1,11 @@
 package main
 
 import (
+       "strings"
        "time"
+
+       dmp "github.com/sergi/go-diff/diffmatchpatch"
+       "github.com/yuin/gopher-lua"
 )
 
 const (
@@ -11,116 +15,166 @@ const (
        TextEventInsert = 1
        // TextEventRemove represents a deletion event
        TextEventRemove = -1
+
+       TextEventReplace = 0
 )
 
 // TextEvent holds data for a manipulation on some text that can be undone
 type TextEvent struct {
-       c Cursor
+       C Cursor
 
-       eventType int
-       text      string
-       start     int
-       end       int
-       buf       *Buffer
-       time      time.Time
+       EventType int
+       Deltas    []Delta
+       Time      time.Time
+}
+
+type Delta struct {
+       Text  string
+       Start Loc
+       End   Loc
 }
 
 // ExecuteTextEvent runs a text event
-func ExecuteTextEvent(t *TextEvent) {
-       if t.eventType == TextEventInsert {
-               t.buf.Insert(t.start, t.text)
-       } else if t.eventType == TextEventRemove {
-               t.text = t.buf.Remove(t.start, t.end)
+func ExecuteTextEvent(t *TextEvent, buf *Buffer) {
+       if t.EventType == TextEventInsert {
+               for _, d := range t.Deltas {
+                       buf.insert(d.Start, []byte(d.Text))
+               }
+       } else if t.EventType == TextEventRemove {
+               for i, d := range t.Deltas {
+                       t.Deltas[i].Text = buf.remove(d.Start, d.End)
+               }
+       } else if t.EventType == TextEventReplace {
+               for i, d := range t.Deltas {
+                       t.Deltas[i].Text = buf.remove(d.Start, d.End)
+                       buf.insert(d.Start, []byte(d.Text))
+               }
        }
 }
 
 // UndoTextEvent undoes a text event
-func UndoTextEvent(t *TextEvent) {
-       t.eventType = -t.eventType
-       ExecuteTextEvent(t)
+func UndoTextEvent(t *TextEvent, buf *Buffer) {
+       t.EventType = -t.EventType
+       ExecuteTextEvent(t, buf)
 }
 
 // EventHandler executes text manipulations and allows undoing and redoing
 type EventHandler struct {
-       v    *View
-       undo *Stack
-       redo *Stack
+       buf       *Buffer
+       UndoStack *Stack
+       RedoStack *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.UndoStack = new(Stack)
+       eh.RedoStack = new(Stack)
+       eh.buf = buf
        return eh
 }
 
+// ApplyDiff takes a string and runs the necessary insertion and deletion events to make
+// the buffer equal to that string
+// This means that we can transform the buffer into any string and still preserve undo/redo
+// through insert and delete events
+func (eh *EventHandler) ApplyDiff(new string) {
+       differ := dmp.New()
+       diff := differ.DiffMain(eh.buf.String(), new, false)
+       loc := eh.buf.Start()
+       for _, d := range diff {
+               if d.Type == dmp.DiffDelete {
+                       eh.Remove(loc, loc.Move(Count(d.Text), eh.buf))
+               } else {
+                       if d.Type == dmp.DiffInsert {
+                               eh.Insert(loc, d.Text)
+                       }
+                       loc = loc.Move(Count(d.Text), eh.buf)
+               }
+       }
+}
+
 // Insert creates an insert text event and executes it
-func (eh *EventHandler) Insert(start int, text string) {
+func (eh *EventHandler) Insert(start Loc, text string) {
        e := &TextEvent{
-               c:         eh.v.cursor,
-               eventType: TextEventInsert,
-               text:      text,
-               start:     start,
-               end:       start + Count(text),
-               buf:       eh.v.buf,
-               time:      time.Now(),
+               C:         eh.buf.Cursor,
+               EventType: TextEventInsert,
+               Deltas:    []Delta{Delta{text, start, Loc{0, 0}}},
+               Time:      time.Now(),
        }
        eh.Execute(e)
+       e.Deltas[0].End = start.Move(Count(text), eh.buf)
 }
 
 // Remove creates a remove text event and executes it
-func (eh *EventHandler) Remove(start, end int) {
+func (eh *EventHandler) Remove(start, end Loc) {
        e := &TextEvent{
-               c:         eh.v.cursor,
-               eventType: TextEventRemove,
-               start:     start,
-               end:       end,
-               buf:       eh.v.buf,
-               time:      time.Now(),
+               C:         eh.buf.Cursor,
+               EventType: TextEventRemove,
+               Deltas:    []Delta{Delta{"", start, end}},
+               Time:      time.Now(),
+       }
+       eh.Execute(e)
+}
+
+// Multiple creates an multiple insertions executes them
+func (eh *EventHandler) MultipleReplace(deltas []Delta) {
+       e := &TextEvent{
+               C:         eh.buf.Cursor,
+               EventType: TextEventReplace,
+               Deltas:    deltas,
+               Time:      time.Now(),
        }
        eh.Execute(e)
 }
 
 // Replace deletes from start to end and replaces it with the given string
-func (eh *EventHandler) Replace(start, end int, replace string) {
+func (eh *EventHandler) Replace(start, end Loc, replace string) {
        eh.Remove(start, end)
        eh.Insert(start, replace)
 }
 
 // Execute a textevent and add it to the undo stack
 func (eh *EventHandler) Execute(t *TextEvent) {
-       if eh.redo.Len() > 0 {
-               eh.redo = new(Stack)
+       if eh.RedoStack.Len() > 0 {
+               eh.RedoStack = new(Stack)
        }
-       eh.undo.Push(t)
-       ExecuteTextEvent(t)
+       eh.UndoStack.Push(t)
+
+       for pl := range loadedPlugins {
+               ret, err := Call(pl+".onBeforeTextEvent", t)
+               if err != nil && !strings.HasPrefix(err.Error(), "function does not exist") {
+                       TermMessage(err)
+               }
+               if val, ok := ret.(lua.LBool); ok && val == lua.LFalse {
+                       return
+               }
+       }
+
+       ExecuteTextEvent(t, eh.buf)
 }
 
 // Undo the first event in the undo stack
 func (eh *EventHandler) Undo() {
-       t := eh.undo.Peek()
+       t := eh.UndoStack.Peek()
        if t == nil {
                return
        }
 
-       te := t.(*TextEvent)
-       startTime := t.(*TextEvent).time.UnixNano() / int64(time.Millisecond)
+       startTime := t.Time.UnixNano() / int64(time.Millisecond)
 
        eh.UndoOneEvent()
 
        for {
-               t = eh.undo.Peek()
+               t = eh.UndoStack.Peek()
                if t == nil {
                        return
                }
 
-               te = t.(*TextEvent)
-
-               if startTime-(te.time.UnixNano()/int64(time.Millisecond)) > undoThreshold {
+               if startTime-(t.Time.UnixNano()/int64(time.Millisecond)) > undoThreshold {
                        return
                }
+               startTime = t.Time.UnixNano() / int64(time.Millisecond)
 
                eh.UndoOneEvent()
        }
@@ -130,46 +184,42 @@ func (eh *EventHandler) Undo() {
 func (eh *EventHandler) UndoOneEvent() {
        // This event should be undone
        // Pop it off the stack
-       t := eh.undo.Pop()
+       t := eh.UndoStack.Pop()
        if t == nil {
                return
        }
 
-       te := t.(*TextEvent)
        // Undo it
        // Modifies the text event
-       UndoTextEvent(te)
+       UndoTextEvent(t, eh.buf)
 
        // Set the cursor in the right place
-       teCursor := te.c
-       te.c = eh.v.cursor
-       eh.v.cursor = teCursor
+       teCursor := t.C
+       t.C = eh.buf.Cursor
+       eh.buf.Cursor.Goto(teCursor)
 
        // Push it to the redo stack
-       eh.redo.Push(te)
+       eh.RedoStack.Push(t)
 }
 
 // Redo the first event in the redo stack
 func (eh *EventHandler) Redo() {
-       t := eh.redo.Peek()
+       t := eh.RedoStack.Peek()
        if t == nil {
                return
        }
 
-       te := t.(*TextEvent)
-       startTime := t.(*TextEvent).time.UnixNano() / int64(time.Millisecond)
+       startTime := t.Time.UnixNano() / int64(time.Millisecond)
 
        eh.RedoOneEvent()
 
        for {
-               t = eh.redo.Peek()
+               t = eh.RedoStack.Peek()
                if t == nil {
                        return
                }
 
-               te = t.(*TextEvent)
-
-               if (te.time.UnixNano()/int64(time.Millisecond))-startTime > undoThreshold {
+               if (t.Time.UnixNano()/int64(time.Millisecond))-startTime > undoThreshold {
                        return
                }
 
@@ -179,18 +229,17 @@ func (eh *EventHandler) Redo() {
 
 // RedoOneEvent redoes one event
 func (eh *EventHandler) RedoOneEvent() {
-       t := eh.redo.Pop()
+       t := eh.RedoStack.Pop()
        if t == nil {
                return
        }
 
-       te := t.(*TextEvent)
        // Modifies the text event
-       UndoTextEvent(te)
+       UndoTextEvent(t, eh.buf)
 
-       teCursor := te.c
-       te.c = eh.v.cursor
-       eh.v.cursor = teCursor
+       teCursor := t.C
+       t.C = eh.buf.Cursor
+       eh.buf.Cursor.Goto(teCursor)
 
-       eh.undo.Push(te)
+       eh.UndoStack.Push(t)
 }