Fix highlighting and searching with unicode characters

Fixes #134
Fixes #138
This commit is contained in:
Zachary Yedidia 2016-05-25 11:49:04 -04:00
parent ca58fc949e
commit 66d448a59c
3 changed files with 12 additions and 5 deletions

View file

@ -416,8 +416,8 @@ func Match(v *View) SyntaxMatches {
if rule.startend {
if indicies := rule.regex.FindAllStringIndex(str, -1); indicies != nil {
for _, value := range indicies {
value[0] += startNum
value[1] += startNum
value[0] = runePos(value[0], str) + startNum
value[1] = runePos(value[1], str) + startNum
for i := value[0]; i < value[1]; i++ {
if i < toplineNum {
continue
@ -437,7 +437,9 @@ func Match(v *View) SyntaxMatches {
for lineN, line := range lines {
if indicies := rule.regex.FindAllStringIndex(line, -1); indicies != nil {
for _, value := range indicies {
for i := value[0]; i < value[1]; i++ {
start := runePos(value[0], line)
end := runePos(value[1], line)
for i := start; i < end; i++ {
matches[lineN][i] = rule.style
}
}

View file

@ -106,6 +106,7 @@ func Search(searchStr string, v *View, down bool) {
} else {
match = matches[0]
}
str = text
}
if !down {
@ -118,8 +119,8 @@ func Search(searchStr string, v *View, down bool) {
return
}
v.Cursor.curSelection[0] = charPos + match[0]
v.Cursor.curSelection[1] = charPos + match[1]
v.Cursor.curSelection[0] = charPos + runePos(match[0], str)
v.Cursor.curSelection[1] = charPos + runePos(match[1], str)
v.Cursor.x, v.Cursor.y = FromCharPos(charPos+match[1]-1, v.Buf)
if v.Relocate() {
v.matches = Match(v)

View file

@ -117,3 +117,7 @@ func ParseBool(str string) (bool, error) {
}
return strconv.ParseBool(str)
}
func runePos(p int, str string) int {
return utf8.RuneCountInString(str[:p])
}