2019-02-12 11:13:49 -07:00
|
|
|
// Copyright 2019 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package span
|
|
|
|
|
|
|
|
import (
|
2019-03-15 11:19:43 -06:00
|
|
|
"fmt"
|
2019-02-12 11:13:49 -07:00
|
|
|
"go/token"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Range represents a source code range in token.Pos form.
|
|
|
|
// It also carries the FileSet that produced the positions, so that it is
|
|
|
|
// self contained.
|
|
|
|
type Range struct {
|
|
|
|
FileSet *token.FileSet
|
|
|
|
Start token.Pos
|
|
|
|
End token.Pos
|
|
|
|
}
|
|
|
|
|
|
|
|
// TokenConverter is a Converter backed by a token file set and file.
|
|
|
|
// It uses the file set methods to work out determine the conversions which
|
|
|
|
// make if fast and do not require the file contents.
|
|
|
|
type TokenConverter struct {
|
|
|
|
fset *token.FileSet
|
|
|
|
file *token.File
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewRange creates a new Range from a FileSet and two positions.
|
|
|
|
// To represent a point pass a 0 as the end pos.
|
|
|
|
func NewRange(fset *token.FileSet, start, end token.Pos) Range {
|
|
|
|
return Range{
|
|
|
|
FileSet: fset,
|
|
|
|
Start: start,
|
|
|
|
End: end,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-15 11:19:43 -06:00
|
|
|
// NewTokenConverter returns an implementation of Converter backed by a
|
2019-02-12 11:13:49 -07:00
|
|
|
// token.File.
|
|
|
|
func NewTokenConverter(fset *token.FileSet, f *token.File) *TokenConverter {
|
|
|
|
return &TokenConverter{fset: fset, file: f}
|
|
|
|
}
|
|
|
|
|
2019-03-15 11:19:43 -06:00
|
|
|
// NewContentConverter returns an implementation of Converter for the
|
2019-02-12 11:13:49 -07:00
|
|
|
// given file content.
|
2019-02-19 19:11:15 -07:00
|
|
|
func NewContentConverter(filename string, content []byte) *TokenConverter {
|
2019-02-12 11:13:49 -07:00
|
|
|
fset := token.NewFileSet()
|
|
|
|
f := fset.AddFile(filename, -1, len(content))
|
|
|
|
f.SetLinesForContent(content)
|
|
|
|
return &TokenConverter{fset: fset, file: f}
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsPoint returns true if the range represents a single point.
|
|
|
|
func (r Range) IsPoint() bool {
|
|
|
|
return r.Start == r.End
|
|
|
|
}
|
|
|
|
|
|
|
|
// Span converts a Range to a Span that represents the Range.
|
|
|
|
// It will fill in all the members of the Span, calculating the line and column
|
|
|
|
// information.
|
2019-03-15 11:19:43 -06:00
|
|
|
func (r Range) Span() (Span, error) {
|
2019-02-12 11:13:49 -07:00
|
|
|
f := r.FileSet.File(r.Start)
|
2019-03-15 11:19:43 -06:00
|
|
|
if f == nil {
|
|
|
|
return Span{}, fmt.Errorf("file not found in FileSet")
|
|
|
|
}
|
|
|
|
s := Span{v: span{URI: FileURI(f.Name())}}
|
2019-03-26 17:48:42 -06:00
|
|
|
if !r.Start.IsValid() {
|
|
|
|
return Span{}, fmt.Errorf("invalid position for start of range")
|
|
|
|
}
|
2019-03-15 11:19:43 -06:00
|
|
|
s.v.Start.Offset = f.Offset(r.Start)
|
2019-02-12 11:13:49 -07:00
|
|
|
if r.End.IsValid() {
|
2019-03-15 11:19:43 -06:00
|
|
|
s.v.End.Offset = f.Offset(r.End)
|
2019-02-12 11:13:49 -07:00
|
|
|
}
|
2019-03-15 11:19:43 -06:00
|
|
|
s.v.Start.clean()
|
|
|
|
s.v.End.clean()
|
|
|
|
s.v.clean()
|
2019-02-12 11:13:49 -07:00
|
|
|
converter := NewTokenConverter(r.FileSet, f)
|
2019-03-15 11:19:43 -06:00
|
|
|
return s.WithPosition(converter)
|
2019-02-12 11:13:49 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Range converts a Span to a Range that represents the Span for the supplied
|
|
|
|
// File.
|
2019-03-15 11:19:43 -06:00
|
|
|
func (s Span) Range(converter *TokenConverter) (Range, error) {
|
|
|
|
s, err := s.WithOffset(converter)
|
|
|
|
if err != nil {
|
|
|
|
return Range{}, err
|
|
|
|
}
|
2019-02-12 11:13:49 -07:00
|
|
|
return Range{
|
|
|
|
FileSet: converter.fset,
|
2019-03-15 11:19:43 -06:00
|
|
|
Start: converter.file.Pos(s.Start().Offset()),
|
|
|
|
End: converter.file.Pos(s.End().Offset()),
|
|
|
|
}, nil
|
2019-02-12 11:13:49 -07:00
|
|
|
}
|
|
|
|
|
2019-03-15 11:19:43 -06:00
|
|
|
func (l *TokenConverter) ToPosition(offset int) (int, int, error) {
|
|
|
|
//TODO: check offset fits in file
|
2019-02-12 11:13:49 -07:00
|
|
|
pos := l.file.Pos(offset)
|
|
|
|
p := l.fset.Position(pos)
|
2019-03-15 11:19:43 -06:00
|
|
|
return p.Line, p.Column, nil
|
2019-02-12 11:13:49 -07:00
|
|
|
}
|
|
|
|
|
2019-03-15 11:19:43 -06:00
|
|
|
func (l *TokenConverter) ToOffset(line, col int) (int, error) {
|
2019-02-12 11:13:49 -07:00
|
|
|
if line < 0 {
|
2019-03-15 11:19:43 -06:00
|
|
|
return -1, fmt.Errorf("line is not valid")
|
2019-02-12 11:13:49 -07:00
|
|
|
}
|
|
|
|
lineMax := l.file.LineCount() + 1
|
|
|
|
if line > lineMax {
|
2019-03-15 11:19:43 -06:00
|
|
|
return -1, fmt.Errorf("line is beyond end of file")
|
2019-02-12 11:13:49 -07:00
|
|
|
} else if line == lineMax {
|
|
|
|
if col > 1 {
|
2019-03-15 11:19:43 -06:00
|
|
|
return -1, fmt.Errorf("column is beyond end of file")
|
2019-02-12 11:13:49 -07:00
|
|
|
}
|
|
|
|
// at the end of the file, allowing for a trailing eol
|
2019-03-15 11:19:43 -06:00
|
|
|
return l.file.Size(), nil
|
2019-02-12 11:13:49 -07:00
|
|
|
}
|
|
|
|
pos := lineStart(l.file, line)
|
|
|
|
if !pos.IsValid() {
|
2019-03-15 11:19:43 -06:00
|
|
|
return -1, fmt.Errorf("line is not in file")
|
2019-02-12 11:13:49 -07:00
|
|
|
}
|
|
|
|
// we assume that column is in bytes here, and that the first byte of a
|
|
|
|
// line is at column 1
|
|
|
|
pos += token.Pos(col - 1)
|
2019-03-15 11:19:43 -06:00
|
|
|
return l.file.Offset(pos), nil
|
2019-02-12 11:13:49 -07:00
|
|
|
}
|