Move cursor selections on Insert or Remove

Ref #715
This commit is contained in:
Zachary Yedidia
2017-06-25 19:14:01 -04:00
parent 3a02ad8664
commit 8f4820ba28
2 changed files with 25 additions and 11 deletions

View File

@@ -316,7 +316,7 @@ func (b *Buffer) MergeCursors() {
if c1 != nil { if c1 != nil {
for j := 0; j < len(b.cursors); j++ { for j := 0; j < len(b.cursors); j++ {
c2 := b.cursors[j] c2 := b.cursors[j]
if i != j && c1.Loc == c2.Loc { if c2 != nil && i != j && c1.Loc == c2.Loc {
b.cursors[j] = nil b.cursors[j] = nil
} }
} }

View File

@@ -107,11 +107,19 @@ func (eh *EventHandler) Insert(start Loc, text string) {
end := e.Deltas[0].End end := e.Deltas[0].End
for _, c := range eh.buf.cursors { for _, c := range eh.buf.cursors {
if start.Y != end.Y && c.GreaterThan(start) { move := func(loc Loc) Loc {
c.Loc.Y += end.Y - start.Y if start.Y != end.Y && loc.GreaterThan(start) {
} else if c.Y == start.Y && c.GreaterEqual(start) { loc.Y += end.Y - start.Y
c.Loc = c.Move(Count(text), eh.buf) } else if loc.Y == start.Y && loc.GreaterEqual(start) {
loc = loc.Move(Count(text), eh.buf)
}
return loc
} }
c.Loc = move(c.Loc)
c.CurSelection[0] = move(c.CurSelection[0])
c.CurSelection[1] = move(c.CurSelection[1])
c.OrigSelection[0] = move(c.OrigSelection[0])
c.OrigSelection[1] = move(c.OrigSelection[1])
c.LastVisualX = c.GetVisualX() c.LastVisualX = c.GetVisualX()
} }
} }
@@ -127,13 +135,19 @@ func (eh *EventHandler) Remove(start, end Loc) {
eh.Execute(e) eh.Execute(e)
for _, c := range eh.buf.cursors { for _, c := range eh.buf.cursors {
if start.Y != end.Y && c.GreaterThan(end) { move := func(loc Loc) Loc {
c.Loc.Y -= end.Y - start.Y if start.Y != end.Y && loc.GreaterThan(end) {
} else if c.Y == end.Y && c.GreaterEqual(end) { loc.Y -= end.Y - start.Y
// TermMessage(start, end) } else if loc.Y == end.Y && loc.GreaterEqual(end) {
c.Loc = c.Move(-Diff(start, end, eh.buf), eh.buf) loc = loc.Move(-Diff(start, end, eh.buf), eh.buf)
// c.Loc = c.Move(ToCharPos(start, eh.buf)-ToCharPos(end, eh.buf), eh.buf) }
return loc
} }
c.Loc = move(c.Loc)
c.CurSelection[0] = move(c.CurSelection[0])
c.CurSelection[1] = move(c.CurSelection[1])
c.OrigSelection[0] = move(c.OrigSelection[0])
c.OrigSelection[1] = move(c.OrigSelection[1])
c.LastVisualX = c.GetVisualX() c.LastVisualX = c.GetVisualX()
} }
} }