Skip to content

Commit 046b214

Browse files
committed
added FindNextSubmatch
1 parent 56c1f75 commit 046b214

1 file changed

Lines changed: 39 additions & 23 deletions

File tree

internal/buffer/search.go

Lines changed: 39 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"github.com/zyedidia/micro/v2/internal/util"
77
)
88

9-
func (b *Buffer) findDown(r *regexp.Regexp, start, end Loc) ([2]Loc, bool) {
9+
func (b *Buffer) findDown(r *regexp.Regexp, start, end Loc) ([]Loc, bool) {
1010
lastcn := util.CharacterCount(b.LineBytes(b.LinesNum() - 1))
1111
if start.Y > b.LinesNum()-1 {
1212
start.X = lastcn - 1
@@ -43,18 +43,20 @@ func (b *Buffer) findDown(r *regexp.Regexp, start, end Loc) ([2]Loc, bool) {
4343
l = util.SliceStart(l, end.X)
4444
}
4545

46-
match := r.FindIndex(l)
46+
match := r.FindSubmatchIndex(l)
4747

4848
if match != nil {
49-
start := Loc{charpos + util.RunePos(l, match[0]), i}
50-
end := Loc{charpos + util.RunePos(l, match[1]), i}
51-
return [2]Loc{start, end}, true
49+
loc := make([]Loc, len(match))
50+
for j, x := range match {
51+
loc[j] = Loc{charpos + util.RunePos(l, x), i}
52+
}
53+
return loc, true
5254
}
5355
}
54-
return [2]Loc{}, false
56+
return []Loc{}, false
5557
}
5658

57-
func (b *Buffer) findUp(r *regexp.Regexp, start, end Loc) ([2]Loc, bool) {
59+
func (b *Buffer) findUp(r *regexp.Regexp, start, end Loc) ([]Loc, bool) {
5860
lastcn := util.CharacterCount(b.LineBytes(b.LinesNum() - 1))
5961
if start.Y > b.LinesNum()-1 {
6062
start.X = lastcn - 1
@@ -91,46 +93,44 @@ func (b *Buffer) findUp(r *regexp.Regexp, start, end Loc) ([2]Loc, bool) {
9193
l = util.SliceStart(l, end.X)
9294
}
9395

94-
allMatches := r.FindAllIndex(l, -1)
96+
allMatches := r.FindAllSubmatchIndex(l, -1)
9597

9698
if allMatches != nil {
9799
match := allMatches[len(allMatches)-1]
98-
start := Loc{charpos + util.RunePos(l, match[0]), i}
99-
end := Loc{charpos + util.RunePos(l, match[1]), i}
100-
return [2]Loc{start, end}, true
100+
loc := make([]Loc, len(match))
101+
for j, x := range match {
102+
loc[j] = Loc{charpos + util.RunePos(l, x), i}
103+
}
104+
return loc, true
101105
}
102106
}
103-
return [2]Loc{}, false
107+
return []Loc{}, false
104108
}
105109

106-
// FindNext finds the next occurrence of a given string in the buffer
107-
// It returns the start and end location of the match (if found) and
108-
// a boolean indicating if it was found
110+
// FindNextSubmatch finds the next occurrence of a given string in the
111+
// buffer. It returns the start and end location of the match and of
112+
// all submatches (if found) and a boolean indicating if it was found.
109113
// May also return an error if the search regex is invalid
110-
func (b *Buffer) FindNext(s string, start, end, from Loc, down bool, useRegex bool) ([2]Loc, bool, error) {
114+
func (b *Buffer) FindNextSubmatch(s string, start, end, from Loc, down bool) ([]Loc, bool, error) {
111115
if s == "" {
112-
return [2]Loc{}, false, nil
116+
return []Loc{}, false, nil
113117
}
114118

115119
var r *regexp.Regexp
116120
var err error
117121

118-
if !useRegex {
119-
s = regexp.QuoteMeta(s)
120-
}
121-
122122
if b.Settings["ignorecase"].(bool) {
123123
r, err = regexp.Compile("(?i)" + s)
124124
} else {
125125
r, err = regexp.Compile(s)
126126
}
127127

128128
if err != nil {
129-
return [2]Loc{}, false, err
129+
return []Loc{}, false, err
130130
}
131131

132132
var found bool
133-
var l [2]Loc
133+
var l []Loc
134134
if down {
135135
l, found = b.findDown(r, from, end)
136136
if !found {
@@ -145,6 +145,22 @@ func (b *Buffer) FindNext(s string, start, end, from Loc, down bool, useRegex bo
145145
return l, found, nil
146146
}
147147

148+
// FindNext finds the next occurrence of a given string in the buffer
149+
// It returns the start and end location of the match (if found) and
150+
// a boolean indicating if it was found
151+
// May also return an error if the search regex is invalid
152+
func (b *Buffer) FindNext(s string, start, end, from Loc, down bool, useRegex bool) ([2]Loc, bool, error) {
153+
if !useRegex {
154+
s = regexp.QuoteMeta(s)
155+
}
156+
l, found, err := b.FindNextSubmatch(s, start, end, from, down)
157+
if found {
158+
return [2]Loc{l[0], l[1]}, found, err
159+
} else {
160+
return [2]Loc{}, found, err
161+
}
162+
}
163+
148164
// ReplaceRegex replaces all occurrences of 'search' with 'replace' in the given area
149165
// and returns the number of replacements made and the number of runes
150166
// added or removed on the last line of the range

0 commit comments

Comments
 (0)