Skip to content

Commit

Permalink
cmd/govim/internal/types: add PointFromOffset (#860)
Browse files Browse the repository at this point in the history
This is used in the later Signature Help PR, where we need to translate
from byte offsets in a file (which we get from go/token.Pos values) into
Points.
  • Loading branch information
myitcv committed Apr 23, 2020
1 parent 1986dfd commit 97f9177
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions cmd/govim/internal/types/point.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,26 @@ type Point struct {
utf16Col int
}

func PointFromOffset(b *Buffer, offset int) (Point, error) {
cc := b.tokenConvertor()
line, col, err := cc.ToPosition(offset)
if err != nil {
return Point{}, fmt.Errorf("failed to calculate position within buffer %v: %v", b.Num, err)
}
p := span.NewPoint(line, col, offset)
utf16col, err := span.ToUTF16Column(p, b.contents)
if err != nil {
return Point{}, fmt.Errorf("failed to calculate UTF16 char value: %v", err)
}
res := Point{
line: line,
col: col,
offset: offset,
utf16Col: utf16col - 1,
}
return res, nil
}

func PointFromVim(b *Buffer, line, col int) (Point, error) {
cc := b.tokenConvertor()
off, err := cc.ToOffset(line, col)
Expand Down

0 comments on commit 97f9177

Please sign in to comment.