query

package
v0.0.0-...-8f660a8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 3, 2023 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

View Source
const (
	NodeTypeModule              = "module"
	NodeTypeCall                = "call"
	NodeTypeArgList             = "argument_list"
	NodeTypeFunctionDef         = "function_definition"
	NodeTypeParameters          = "parameters"
	NodeTypeKeywordArgument     = "keyword_argument"
	NodeTypeIdentifier          = "identifier"
	NodeTypeIfStatement         = "if_statement"
	NodeTypeExpressionStatement = "expression_statement"
	NodeTypeForStatement        = "for_statement"
	NodeTypeAssignment          = "assignment"
	NodeTypeAttribute           = "attribute"
	NodeTypeString              = "string"
	NodeTypeDictionary          = "dictionary"
	NodeTypeList                = "list"
	NodeTypeComment             = "comment"
	NodeTypeBlock               = "block"
	NodeTypeERROR               = "ERROR"

	FieldName       = "name"
	FieldParameters = "parameters"
	FieldReturnType = "return_type"
	FieldBody       = "body"
)
View Source
const FunctionParameters = `` /* 151-byte string literal not displayed */

FunctionParameters extracts parameters from a function definition and supports a mixture of positional parameters, default value parameters, typed parameters*, and typed default value parameters*.

  • These are not valid Starlark, but we support them to enable using Python type-stub files for improved editor experience.
View Source
const Identifiers = `
[(module) @module
 (identifier) @id
 "." @dot
 (ERROR "." @trailing-dot
	.)
 ]
`

Extract all identifiers from the subtree. Include an extra empty identifier "" if there is an error node with a trailing period.

Variables

View Source
var LanguagePython = python.GetLanguage()

Functions

func ChildNodeAtPoint

func ChildNodeAtPoint(pt sitter.Point, node *sitter.Node) (*sitter.Node, bool)

func ExtractIdentifiers

func ExtractIdentifiers(doc DocumentContent, nodes []*sitter.Node, limit *sitter.Point) []string

func FindChildNode

func FindChildNode(node *sitter.Node, compare func(*sitter.Node) int) *sitter.Node

Find the deepest child node for which `compare` returns 0. Compare should return: - `-1` if the node is located before the range of interest - `0` if the node covers the range of interest - `1` if the node is located after the range of interest

func Functions

func Functions(doc DocumentContent, node *sitter.Node) map[string]Signature

Functions finds all function definitions that are direct children of the provided sitter.Node.

func HasAncestor

func HasAncestor(node *sitter.Node, compfn func(*sitter.Node) bool) bool

func IsModuleScope

func IsModuleScope(doc DocumentContent, node *sitter.Node) bool

A node is in the scope of the top level module if there are no function definitions in the ancestry of the node.

func LeafNodes

func LeafNodes(node *sitter.Node) []*sitter.Node

func LoadStatements

func LoadStatements(input []byte, tree *sitter.Tree) []*sitter.Node

func MustQuery

func MustQuery(pattern []byte, lang *sitter.Language) *sitter.Query

func NamedNodeAtPoint

func NamedNodeAtPoint(doc DocumentContent, pt sitter.Point) (*sitter.Node, bool)

func NamedNodeAtPosition

func NamedNodeAtPosition(doc DocumentContent, pos protocol.Position) (*sitter.Node, bool)

NamedNodeAtPosition returns the most granular named descendant at a position.

func NodeAtPoint

func NodeAtPoint(doc DocumentContent, pt sitter.Point) (*sitter.Node, bool)

func NodeAtPosition

func NodeAtPosition(doc DocumentContent, pos protocol.Position) (*sitter.Node, bool)

NodeAtPosition returns the node (named or unnamed) with the smallest start/end range that covers the given position.

func NodeBefore

func NodeBefore(a, b *sitter.Node) bool

func NodeLocation

func NodeLocation(node *sitter.Node, docURI uri.URI) protocol.Location

func NodeRange

func NodeRange(node *sitter.Node) protocol.Range

func NodesRange

func NodesRange(nodes []*sitter.Node) protocol.Range

func Parse

func Parse(ctx context.Context, input []byte) (*sitter.Tree, error)

func PointAfter

func PointAfter(a, b sitter.Point) bool

func PointAfterOrEqual

func PointAfterOrEqual(a, b sitter.Point) bool

func PointBefore

func PointBefore(a, b sitter.Point) bool

func PointBeforeOrEqual

func PointBeforeOrEqual(a, b sitter.Point) bool

func PointCmp

func PointCmp(a, b sitter.Point) int

func PointCovered

func PointCovered(a sitter.Point, b *sitter.Node) bool

func PointInside

func PointInside(a sitter.Point, b sitter.Range) bool

func PositionToPoint

func PositionToPoint(pos protocol.Position) sitter.Point

PositionToPoint converts an LSP protocol file location to a Tree-sitter file location.

func Query

func Query(node *sitter.Node, pattern string, matchFn MatchFunc)

Query executes a Tree-sitter S-expression query against a subtree and invokes matchFn on each result.

func RangeContainsPoint

func RangeContainsPoint(r sitter.Range, p sitter.Point) bool

func SitterRange

func SitterRange(r protocol.Range) sitter.Range

func SymbolsBefore

func SymbolsBefore(symbols []protocol.DocumentSymbol, before *sitter.Node) []protocol.DocumentSymbol

Returns only the symbols that occur before the node given if any, otherwise return all symbols.

func Unquote

func Unquote(input []byte, n *sitter.Node) string

Unquote a Tree sitter string node into its string contents.

Also accepts a parent module, block or expression statement containing a string node for convenience. If a non-string node is passed, Unquote will panic().

Tree sitter parses a string literal into 2 or more child nodes representing the beginning/ending delimiters, and any number of escape sequences inside. Thus, the string

"""hello\nTilted\nWorld"""

gets parsed into a tree like:

module [0, 0] - [1, 0]
  expression_statement [0, 0] - [0, 26]
    string [0, 0] - [0, 26]
      " [0, 0] - [0, 3]
      escape_sequence [0, 8] - [0, 10]
      escape_sequence [0, 16] - [0, 18]
      " [0, 23] - [0, 26]

Notably, there are no nodes to represent the contents in between the string delimiters and any escape sequences, so we have to extract those manually based on the start/end byte boundaries of the adjacent delimiter or escape sequence nodes.

Types

type DocumentContent

type DocumentContent interface {
	Input() []byte
	Content(n *sitter.Node) string
	ContentRange(r sitter.Range) string
	Tree() *sitter.Tree
	URI() uri.URI
}

type MatchFunc

type MatchFunc func(q *sitter.Query, match *sitter.QueryMatch) bool

type Parameter

type Parameter struct {
	Name         string
	TypeHint     string
	DefaultValue string
	Content      string
	DocURI       uri.URI
	Location     protocol.Location
}

func (Parameter) ParameterInfo

func (p Parameter) ParameterInfo(fnDocs docstring.Parsed) protocol.ParameterInformation

func (Parameter) Symbol

func (p Parameter) Symbol() Symbol

type Signature

type Signature struct {
	Name       string
	Params     []Parameter
	ReturnType string
	Docs       docstring.Parsed

	Range protocol.Range
	// contains filtered or unexported fields
}

func ExtractSignature

func ExtractSignature(doc DocumentContent, n *sitter.Node) Signature

func Function

func Function(doc DocumentContent, node *sitter.Node, fnName string) (Signature, bool)

Function finds a function definition for the given function name that is a direct child of the provided sitter.Node.

func (Signature) Label

func (s Signature) Label() string

Label produces a human-readable Label for a function signature.

It's modeled to behave similarly to VSCode Python signature labels.

func (Signature) SignatureInfo

func (s Signature) SignatureInfo() protocol.SignatureInformation

func (Signature) Symbol

func (s Signature) Symbol() Symbol

type Symbol

type Symbol struct {
	Name           string
	Detail         string
	Kind           protocol.SymbolKind
	Tags           []protocol.SymbolTag
	Location       protocol.Location
	SelectionRange protocol.Range
	Children       []Symbol
}

func DocumentSymbols

func DocumentSymbols(doc DocumentContent) []Symbol

DocumentSymbols returns all symbols with document-wide visibility.

func ExtractVariableAssignment

func ExtractVariableAssignment(doc DocumentContent, n *sitter.Node) Symbol

func SiblingSymbols

func SiblingSymbols(doc DocumentContent, node, before *sitter.Node) []Symbol

Get all symbols defined at the same level as the given node. If before != nil, only include symbols that appear before that node.

func SymbolsInScope

func SymbolsInScope(doc DocumentContent, node *sitter.Node) []Symbol

Get all symbols defined in scopes at or above the level of the given node, excluding symbols from the top-level module (document symbols).

func (Symbol) HasLocation

func (s Symbol) HasLocation() bool

builtins (e.g., `False`, `k8s_resource`) have no location

type Type

type Type struct {
	Name    string
	Methods []Signature
	Fields  []Symbol
	Members []Symbol
}

func Types

func Types(doc DocumentContent, node *sitter.Node) []Type

func (Type) FindMethod

func (t Type) FindMethod(name string) (Signature, bool)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL