Documentation ¶
Overview ¶
Package span contains support for representing with positions and ranges in text files.
Index ¶
- Variables
- func ComparePoint(a, b Point) int
- func NewTokenFile(filename string, content []byte) *token.File
- func SameExistingFile(a, b URI) bool
- func SortSpans(spans []Span)
- func ToOffset(tf *token.File, line, col int) (int, error)
- func ToPosition(tf *token.File, offset int) (int, int, error)
- func ToUTF16Column(p Point, content []byte) (int, error)
- type Point
- type Range
- type Span
- func (s Span) End() Point
- func (s Span) Format(f fmt.State, c rune)
- func (s Span) HasOffset() bool
- func (s Span) HasPosition() bool
- func (s Span) IsPoint() bool
- func (s Span) IsValid() bool
- func (s *Span) MarshalJSON() ([]byte, error)
- func (s Span) Range(tf *token.File) (Range, error)
- func (span *Span) SetRange(file *token.File, start, end token.Pos)
- func (s Span) Start() Point
- func (s Span) URI() URI
- func (s *Span) UnmarshalJSON(b []byte) error
- func (s Span) WithAll(tf *token.File) (Span, error)
- func (s Span) WithOffset(tf *token.File) (Span, error)
- type URI
Constants ¶
This section is empty.
Variables ¶
var Invalid = Span{/* contains filtered or unexported fields */}
Invalid is a span that reports false from IsValid
Functions ¶
func ComparePoint ¶
func NewTokenFile ¶
NewTokenFile returns a token.File for the given file content.
func SameExistingFile ¶
SameExistingFile reports whether two spans denote the same existing file by querying the file system.
func SortSpans ¶
func SortSpans(spans []Span)
SortSpans sorts spans into a stable but unspecified order.
func ToOffset ¶
ToOffset converts a 1-based line and utf-8 column index into a byte offset in the file corresponding to tf.
func ToPosition ¶
ToPosition converts a byte offset in the file corresponding to tf into 1-based line and utf-8 column indexes.
func ToUTF16Column ¶
ToUTF16Column calculates the utf16 column expressed by the point given the supplied file contents. This is used to convert from the native (always in bytes) column representation and the utf16 counts used by some editors.
TODO(adonovan): this function is unused except by its test. Delete, or consolidate with (*protocol.ColumnMapper).utf16Column.
Types ¶
type Point ¶
type Point struct {
// contains filtered or unexported fields
}
Point represents a single point within a file. In general this should only be used as part of a Span, as on its own it does not carry enough information.
func FromUTF16Column ¶
FromUTF16Column advances the point by the utf16 character offset given the supplied line contents. This is used to convert from the utf16 counts used by some editors to the native (always in bytes) column representation.
The resulting Point always has an offset.
TODO: it looks like this may incorrectly confer a "position" to the resulting Point, when it shouldn't. If p.HasPosition() == false, the resulting Point will return p.HasPosition() == true, but have the wrong position.
func (Point) HasPosition ¶
func (*Point) MarshalJSON ¶
func (*Point) UnmarshalJSON ¶
type Range ¶
Range represents a source code range in token.Pos form. It also carries the token.File that produced the positions, so that it is self contained.
func NewRange ¶
NewRange creates a new Range from a token.File and two positions within it. The given start position must be valid; if end is invalid, start is used as the end position.
(If you only have a token.FileSet, use file = fset.File(start). But most callers know exactly which token.File they're dealing with and should pass it explicitly. Not only does this save a lookup, but it brings us a step closer to eliminating the global FileSet.)
type Span ¶
type Span struct {
// contains filtered or unexported fields
}
Span represents a source code range in standardized form.
func FileSpan ¶
FileSpan returns a span within the file referenced by start and end, using a token.File to translate between offsets and positions.
The start and end position must be contained within posFile, though due to line directives they may reference positions in another file. If srcFile is provided, it is used to map the line:column positions referenced by start and end to offsets in the corresponding file.
TODO(adonovan): clarify whether it is valid to pass posFile==srcFile when //line directives are in use. If so, fix this function; if not, fix Range.Span.
func Parse ¶
Parse returns the location represented by the input. Only file paths are accepted, not URIs. The returned span will be normalized, and thus if printed may produce a different string.
func ParseInDir ¶
ParseInDir is like Parse, but interprets paths relative to wd.
func (Span) Format ¶
Format implements fmt.Formatter to print the Location in a standard form. The format produced is one that can be read back in using Parse.
func (Span) HasPosition ¶
func (*Span) MarshalJSON ¶
func (Span) Range ¶
Range converts a Span to a Range that represents the Span for the supplied File.
func (*Span) SetRange ¶
SetRange implements packagestest.rangeSetter, allowing gopls' test suites to use Spans instead of Range in parameters.
func (*Span) UnmarshalJSON ¶
type URI ¶
type URI string
URI represents the full URI for a file.
func URIFromPath ¶
URIFromPath returns a span URI for the supplied file path.
For empty paths, URIFromPath returns the empty URI "". For non-empty paths, URIFromPath returns a uri with the file:// scheme.