mirror of
https://github.com/zyedidia/micro.git
synced 2026-03-23 17:27:14 +09:00
Optimize search and replace a lot
This commit is contained in:
@@ -323,13 +323,25 @@ func Replace(args []string) {
|
||||
}
|
||||
}
|
||||
} else {
|
||||
for {
|
||||
match := regex.FindStringIndex(view.Buf.String())
|
||||
if match == nil {
|
||||
break
|
||||
matches := regex.FindAllStringIndex(view.Buf.String(), -1)
|
||||
if matches != nil && len(matches) > 0 {
|
||||
adjust := 0
|
||||
prevMatch := matches[0]
|
||||
from := FromCharPos(prevMatch[0], view.Buf)
|
||||
to := from.Move(Count(search), view.Buf)
|
||||
adjust += Count(replace) - Count(search)
|
||||
view.Buf.Replace(from, to, replace)
|
||||
if len(matches) > 1 {
|
||||
for _, match := range matches[1:] {
|
||||
found++
|
||||
from = from.Move(match[0]-prevMatch[0]+adjust, view.Buf)
|
||||
to := from.Move(Count(search), view.Buf)
|
||||
// TermMessage(match[0], " ", prevMatch[0], " ", adjust, "\n", from, " ", to)
|
||||
view.Buf.Replace(from, to, replace)
|
||||
prevMatch = match
|
||||
// adjust += Count(replace) - Count(search)
|
||||
}
|
||||
}
|
||||
found++
|
||||
view.Buf.Replace(FromCharPos(match[0], view.Buf), FromCharPos(match[1], view.Buf), replace)
|
||||
}
|
||||
}
|
||||
view.Cursor.Relocate()
|
||||
|
||||
Reference in New Issue
Block a user