Skip to content

v.astquery #

Module astquery answers questions about a parsed V file that an editor, a language server or an agent has to answer constantly: what is declared here, where is a name mentioned, and what sits under the cursor.

It reads the flat AST that v.parser produces and never type checks, so every answer is available even for a file that does not compile. That is the point: a tool has to be able to look at broken code.

Declaration results omit visibility modifiers. Struct fields retain their names and types regardless of access modifiers, which are AST metadata rather than separate declarations.

fn declarations #

fn declarations(a &flat.FlatAst) []Declaration

declarations returns every named declaration in the user code of a.

Order is the order the parser produced, which is source order for a single file. A declaration nested in a $if block is reported where it appears. Struct field names and types are reported regardless of access modifiers, including fields named pub or priv.

fn occurrence_at #

fn occurrence_at(a &flat.FlatAst, name string, line int, column int) ?Occurrence

occurrence_at returns the innermost mention of name that covers line:column, which is what a cursor position asks for.

fn parse #

fn parse(path string) &flat.FlatAst

parse reads one V file into a fresh AST with the preferences a formatter and an inspector agree on.

fn references #

fn references(a &flat.FlatAst, name string) []Occurrence

references returns every mention of name in the user code of a.

The name is matched against identifier positions in the AST, not against raw text, so a comment or a string literal that happens to contain it is not reported.

fn DeclKind.from #

fn DeclKind.from[W](input W) !DeclKind

from returns the DeclKind value of an integer or of a field name, or an error if there is none.

enum DeclKind #

enum DeclKind {
	module
	import
	fn
	method
	struct
	field
	enum
	enum_value
	interface
	interface_method
	type_alias
	sumtype
	const
	global
}

DeclKind classifies a declaration found by declarations.

fn (DeclKind) str #

fn (k DeclKind) str() string

str returns the lowercase name of the kind, as used in JSON output.

struct Declaration #

struct Declaration {
pub:
	kind DeclKind
	// name is the declared name. A method is `receiver` plus `name`.
	name string
	// receiver is the type a method belongs to, empty otherwise.
	receiver string
	// type_name is the field type, the function return type, or empty.
	type_name string
	file      string
	// line and column are 1-based, as an editor reports them.
	line   int
	column int
	// end_line and end_column bound the declaration.
	end_line   int
	end_column int
	// doc is the doc comment directly above the declaration, without its `//`
	// markers, when there is one.
	doc string
}

Declaration is one named declaration, with where it starts and what it is.

struct Occurrence #

struct Occurrence {
pub:
	name string
	// node_kind is the AST kind that carries the name, for example `ident` or
	// `selector`.
	node_kind string
	file      string
	line      int
	// column and end_column bound the name itself, one past the end.
	column     int
	end_column int
	// declaration is true when the mention is the declaration itself.
	declaration bool
	// type_name is what the parser resolved the mention to, when known.
	type_name string
}

Occurrence is one mention of a name.