Documentation
¶
Overview ¶
The parsego package defines the File type, a wrapper around a go/ast.File that is useful for answering LSP queries. Notably, it bundles the *token.File and *protocol.Mapper necessary for token.Pos locations to and from UTF-16 LSP positions.
Run `go generate` to update resolver.go from GOROOT.
Index ¶
- Constants
- func Parse(ctx context.Context, fset *token.FileSet, uri protocol.DocumentURI, src []byte, ...) (res *File, fixes []FixType)
- type File
- func (pgf *File) CheckNode(node ast.Node)
- func (pgf *File) CheckPos(pos token.Pos)
- func (pgf *File) Fixed() bool
- func (pgf *File) Indentation(pos token.Pos) (string, error)
- func (pgf *File) NodeLocation(node ast.Node) (protocol.Location, error)
- func (pgf *File) NodeOffsets(node ast.Node) (start int, end int, _ error)
- func (pgf *File) NodeRange(node ast.Node) (protocol.Range, error)
- func (pgf *File) NodeText(node ast.Node) ([]byte, error)
- func (pgf *File) PosLocation(start, end token.Pos) (protocol.Location, error)
- func (pgf *File) PosPosition(pos token.Pos) (protocol.Position, error)
- func (pgf *File) PosRange(start, end token.Pos) (protocol.Range, error)
- func (pgf *File) PosText(start, end token.Pos) ([]byte, error)
- func (pgf *File) PositionPos(p protocol.Position) (token.Pos, error)
- func (pgf *File) RangePos(r protocol.Range) (token.Pos, token.Pos, error)
- func (pgf *File) Resolve()
- func (pgf *File) String() string
- type FixType
Constants ¶
const ( // Header specifies that the main package declaration and imports are needed. // This is the mode used when attempting to examine the package graph structure. Header = parser.AllErrors | parser.ParseComments | parser.ImportsOnly | parser.SkipObjectResolution // Full specifies the full AST is needed. // This is used for files of direct interest where the entire contents must // be considered. Full = parser.AllErrors | parser.ParseComments | parser.SkipObjectResolution )
Common parse modes; these should be reused wherever possible to increase cache hits.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type File ¶
type File struct { URI protocol.DocumentURI Mode parser.Mode // File is the file resulting from parsing. It is always non-nil. // // Clients must not access the AST's legacy ast.Object-related // fields without first ensuring that [File.Resolve] was // already called. File *ast.File Tok *token.File // Source code used to build the AST. It may be different from the // actual content of the file if we have fixed the AST. Src []byte Cursor inspector.Cursor // cursor of *ast.File, sans sibling files Mapper *protocol.Mapper // may map fixed Src, not file content ParseErr scanner.ErrorList // contains filtered or unexported fields }
A File contains the results of parsing a Go file.
func (*File) CheckNode ¶ added in v0.17.0
CheckNode asserts that the Node's positions are valid w.r.t. pgf.Tok.
func (*File) CheckPos ¶ added in v0.17.0
CheckPos asserts that the position is valid w.r.t. pgf.Tok.
func (*File) Fixed ¶
Fixed reports whether p was "Fixed", meaning that its source or positions may not correlate with the original file.
func (*File) Indentation ¶ added in v0.19.0
Indentation returns the string of spaces representing the indentation of the line containing the specified position. This can be used to ensure that inserted code maintains consistent indentation and column alignment.
func (*File) NodeLocation ¶
NodeLocation returns a protocol Location for the ast.Node interval in this file.
func (*File) NodeOffsets ¶ added in v0.17.0
NodeOffsets returns offsets for the ast.Node.
func (*File) NodeText ¶ added in v0.19.0
NodeText returns the source text for the ast.Node interval in this file.
func (*File) PosLocation ¶
PosLocation returns a protocol Location for the token.Pos interval in this file.
func (*File) PosPosition ¶ added in v0.19.0
PosPosition returns a protocol Position for the token.Pos in this file.
func (*File) PosText ¶ added in v0.19.0
PosText returns the source text for the token.Pos interval in this file.
func (*File) PositionPos ¶
PositionPos returns the token.Pos of protocol position p within the file.