Add ByteOffset and ToCharPos to plugin API

This commit is contained in:
Zachary Yedidia
2016-10-28 20:34:28 -04:00
parent 63ccbc1ebd
commit ad0e098a25
4 changed files with 19 additions and 1 deletions

View File

@@ -28,6 +28,18 @@ func ToCharPos(start Loc, buf *Buffer) int {
return loc
}
// ByteOffset is just like ToCharPos except it counts bytes instead of runes
func ByteOffset(pos Loc, buf *Buffer) int {
x, y := pos.X, pos.Y
loc := 0
for i := 0; i < y; i++ {
// + 1 for the newline
loc += len(buf.Line(i)) + 1
}
loc += len(buf.Line(y)[:x])
return loc
}
// Loc stores a location
type Loc struct {
X, Y int