treesitter

package
v0.1.156 Latest Latest
Warning

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

Go to latest
Published: Apr 24, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package treesitter implements code analysis using tree-sitter parsers.

ARCHITECTURE: This package is THE backend for code intelligence. LSP was deleted — tree-sitter is the only parser.

CIS Design Document v2.0:

"No LSP as primary layer. LSP is too stateful, too slow, and too single-client
 to serve concurrent sessions. The compiler runs once at commit time for type
 validation. Everything else is tree-sitter and graph."

What tree-sitter gives us:

  • Single-file, error-tolerant parsing (tolerates syntax errors)
  • Incremental parsing (re-parse only changed bytes)
  • In-process (no IPC, no language server lifecycle)
  • Deterministic (same input → same tree)
  • Multi-language (Go, Python, TypeScript, Rust, ...)

What tree-sitter does NOT give us (and we layer on top):

  • Type information → compiler at commit time
  • Cross-file resolution → symbol index built over tree-sitter output
  • Refactoring → agents edit via SymbolPatch

Language support is added by creating a subpackage (e.g. treesitter/golang) that calls Register() in its init() with a *LanguageConfig containing the grammar and a SymbolExtractor. The core package has no per-language code.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Register

func Register(lang languages.Language, cfg *LanguageConfig)

Register adds a language config to the registry. Typically called from a language subpackage's init().

Types

type Client

type Client interface {
	// ListSymbols returns symbols defined in a file (or the whole workspace
	// when file is empty).
	ListSymbols(ctx context.Context, file string) ([]*codev0.Symbol, error)

	// NotifyChange is a no-op for tree-sitter: the parser is stateless and
	// re-parses on demand.
	NotifyChange(ctx context.Context, file string, content string) error

	// NotifySave is a no-op for tree-sitter.
	NotifySave(ctx context.Context, file string) error

	// Diagnostics returns SYNTAX errors (ERROR nodes) for a file or the
	// entire workspace (file == "").
	Diagnostics(ctx context.Context, file string) ([]DiagnosticResult, error)

	// Definition returns the definition location for the identifier at the
	// given 1-based (line, col). Uses CIS 7.1 3-step resolution.
	Definition(ctx context.Context, file string, line, col int) ([]LocationResult, error)

	// References returns all usage locations of the identifier at (line, col).
	References(ctx context.Context, file string, line, col int) ([]LocationResult, error)

	// Hover returns the node text at (line, col).
	Hover(ctx context.Context, file string, line, col int) (*HoverResult, error)

	// Close releases parser resources. Safe to call multiple times.
	Close(ctx context.Context) error
}

Client is the language-agnostic tree-sitter analysis interface.

func NewClient

func NewClient(ctx context.Context, lang languages.Language, sourceDir string) (Client, error)

NewClient creates a tree-sitter client for the given language and source dir. Unlike LSP NewClient, this does NOT spawn a subprocess — tree-sitter runs in-process. Construction is cheap so callers can create per-request if needed.

type DiagnosticResult

type DiagnosticResult struct {
	File      string
	Line      int32
	Column    int32
	EndLine   int32
	EndColumn int32
	Message   string
	Severity  string // always "error" — tree-sitter only emits ERROR nodes
	Source    string // "tree-sitter"
}

DiagnosticResult is a SYNTAX diagnostic from tree-sitter (ERROR nodes). Type/semantic diagnostics come from the compiler at commit time, not here.

type HoverResult

type HoverResult struct {
	Content  string
	Language string
}

HoverResult carries the text snippet under the cursor. Tree-sitter alone cannot produce semantic hover (types, docs). Callers that need rich hover layer the symbol index + summary store above this client.

type LanguageConfig

type LanguageConfig struct {
	// LanguageID matches the LSP languageId convention ("go", "python").
	LanguageID string

	// FileExtensions lists source file suffixes (e.g. [".go"], [".py"]).
	FileExtensions []string

	// SkipDirs are directory names to skip when walking the workspace.
	SkipDirs []string

	// SkipSuffixes are file suffixes to skip (e.g. "_test.go").
	SkipSuffixes []string

	// Grammar returns the tree-sitter language grammar. Called once per client.
	Grammar func() *sitter.Language

	// ExtractSymbols turns a parsed tree into Symbols. Required.
	ExtractSymbols SymbolExtractor
}

LanguageConfig holds per-language settings for the tree-sitter client.

func Lookup

func Lookup(lang languages.Language) *LanguageConfig

Lookup returns the registered config for a language, or nil if missing. Exposed for tests and debugging.

type LocationResult

type LocationResult struct {
	File       string  // relative path within the workspace
	Line       int     // 1-based
	Column     int     // 1-based
	EndLine    int     // 1-based
	EndColumn  int     // 1-based
	Confidence float32 // 0.0–1.0 per CIS confidence model
	Source     string  // "tree_sitter" | "resolved_explicit" | "resolved_wildcard"
}

LocationResult is a source location returned by tree-sitter resolution.

type Parser

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

Parser is a lightweight, concurrency-safe wrapper that caches one *sitter.Parser per goroutine via a sync.Pool. Reuse cuts allocation overhead to near zero when parsing thousands of files in parallel.

func NewParser

func NewParser(lang languages.Language) (*Parser, error)

NewParser returns a Parser for a registered language. Returns an error if the language has not been imported (registered).

func (*Parser) Config

func (p *Parser) Config() *LanguageConfig

Config returns the language configuration the parser was built with. Useful to callers that want to inspect file extensions or skip dirs.

func (*Parser) ParseBytes

func (p *Parser) ParseBytes(ctx context.Context, relPath string, content []byte) ([]*codev0.Symbol, error)

ParseBytes parses the given content and extracts symbols via the language's SymbolExtractor. relPath is used to populate Symbol.Location.File. Thread-safe: each call acquires a pooled *sitter.Parser.

func (*Parser) ParseBytesAll

func (p *Parser) ParseBytesAll(
	ctx context.Context, relPath string, content []byte,
) (syms []*codev0.Symbol, tree *sitter.Tree, err error)

ParseBytesAll returns BOTH the extracted symbols AND the parse tree in a single pass. The caller OWNS the returned tree and must call tree.Close(). Use this when you need to walk the AST after symbol extraction (e.g. building call-graph edges) without re-parsing the file.

func (*Parser) ParseBytesWithDiagnostics

func (p *Parser) ParseBytesWithDiagnostics(
	ctx context.Context, relPath string, content []byte,
) (syms []*codev0.Symbol, diags []DiagnosticResult, err error)

ParseBytesWithDiagnostics is like ParseBytes but also returns syntax-error diagnostics (ERROR / MISSING nodes). Single parse pass, no extra work.

type SymbolExtractor

type SymbolExtractor func(tree *sitter.Tree, content []byte, relPath string) []*codev0.Symbol

SymbolExtractor converts a parsed tree-sitter tree for one file into language-neutral Symbols. Implemented per language in the subpackage.

Directories

Path Synopsis
Package golang registers the Go tree-sitter grammar with the treesitter registry.
Package golang registers the Go tree-sitter grammar with the treesitter registry.
Package python registers the Python tree-sitter grammar with the treesitter registry.
Package python registers the Python tree-sitter grammar with the treesitter registry.

Jump to

Keyboard shortcuts

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