...

Package span

import "golang.org/x/tools/internal/span"
Overview
Index

Overview ▾

Package span contains support for representing with positions and ranges in text files.

Index ▾

Variables
func Compare(a, b Span) int
func ComparePoint(a, b Point) int
func CompareURI(a, b URI) int
func ToUTF16Column(p Point, content []byte) (int, error)
type Converter
type Point
    func FromUTF16Column(p Point, chr int, content []byte) (Point, error)
    func NewPoint(line, col, offset int) Point
    func (p Point) Column() int
    func (p Point) HasOffset() bool
    func (p Point) HasPosition() bool
    func (p Point) IsValid() bool
    func (p Point) Line() int
    func (p *Point) MarshalJSON() ([]byte, error)
    func (p Point) Offset() int
    func (p *Point) UnmarshalJSON(b []byte) error
type Range
    func NewRange(fset *token.FileSet, start, end token.Pos) Range
    func (r Range) IsPoint() bool
    func (r Range) Span() (Span, error)
type Span
    func New(uri URI, start Point, end Point) Span
    func Parse(input string) 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(converter *TokenConverter) (Range, error)
    func (s Span) Start() Point
    func (s Span) URI() URI
    func (s *Span) UnmarshalJSON(b []byte) error
    func (s Span) WithAll(c Converter) (Span, error)
    func (s Span) WithOffset(c Converter) (Span, error)
    func (s Span) WithPosition(c Converter) (Span, error)
type TokenConverter
    func NewContentConverter(filename string, content []byte) *TokenConverter
    func NewTokenConverter(fset *token.FileSet, f *token.File) *TokenConverter
    func (l *TokenConverter) ToOffset(line, col int) (int, error)
    func (l *TokenConverter) ToPosition(offset int) (int, int, error)
type URI
    func FileURI(path string) URI
    func NewURI(s string) URI
    func (uri URI) Filename() string

Package files

parse.go span.go token.go token111.go uri.go utf16.go

Variables

Invalid is a span that reports false from IsValid

var Invalid = Span{/* contains filtered or unexported fields */}

func Compare

func Compare(a, b Span) int

func ComparePoint

func ComparePoint(a, b Point) int

func CompareURI

func CompareURI(a, b URI) int

func ToUTF16Column

func ToUTF16Column(p Point, content []byte) (int, error)

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.

type Converter

Converter is the interface to an object that can convert between line:column and offset forms for a single file.

type Converter interface {
    //ToPosition converts from an offset to a line:column pair.
    ToPosition(offset int) (int, int, error)
    //ToOffset converts from a line:column pair to an offset.
    ToOffset(line, col int) (int, error)
}

type Point

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.

type Point struct {
    // contains filtered or unexported fields
}

func FromUTF16Column

func FromUTF16Column(p Point, chr int, content []byte) (Point, error)

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.

func NewPoint

func NewPoint(line, col, offset int) Point

func (Point) Column

func (p Point) Column() int

func (Point) HasOffset

func (p Point) HasOffset() bool

func (Point) HasPosition

func (p Point) HasPosition() bool

func (Point) IsValid

func (p Point) IsValid() bool

func (Point) Line

func (p Point) Line() int

func (*Point) MarshalJSON

func (p *Point) MarshalJSON() ([]byte, error)

func (Point) Offset

func (p Point) Offset() int

func (*Point) UnmarshalJSON

func (p *Point) UnmarshalJSON(b []byte) error

type Range

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
    Converter Converter
}

func NewRange

func NewRange(fset *token.FileSet, start, end token.Pos) Range

NewRange creates a new Range from a FileSet and two positions. To represent a point pass a 0 as the end pos.

func (Range) IsPoint

func (r Range) IsPoint() bool

IsPoint returns true if the range represents a single point.

func (Range) Span

func (r Range) Span() (Span, error)

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.

type Span

Span represents a source code range in standardized form.

type Span struct {
    // contains filtered or unexported fields
}

func New

func New(uri URI, start Point, end Point) Span

func Parse

func Parse(input string) Span

Parse returns the location represented by the input. All inputs are valid locations, as they can always be a pure filename. The returned span will be normalized, and thus if printed may produce a different string.

func (Span) End

func (s Span) End() Point

func (Span) Format

func (s Span) Format(f fmt.State, c rune)

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) HasOffset

func (s Span) HasOffset() bool

func (Span) HasPosition

func (s Span) HasPosition() bool

func (Span) IsPoint

func (s Span) IsPoint() bool

func (Span) IsValid

func (s Span) IsValid() bool

func (*Span) MarshalJSON

func (s *Span) MarshalJSON() ([]byte, error)

func (Span) Range

func (s Span) Range(converter *TokenConverter) (Range, error)

Range converts a Span to a Range that represents the Span for the supplied File.

func (Span) Start

func (s Span) Start() Point

func (Span) URI

func (s Span) URI() URI

func (*Span) UnmarshalJSON

func (s *Span) UnmarshalJSON(b []byte) error

func (Span) WithAll

func (s Span) WithAll(c Converter) (Span, error)

func (Span) WithOffset

func (s Span) WithOffset(c Converter) (Span, error)

func (Span) WithPosition

func (s Span) WithPosition(c Converter) (Span, error)

type TokenConverter

TokenConverter is a Converter backed by a token file set and file. It uses the file set methods to work out the conversions, which makes it fast and does not require the file contents.

type TokenConverter struct {
    // contains filtered or unexported fields
}

func NewContentConverter

func NewContentConverter(filename string, content []byte) *TokenConverter

NewContentConverter returns an implementation of Converter for the given file content.

func NewTokenConverter

func NewTokenConverter(fset *token.FileSet, f *token.File) *TokenConverter

NewTokenConverter returns an implementation of Converter backed by a token.File.

func (*TokenConverter) ToOffset

func (l *TokenConverter) ToOffset(line, col int) (int, error)

func (*TokenConverter) ToPosition

func (l *TokenConverter) ToPosition(offset int) (int, int, error)

type URI

URI represents the full URI for a file.

type URI string

func FileURI

func FileURI(path string) URI

FileURI returns a span URI for the supplied file path. It will always have the file scheme.

func NewURI

func NewURI(s string) URI

NewURI returns a span URI for the string. It will attempt to detect if the string is a file path or uri.

func (URI) Filename

func (uri URI) Filename() string

Filename returns the file path for the given URI. It is an error to call this on a URI that is not a valid filename.