Merge cursors properly

Cursors will merge together if they are on top of each other.
This commit is contained in:
Zachary Yedidia
2017-06-17 10:43:14 -04:00
parent f933b90c66
commit 118e6b1804
3 changed files with 26 additions and 8 deletions

View File

@@ -1916,17 +1916,15 @@ func (v *View) SkipMultiCursor(usePlugin bool) bool {
func (v *View) RemoveMultiCursor(usePlugin bool) bool {
end := len(v.Buf.cursors)
if end > 1 {
nextOne := v.Buf.cursors[len(v.Buf.cursors)-2]
if v.Cursor == nextOne {
lastOne := v.Buf.cursors[end-1]
if v.Cursor == lastOne {
if usePlugin && !PreActionCall("RemoveMultiCursor", v) {
return false
}
if end > 1 {
v.Buf.cursors[end-1] = nil
v.Buf.cursors = v.Buf.cursors[:end-1]
v.Buf.UpdateCursors()
}
v.Buf.cursors[end-1] = nil
v.Buf.cursors = v.Buf.cursors[:end-1]
v.Buf.UpdateCursors()
v.Relocate()
if usePlugin {
@@ -1942,7 +1940,7 @@ func (v *View) RemoveMultiCursor(usePlugin bool) bool {
// RemoveAllMultiCursors removes all cursors except the base cursor
func (v *View) RemoveAllMultiCursors(usePlugin bool) bool {
if v.Cursor == &v.Buf.Cursor {
if v.Cursor == v.Buf.cursors[len(v.Buf.cursors)-1] {
if usePlugin && !PreActionCall("RemoveAllMultiCursors", v) {
return false
}

View File

@@ -309,6 +309,24 @@ func (b *Buffer) Update() {
b.NumLines = len(b.lines)
}
func (b *Buffer) MergeCursors() {
var cursors []*Cursor
for i := 0; i < len(b.cursors); i++ {
c1 := b.cursors[i]
if c1 != nil {
for j := 0; j < len(b.cursors); j++ {
c2 := b.cursors[j]
if i != j && c1.Loc == c2.Loc {
b.cursors[j] = nil
}
}
cursors = append(cursors, c1)
}
}
b.cursors = cursors
}
func (b *Buffer) UpdateCursors() {
for i, c := range b.cursors {
c.Num = i

View File

@@ -511,6 +511,7 @@ func (v *View) HandleEvent(event tcell.Event) {
relocate = v.ExecuteActions(actions) || relocate
}
v.SetCursor(&v.Buf.Cursor)
v.Buf.MergeCursors()
break
}
}
@@ -571,6 +572,7 @@ func (v *View) HandleEvent(event tcell.Event) {
relocate = v.ExecuteActions(actions) || relocate
}
v.SetCursor(&v.Buf.Cursor)
v.Buf.MergeCursors()
}
}