From aa0fefcaa18e49a654142d759b46247fad314f89 Mon Sep 17 00:00:00 2001 From: matthias314 <56549971+matthias314@users.noreply.github.com> Date: Tue, 17 Dec 2024 12:44:48 -0500 Subject: [PATCH] skip empty match right after previous match in `ReplaceCmd` (#3566) --- internal/action/command.go | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/internal/action/command.go b/internal/action/command.go index 3b9d3b81..211e237c 100644 --- a/internal/action/command.go +++ b/internal/action/command.go @@ -950,10 +950,12 @@ func (h *BufPane) ReplaceCmd(args []string) { nreplaced := 0 start := h.Buf.Start() end := h.Buf.End() + searchLoc := h.Cursor.Loc selection := h.Cursor.HasSelection() if selection { start = h.Cursor.CurSelection[0] end = h.Cursor.CurSelection[1] + searchLoc = start // otherwise me might start at the end } if all { nreplaced, _ = h.Buf.ReplaceRegex(start, end, regex, replace, !noRegex) @@ -962,7 +964,7 @@ func (h *BufPane) ReplaceCmd(args []string) { return l.GreaterEqual(start) && l.LessEqual(end) } - searchLoc := h.Cursor.Loc + lastMatchEnd := buffer.Loc{-1, -1} var doReplacement func() doReplacement = func() { locs, found, err := h.Buf.FindNext(search, start, end, searchLoc, true, true) @@ -977,6 +979,18 @@ func (h *BufPane) ReplaceCmd(args []string) { return } + if lastMatchEnd == locs[1] { + // skip empty match right after previous match + if searchLoc == end { + searchLoc = start + lastMatchEnd = buffer.Loc{-1, -1} + } else { + searchLoc = searchLoc.Move(1, h.Buf) + } + doReplacement() + return + } + h.Cursor.SetSelectionStart(locs[0]) h.Cursor.SetSelectionEnd(locs[1]) h.GotoLoc(locs[0]) @@ -1002,6 +1016,7 @@ func (h *BufPane) ReplaceCmd(args []string) { h.Buf.RelocateCursors() return } + lastMatchEnd = searchLoc doReplacement() }) }