Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

match beginning and end of line correctly #3575

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 64 additions & 42 deletions internal/buffer/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,50 @@ import (
"github.com/zyedidia/micro/v2/internal/util"
)

const (
padStart = 1 << iota
padEnd
)

// We want "^" and "$" to match only the beginning/end of a line, not the
// beginning/end of the search region if it is in the middle of a line.
// In that case we use padded regexps to require a rune before or after
// the match. (This also affects other empty-string patters like "\\b".)
// The function padRegexp creates these padded regexps.
func padRegexp(r *regexp.Regexp) [4]*regexp.Regexp {
rPadStart := regexp.MustCompile(".(?:" + r.String() + ")")
rPadEnd := regexp.MustCompile("(?:" + r.String() + ").")
rPadBoth := regexp.MustCompile(".(?:" + r.String() + ").")
return [4]*regexp.Regexp{r, rPadStart, rPadEnd, rPadBoth}
}

func findLineParams(b *Buffer, start, end Loc, i int) ([]byte, int, int) {
l := b.LineBytes(i)
charpos := 0
padMode := 0

if i == end.Y {
nchars := util.CharacterCount(l)
end.X = util.Clamp(end.X, 0, nchars)
if end.X < nchars {
l = util.SliceStart(l, end.X+1)
padMode |= padEnd
}
}

if i == start.Y {
nchars := util.CharacterCount(l)
start.X = util.Clamp(start.X, 0, nchars)
if start.X > 0 {
charpos = start.X - 1
l = util.SliceEnd(l, charpos)
padMode |= padStart
}
}

return l, charpos, padMode
}

func (b *Buffer) findDown(r *regexp.Regexp, start, end Loc) ([2]Loc, bool) {
lastcn := util.CharacterCount(b.LineBytes(b.LinesNum() - 1))
if start.Y > b.LinesNum()-1 {
Expand All @@ -21,33 +65,22 @@ func (b *Buffer) findDown(r *regexp.Regexp, start, end Loc) ([2]Loc, bool) {
start, end = end, start
}

for i := start.Y; i <= end.Y; i++ {
l := b.LineBytes(i)
charpos := 0
rPadded := padRegexp(r)

if i == start.Y && start.Y == end.Y {
nchars := util.CharacterCount(l)
start.X = util.Clamp(start.X, 0, nchars)
end.X = util.Clamp(end.X, 0, nchars)
l = util.SliceStart(l, end.X)
l = util.SliceEnd(l, start.X)
charpos = start.X
} else if i == start.Y {
nchars := util.CharacterCount(l)
start.X = util.Clamp(start.X, 0, nchars)
l = util.SliceEnd(l, start.X)
charpos = start.X
} else if i == end.Y {
nchars := util.CharacterCount(l)
end.X = util.Clamp(end.X, 0, nchars)
l = util.SliceStart(l, end.X)
}
for i := start.Y; i <= end.Y; i++ {
l, charpos, padMode := findLineParams(b, start, end, i)

match := r.FindIndex(l)
match := rPadded[padMode].FindIndex(l)

if match != nil {
start := Loc{charpos + util.RunePos(l, match[0]), i}
if padMode&padStart != 0 {
start = start.Move(1, b)
}
end := Loc{charpos + util.RunePos(l, match[1]), i}
if padMode&padEnd != 0 {
end = end.Move(-1, b)
}
return [2]Loc{start, end}, true
}
}
Expand All @@ -69,34 +102,23 @@ func (b *Buffer) findUp(r *regexp.Regexp, start, end Loc) ([2]Loc, bool) {
start, end = end, start
}

for i := end.Y; i >= start.Y; i-- {
l := b.LineBytes(i)
charpos := 0
rPadded := padRegexp(r)

if i == start.Y && start.Y == end.Y {
nchars := util.CharacterCount(l)
start.X = util.Clamp(start.X, 0, nchars)
end.X = util.Clamp(end.X, 0, nchars)
l = util.SliceStart(l, end.X)
l = util.SliceEnd(l, start.X)
charpos = start.X
} else if i == start.Y {
nchars := util.CharacterCount(l)
start.X = util.Clamp(start.X, 0, nchars)
l = util.SliceEnd(l, start.X)
charpos = start.X
} else if i == end.Y {
nchars := util.CharacterCount(l)
end.X = util.Clamp(end.X, 0, nchars)
l = util.SliceStart(l, end.X)
}
for i := end.Y; i >= start.Y; i-- {
l, charpos, padMode := findLineParams(b, start, end, i)

allMatches := r.FindAllIndex(l, -1)
allMatches := rPadded[padMode].FindAllIndex(l, -1)

if allMatches != nil {
match := allMatches[len(allMatches)-1]
start := Loc{charpos + util.RunePos(l, match[0]), i}
if padMode&padStart != 0 {
start = start.Move(1, b)
}
end := Loc{charpos + util.RunePos(l, match[1]), i}
if padMode&padEnd != 0 {
end = end.Move(-1, b)
}
return [2]Loc{start, end}, true
}
}
Expand Down
Loading