DoTextEvent runs a text event
(t *TextEvent, useUndo bool)
| 43 | |
| 44 | // DoTextEvent runs a text event |
| 45 | func (eh *EventHandler) DoTextEvent(t *TextEvent, useUndo bool) { |
| 46 | oldl := eh.buf.LinesNum() |
| 47 | |
| 48 | if useUndo { |
| 49 | eh.Execute(t) |
| 50 | } else { |
| 51 | ExecuteTextEvent(t, eh.buf) |
| 52 | } |
| 53 | |
| 54 | if len(t.Deltas) != 1 { |
| 55 | return |
| 56 | } |
| 57 | |
| 58 | text := t.Deltas[0].Text |
| 59 | start := t.Deltas[0].Start |
| 60 | lastnl := -1 |
| 61 | var endX int |
| 62 | var textX int |
| 63 | if t.EventType == TextEventInsert { |
| 64 | linecount := eh.buf.LinesNum() - oldl |
| 65 | textcount := util.CharacterCount(text) |
| 66 | lastnl = bytes.LastIndex(text, []byte{'\n'}) |
| 67 | if lastnl >= 0 { |
| 68 | endX = util.CharacterCount(text[lastnl+1:]) |
| 69 | textX = endX |
| 70 | } else { |
| 71 | endX = start.X + textcount |
| 72 | textX = textcount |
| 73 | } |
| 74 | t.Deltas[0].End = clamp(Loc{endX, start.Y + linecount}, eh.buf.LineArray) |
| 75 | } |
| 76 | end := t.Deltas[0].End |
| 77 | |
| 78 | for _, c := range eh.cursors { |
| 79 | move := func(loc Loc) Loc { |
| 80 | if t.EventType == TextEventInsert { |
| 81 | if start.Y != loc.Y && loc.GreaterThan(start) { |
| 82 | loc.Y += end.Y - start.Y |
| 83 | } else if loc.Y == start.Y && loc.GreaterEqual(start) { |
| 84 | loc.Y += end.Y - start.Y |
| 85 | if lastnl >= 0 { |
| 86 | loc.X += textX - start.X |
| 87 | } else { |
| 88 | loc.X += textX |
| 89 | } |
| 90 | } |
| 91 | return loc |
| 92 | } else { |
| 93 | if loc.Y != end.Y && loc.GreaterThan(end) { |
| 94 | loc.Y -= end.Y - start.Y |
| 95 | } else if loc.Y == end.Y && loc.GreaterEqual(end) { |
| 96 | loc = loc.MoveLA(-DiffLA(start, end, eh.buf.LineArray), eh.buf.LineArray) |
| 97 | } |
| 98 | return loc |
| 99 | } |
| 100 | } |
| 101 | c.Loc = move(c.Loc) |
| 102 | c.CurSelection[0] = move(c.CurSelection[0]) |
no test coverage detected