1 // Copyright 2019 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
12 // Range represents a source code range in token.Pos form.
13 // It also carries the FileSet that produced the positions, so that it is
16 FileSet *token.FileSet
22 type FileConverter struct {
26 // TokenConverter is a Converter backed by a token file set and file.
27 // It uses the file set methods to work out the conversions, which
28 // makes it fast and does not require the file contents.
29 type TokenConverter struct {
34 // NewRange creates a new Range from a FileSet and two positions.
35 // To represent a point pass a 0 as the end pos.
36 func NewRange(fset *token.FileSet, start, end token.Pos) Range {
44 // NewTokenConverter returns an implementation of Converter backed by a
46 func NewTokenConverter(fset *token.FileSet, f *token.File) *TokenConverter {
47 return &TokenConverter{fset: fset, FileConverter: FileConverter{file: f}}
50 // NewContentConverter returns an implementation of Converter for the
51 // given file content.
52 func NewContentConverter(filename string, content []byte) *TokenConverter {
53 fset := token.NewFileSet()
54 f := fset.AddFile(filename, -1, len(content))
55 f.SetLinesForContent(content)
56 return NewTokenConverter(fset, f)
59 // IsPoint returns true if the range represents a single point.
60 func (r Range) IsPoint() bool {
61 return r.Start == r.End
64 // Span converts a Range to a Span that represents the Range.
65 // It will fill in all the members of the Span, calculating the line and column
67 func (r Range) Span() (Span, error) {
68 if !r.Start.IsValid() {
69 return Span{}, fmt.Errorf("start pos is not valid")
71 f := r.FileSet.File(r.Start)
73 return Span{}, fmt.Errorf("file not found in FileSet")
75 return FileSpan(f, r.Converter, r.Start, r.End)
78 // FileSpan returns a span within tok, using converter to translate between
79 // offsets and positions.
80 func FileSpan(tok *token.File, converter Converter, start, end token.Pos) (Span, error) {
83 var startFilename string
84 startFilename, s.v.Start.Line, s.v.Start.Column, err = position(tok, start)
88 s.v.URI = URIFromPath(startFilename)
90 var endFilename string
91 endFilename, s.v.End.Line, s.v.End.Column, err = position(tok, end)
95 // In the presence of line directives, a single File can have sections from
96 // multiple file names.
97 if endFilename != startFilename {
98 return Span{}, fmt.Errorf("span begins in file %q but ends in %q", startFilename, endFilename)
104 if converter != nil {
105 return s.WithOffset(converter)
107 if startFilename != tok.Name() {
108 return Span{}, fmt.Errorf("must supply Converter for file %q containing lines from %q", tok.Name(), startFilename)
110 return s.WithOffset(&FileConverter{tok})
113 func position(f *token.File, pos token.Pos) (string, int, int, error) {
114 off, err := offset(f, pos)
118 return positionFromOffset(f, off)
121 func positionFromOffset(f *token.File, offset int) (string, int, int, error) {
122 if offset > f.Size() {
123 return "", 0, 0, fmt.Errorf("offset %v is past the end of the file %v", offset, f.Size())
127 // TODO(golang/go#41029): Consider returning line, column instead of line+1, 1 if
128 // the file's last character is not a newline.
129 if offset == f.Size() {
130 return p.Filename, p.Line + 1, 1, nil
132 return p.Filename, p.Line, p.Column, nil
135 // offset is a copy of the Offset function in go/token, but with the adjustment
136 // that it does not panic on invalid positions.
137 func offset(f *token.File, pos token.Pos) (int, error) {
138 if int(pos) < f.Base() || int(pos) > f.Base()+f.Size() {
139 return 0, fmt.Errorf("invalid pos")
141 return int(pos) - f.Base(), nil
144 // Range converts a Span to a Range that represents the Span for the supplied
146 func (s Span) Range(converter *TokenConverter) (Range, error) {
147 s, err := s.WithOffset(converter)
151 // go/token will panic if the offset is larger than the file's size,
152 // so check here to avoid panicking.
153 if s.Start().Offset() > converter.file.Size() {
154 return Range{}, fmt.Errorf("start offset %v is past the end of the file %v", s.Start(), converter.file.Size())
156 if s.End().Offset() > converter.file.Size() {
157 return Range{}, fmt.Errorf("end offset %v is past the end of the file %v", s.End(), converter.file.Size())
160 FileSet: converter.fset,
161 Start: converter.file.Pos(s.Start().Offset()),
162 End: converter.file.Pos(s.End().Offset()),
163 Converter: converter,
167 func (l *FileConverter) ToPosition(offset int) (int, int, error) {
168 _, line, col, err := positionFromOffset(l.file, offset)
169 return line, col, err
172 func (l *FileConverter) ToOffset(line, col int) (int, error) {
174 return -1, fmt.Errorf("line is not valid")
176 lineMax := l.file.LineCount() + 1
178 return -1, fmt.Errorf("line is beyond end of file %v", lineMax)
179 } else if line == lineMax {
181 return -1, fmt.Errorf("column is beyond end of file")
183 // at the end of the file, allowing for a trailing eol
184 return l.file.Size(), nil
186 pos := lineStart(l.file, line)
188 return -1, fmt.Errorf("line is not in file")
190 // we assume that column is in bytes here, and that the first byte of a
191 // line is at column 1
192 pos += token.Pos(col - 1)
193 return offset(l.file, pos)