From 8f4820ba28dc6cad6adbdfa405d1ce4a0ab7dfd3 Mon Sep 17 00:00:00 2001 From: Zachary Yedidia Date: Sun, 25 Jun 2017 19:14:01 -0400 Subject: [PATCH] Move cursor selections on Insert or Remove Ref #715 --- cmd/micro/buffer.go | 2 +- cmd/micro/eventhandler.go | 34 ++++++++++++++++++++++++---------- 2 files changed, 25 insertions(+), 11 deletions(-) diff --git a/cmd/micro/buffer.go b/cmd/micro/buffer.go index 6471baf0..258ea18b 100644 --- a/cmd/micro/buffer.go +++ b/cmd/micro/buffer.go @@ -316,7 +316,7 @@ func (b *Buffer) MergeCursors() { if c1 != nil { for j := 0; j < len(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 } } diff --git a/cmd/micro/eventhandler.go b/cmd/micro/eventhandler.go index 24bc593e..d50bdb0e 100644 --- a/cmd/micro/eventhandler.go +++ b/cmd/micro/eventhandler.go @@ -107,11 +107,19 @@ func (eh *EventHandler) Insert(start Loc, text string) { end := e.Deltas[0].End for _, c := range eh.buf.cursors { - if start.Y != end.Y && c.GreaterThan(start) { - c.Loc.Y += end.Y - start.Y - } else if c.Y == start.Y && c.GreaterEqual(start) { - c.Loc = c.Move(Count(text), eh.buf) + move := func(loc Loc) Loc { + if start.Y != end.Y && loc.GreaterThan(start) { + loc.Y += end.Y - start.Y + } 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() } } @@ -127,13 +135,19 @@ func (eh *EventHandler) Remove(start, end Loc) { eh.Execute(e) for _, c := range eh.buf.cursors { - if start.Y != end.Y && c.GreaterThan(end) { - c.Loc.Y -= end.Y - start.Y - } else if c.Y == end.Y && c.GreaterEqual(end) { - // TermMessage(start, end) - c.Loc = c.Move(-Diff(start, end, eh.buf), eh.buf) - // c.Loc = c.Move(ToCharPos(start, eh.buf)-ToCharPos(end, eh.buf), eh.buf) + move := func(loc Loc) Loc { + if start.Y != end.Y && loc.GreaterThan(end) { + loc.Y -= end.Y - start.Y + } else if loc.Y == end.Y && loc.GreaterEqual(end) { + loc = loc.Move(-Diff(start, 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() } }