MCPcopy
hub / github.com/micro-editor/micro / DoTextEvent

Method DoTextEvent

internal/buffer/eventhandler.go:45–113  ·  view source on GitHub ↗

DoTextEvent runs a text event

(t *TextEvent, useUndo bool)

Source from the content-addressed store, hash-verified

43
44// DoTextEvent runs a text event
45func (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])

Callers 3

UndoTextEventMethod · 0.95
InsertBytesMethod · 0.95
RemoveMethod · 0.95

Calls 12

ExecuteMethod · 0.95
updateTrailingWsMethod · 0.95
CharacterCountFunction · 0.92
ExecuteTextEventFunction · 0.85
clampFunction · 0.85
DiffLAFunction · 0.85
MoveLAMethod · 0.80
StoreVisualXMethod · 0.80
LinesNumMethod · 0.65
RelocateMethod · 0.65
GreaterThanMethod · 0.45
GreaterEqualMethod · 0.45

Tested by

no test coverage detected