Fix multi cursor relocate

This commit is contained in:
Zachary Yedidia
2019-01-16 17:52:30 -05:00
parent 1a710272f8
commit 254b892a3b
6 changed files with 29 additions and 10 deletions

View File

@@ -2,6 +2,7 @@ package action
import (
"os"
"regexp"
"strings"
"time"
"unicode/utf8"
@@ -1186,10 +1187,11 @@ func (h *BufHandler) SpawnMultiCursor() bool {
searchStart := spawner.CurSelection[1]
search := string(sel)
search = regexp.QuoteMeta(search)
if h.multiWord {
search = "\\b" + search + "\\b"
}
match, found, err := h.Buf.FindNext(search, h.Buf.Start(), h.Buf.End(), searchStart, true, false)
match, found, err := h.Buf.FindNext(search, h.Buf.Start(), h.Buf.End(), searchStart, true, true)
if err != nil {
InfoBar.Error(err)
}
@@ -1202,6 +1204,7 @@ func (h *BufHandler) SpawnMultiCursor() bool {
c.Loc = c.CurSelection[1]
h.Buf.AddCursor(c)
h.Buf.SetCurCursor(h.Buf.NumCursors() - 1)
h.Buf.MergeCursors()
} else {
InfoBar.Message("No matches found")
@@ -1262,7 +1265,13 @@ func (h *BufHandler) SkipMultiCursor() bool {
sel := lastC.GetSelection()
searchStart := lastC.CurSelection[1]
match, found, err := h.Buf.FindNext(string(sel), h.Buf.Start(), h.Buf.End(), searchStart, true, false)
search := string(sel)
search = regexp.QuoteMeta(search)
if h.multiWord {
search = "\\b" + search + "\\b"
}
match, found, err := h.Buf.FindNext(search, h.Buf.Start(), h.Buf.End(), searchStart, true, true)
if err != nil {
InfoBar.Error(err)
}
@@ -1274,22 +1283,23 @@ func (h *BufHandler) SkipMultiCursor() bool {
lastC.Loc = lastC.CurSelection[1]
h.Buf.MergeCursors()
h.Relocate()
h.Buf.SetCurCursor(h.Buf.NumCursors() - 1)
} else {
InfoBar.Message("No matches found")
}
return false
return true
}
// RemoveMultiCursor removes the latest multiple cursor
func (h *BufHandler) RemoveMultiCursor() bool {
if h.Buf.NumCursors() > 1 {
h.Buf.RemoveCursor(h.Buf.NumCursors() - 1)
h.Buf.SetCurCursor(h.Buf.NumCursors() - 1)
h.Buf.UpdateCursors()
} else {
h.multiWord = false
}
return false
return true
}
// RemoveAllMultiCursors removes all cursors except the base cursor

View File

@@ -253,6 +253,7 @@ func (h *BufHandler) DoRuneInsert(r rune) {
cursors := h.Buf.GetCursors()
for _, c := range cursors {
// Insert a character
h.Buf.SetCurCursor(c.Num)
if c.HasSelection() {
c.DeleteSelection()
c.ResetSelection()

View File

@@ -242,11 +242,13 @@ func (b *Buffer) SetName(s string) {
func (b *Buffer) Insert(start Loc, text []byte) {
b.EventHandler.cursors = b.cursors
b.EventHandler.active = b.curCursor
b.EventHandler.Insert(start, text)
}
func (b *Buffer) Remove(start, end Loc) {
b.EventHandler.cursors = b.cursors
b.EventHandler.active = b.curCursor
b.EventHandler.Remove(start, end)
}
@@ -428,12 +430,14 @@ func (b *Buffer) IndentString(tabsize int) []byte {
func (b *Buffer) SetCursors(c []*Cursor) {
b.cursors = c
b.EventHandler.cursors = b.cursors
b.EventHandler.active = b.curCursor
}
// AddCursor adds a new cursor to the list
func (b *Buffer) AddCursor(c *Cursor) {
b.cursors = append(b.cursors, c)
b.EventHandler.cursors = b.cursors
b.EventHandler.active = b.curCursor
b.UpdateCursors()
}
@@ -489,10 +493,13 @@ func (b *Buffer) MergeCursors() {
b.curCursor = len(b.cursors) - 1
}
b.EventHandler.cursors = b.cursors
b.EventHandler.active = b.curCursor
}
// UpdateCursors updates all the cursors indicies
func (b *Buffer) UpdateCursors() {
b.EventHandler.cursors = b.cursors
b.EventHandler.active = b.curCursor
for i, c := range b.cursors {
c.Num = i
}
@@ -502,6 +509,8 @@ func (b *Buffer) RemoveCursor(i int) {
copy(b.cursors[i:], b.cursors[i+1:])
b.cursors[len(b.cursors)-1] = nil
b.cursors = b.cursors[:len(b.cursors)-1]
b.curCursor = Clamp(b.curCursor, 0, len(b.cursors)-1)
b.UpdateCursors()
}
// ClearCursors removes all extra cursors

View File

@@ -69,6 +69,7 @@ func UndoTextEvent(t *TextEvent, buf *SharedBuffer) {
type EventHandler struct {
buf *SharedBuffer
cursors []*Cursor
active int
UndoStack *TEStack
RedoStack *TEStack
}
@@ -106,7 +107,7 @@ func (eh *EventHandler) ApplyDiff(str string) {
// Insert creates an insert text event and executes it
func (eh *EventHandler) Insert(start Loc, text []byte) {
e := &TextEvent{
C: *eh.cursors[0],
C: *eh.cursors[eh.active],
EventType: TextEventInsert,
Deltas: []Delta{{text, start, Loc{0, 0}}},
Time: time.Now(),
@@ -136,7 +137,7 @@ func (eh *EventHandler) Insert(start Loc, text []byte) {
// Remove creates a remove text event and executes it
func (eh *EventHandler) Remove(start, end Loc) {
e := &TextEvent{
C: *eh.cursors[0],
C: *eh.cursors[eh.active],
EventType: TextEventRemove,
Deltas: []Delta{{[]byte{}, start, end}},
Time: time.Now(),
@@ -164,7 +165,7 @@ func (eh *EventHandler) Remove(start, end Loc) {
// MultipleReplace creates an multiple insertions executes them
func (eh *EventHandler) MultipleReplace(deltas []Delta) {
e := &TextEvent{
C: *eh.cursors[0],
C: *eh.cursors[eh.active],
EventType: TextEventReplace,
Deltas: deltas,
Time: time.Now(),

View File

@@ -144,7 +144,6 @@ func (b *Buffer) ReplaceRegex(start, end Loc, search *regexp.Regexp, replace []b
l := b.lines[i].data
charpos := 0
// TODO: replace within X coords of selection
if start.Y == end.Y && i == start.Y {
l = util.SliceStart(l, end.X)
l = util.SliceEnd(l, start.X)

View File

@@ -364,7 +364,6 @@ func (w *BufWindow) displayBuffer() {
bufHeight--
}
// TODO: Rehighlighting
start := w.StartLine
if b.Settings["syntax"].(bool) && b.SyntaxDef != nil {
if start > 0 && b.Rehighlight(start-1) {