Skip to content

Commit aa0fefc

Browse files
authored
skip empty match right after previous match in ReplaceCmd (zyedidia#3566)
1 parent 8cdf68b commit aa0fefc

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

internal/action/command.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -950,10 +950,12 @@ func (h *BufPane) ReplaceCmd(args []string) {
950950
nreplaced := 0
951951
start := h.Buf.Start()
952952
end := h.Buf.End()
953+
searchLoc := h.Cursor.Loc
953954
selection := h.Cursor.HasSelection()
954955
if selection {
955956
start = h.Cursor.CurSelection[0]
956957
end = h.Cursor.CurSelection[1]
958+
searchLoc = start // otherwise me might start at the end
957959
}
958960
if all {
959961
nreplaced, _ = h.Buf.ReplaceRegex(start, end, regex, replace, !noRegex)
@@ -962,7 +964,7 @@ func (h *BufPane) ReplaceCmd(args []string) {
962964
return l.GreaterEqual(start) && l.LessEqual(end)
963965
}
964966

965-
searchLoc := h.Cursor.Loc
967+
lastMatchEnd := buffer.Loc{-1, -1}
966968
var doReplacement func()
967969
doReplacement = func() {
968970
locs, found, err := h.Buf.FindNext(search, start, end, searchLoc, true, true)
@@ -977,6 +979,18 @@ func (h *BufPane) ReplaceCmd(args []string) {
977979
return
978980
}
979981

982+
if lastMatchEnd == locs[1] {
983+
// skip empty match right after previous match
984+
if searchLoc == end {
985+
searchLoc = start
986+
lastMatchEnd = buffer.Loc{-1, -1}
987+
} else {
988+
searchLoc = searchLoc.Move(1, h.Buf)
989+
}
990+
doReplacement()
991+
return
992+
}
993+
980994
h.Cursor.SetSelectionStart(locs[0])
981995
h.Cursor.SetSelectionEnd(locs[1])
982996
h.GotoLoc(locs[0])
@@ -1002,6 +1016,7 @@ func (h *BufPane) ReplaceCmd(args []string) {
10021016
h.Buf.RelocateCursors()
10031017
return
10041018
}
1019+
lastMatchEnd = searchLoc
10051020
doReplacement()
10061021
})
10071022
}

0 commit comments

Comments
 (0)