Search and replace within a selection

Closes #1098
This commit is contained in:
Zachary Yedidia
2020-02-13 19:48:48 -05:00
parent faa207907c
commit 38f88ade60
4 changed files with 30 additions and 26 deletions

View File

@@ -102,7 +102,7 @@ func (l Loc) left(buf *LineArray) Loc {
return res
}
// Move moves the cursor n characters to the left or right
// MoveLA moves the cursor n characters to the left or right
// It moves the cursor left if n is negative
func (l Loc) MoveLA(n int, buf *LineArray) Loc {
if n > 0 {
@@ -117,9 +117,12 @@ func (l Loc) MoveLA(n int, buf *LineArray) Loc {
return l
}
func (l Loc) Diff(a, b Loc, buf *Buffer) int {
return DiffLA(a, b, buf.LineArray)
// Diff returns the difference between two locs
func (l Loc) Diff(b Loc, buf *Buffer) int {
return DiffLA(l, b, buf.LineArray)
}
// Move moves a loc n characters
func (l Loc) Move(n int, buf *Buffer) Loc {
return l.MoveLA(n, buf.LineArray)
}

View File

@@ -120,24 +120,27 @@ func (b *Buffer) FindNext(s string, start, end, from Loc, down bool, useRegex bo
if down {
l, found = b.findDown(r, from, end)
if !found {
l, found = b.findDown(r, start, from)
l, found = b.findDown(r, start, end)
}
} else {
l, found = b.findUp(r, from, start)
if !found {
l, found = b.findUp(r, end, from)
l, found = b.findUp(r, end, start)
}
}
return l, found, nil
}
// ReplaceRegex replaces all occurrences of 'search' with 'replace' in the given area
// and returns the number of replacements made
func (b *Buffer) ReplaceRegex(start, end Loc, search *regexp.Regexp, replace []byte) int {
// and returns the number of replacements made and the number of runes
// added or removed
func (b *Buffer) ReplaceRegex(start, end Loc, search *regexp.Regexp, replace []byte) (int, int) {
if start.GreaterThan(end) {
start, end = end, start
}
netrunes := 0
found := 0
var deltas []Delta
for i := start.Y; i <= end.Y; i++ {
@@ -160,6 +163,7 @@ func (b *Buffer) ReplaceRegex(start, end Loc, search *regexp.Regexp, replace []b
result = search.Expand(result, replace, in, submatches)
}
found++
netrunes += utf8.RuneCount(in) - utf8.RuneCount(result)
return result
})
@@ -170,5 +174,5 @@ func (b *Buffer) ReplaceRegex(start, end Loc, search *regexp.Regexp, replace []b
}
b.MultipleReplace(deltas)
return found
return found, netrunes
}