treesitter

package
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Jul 28, 2025 License: AGPL-3.0 Imports: 11 Imported by: 0

README

Tree-Sitter Code Parsing

This package provides advanced code parsing and understanding using Tree-sitter.

Components

parser.go
  • Tree-sitter Go bindings integration
  • Multi-language AST parsing support:
    • Go
    • Python
    • JavaScript/TypeScript
    • Rust
    • Java
    • C/C++
  • Lazy loading of language grammars
  • Parser caching for performance
definitions.go
  • Code definition extraction from ASTs:
    • Function definitions
    • Method definitions with receivers (Go-specific)
    • Class/struct definitions
    • Interface definitions
    • Type declarations
  • Code summaries and signatures generation
  • Go-specific constructs (embedding, receivers, etc.)
queries.go
  • Language-specific tree-sitter queries
  • Query result processing and caching
  • Support for:
    • Function/method extraction
    • Type declarations
    • Import statements
    • Comments and documentation
    • Language-specific patterns

Features

  • Multi-Language Support: Parses 6+ programming languages
  • AST-Based Analysis: Deep understanding of code structure
  • Performance Optimized: Lazy loading and caching
  • Go-Aware: Special handling for Go idioms and patterns
  • Rich Queries: Sophisticated pattern matching for code elements

Usage

This package enables:

  • AI assistants to understand code structure and context
  • Intelligent code navigation and analysis
  • Extraction of function signatures and documentation
  • Context-aware code suggestions and edits
  • Integration with file editing for structure-aware modifications

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Definition

type Definition struct {
	Type        DefinitionType         `json:"type"`
	Name        string                 `json:"name"`
	Signature   string                 `json:"signature"`
	Summary     string                 `json:"summary"`
	FilePath    string                 `json:"file_path"`
	StartLine   uint32                 `json:"start_line"`
	StartColumn uint32                 `json:"start_column"`
	EndLine     uint32                 `json:"end_line"`
	EndColumn   uint32                 `json:"end_column"`
	Language    Language               `json:"language"`
	Content     string                 `json:"content"`
	DocString   string                 `json:"doc_string,omitempty"`
	Parameters  []Parameter            `json:"parameters,omitempty"`
	ReturnType  string                 `json:"return_type,omitempty"`
	Receiver    string                 `json:"receiver,omitempty"` // For Go methods
	Package     string                 `json:"package,omitempty"`
	Modifiers   []string               `json:"modifiers,omitempty"` // public, private, static, etc.
	Metadata    map[string]interface{} `json:"metadata,omitempty"`
}

Definition represents a code definition extracted from AST

type DefinitionExtractor

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

DefinitionExtractor extracts code definitions from AST

func NewDefinitionExtractor

func NewDefinitionExtractor(parser *TreeSitterParser) *DefinitionExtractor

NewDefinitionExtractor creates a new definition extractor

func (*DefinitionExtractor) ExtractDefinitions

func (de *DefinitionExtractor) ExtractDefinitions(result *ParseResult) ([]Definition, error)

ExtractDefinitions extracts all definitions from a parsed AST

func (*DefinitionExtractor) ExtractDefinitionsByType

func (de *DefinitionExtractor) ExtractDefinitionsByType(result *ParseResult, defType DefinitionType) ([]Definition, error)

ExtractDefinitionsByType extracts definitions of a specific type

func (*DefinitionExtractor) FindDefinition

func (de *DefinitionExtractor) FindDefinition(result *ParseResult, name string) (*Definition, error)

FindDefinition finds a specific definition by name

type DefinitionType

type DefinitionType string

DefinitionType represents different types of code definitions

const (
	DefFunction  DefinitionType = "function"
	DefMethod    DefinitionType = "method"
	DefClass     DefinitionType = "class"
	DefStruct    DefinitionType = "struct"
	DefInterface DefinitionType = "interface"
	DefType      DefinitionType = "type"
	DefVariable  DefinitionType = "variable"
	DefConstant  DefinitionType = "constant"
	DefImport    DefinitionType = "import"
	DefPackage   DefinitionType = "package"
)

type Language

type Language string

Language represents a supported programming language

const (
	LanguageGo         Language = "go"
	LanguagePython     Language = "python"
	LanguageJavaScript Language = "javascript"
	LanguageRust       Language = "rust"
	LanguageC          Language = "c"
	LanguageCPP        Language = "cpp"
	LanguageJava       Language = "java"
	LanguageUnknown    Language = "unknown"
)

type Parameter

type Parameter struct {
	Name    string `json:"name"`
	Type    string `json:"type"`
	Default string `json:"default,omitempty"`
}

Parameter represents a function or method parameter

type ParseResult

type ParseResult struct {
	Language  Language               `json:"language"`
	FilePath  string                 `json:"file_path"`
	Content   string                 `json:"content"`
	Tree      *sitter.Tree           `json:"-"`
	RootNode  *sitter.Node           `json:"-"`
	ParseTime time.Duration          `json:"parse_time"`
	NodeCount uint32                 `json:"node_count"`
	Error     string                 `json:"error,omitempty"`
	Metadata  map[string]interface{} `json:"metadata,omitempty"`
}

ParseResult contains the result of parsing a file

type ParserConfig

type ParserConfig struct {
	CacheSize     int           `json:"cache_size"`
	ParseTimeout  time.Duration `json:"parse_timeout"`
	MaxFileSize   int64         `json:"max_file_size"`
	EnableLogging bool          `json:"enable_logging"`
	LazyLoad      bool          `json:"lazy_load"`
}

ParserConfig configures the tree-sitter parser behavior

type QueryBuilder

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

QueryBuilder helps build complex queries programmatically

func NewQueryBuilder

func NewQueryBuilder(language Language) *QueryBuilder

NewQueryBuilder creates a new query builder

func (*QueryBuilder) AddCapture

func (qb *QueryBuilder) AddCapture(capture string) *QueryBuilder

AddCapture adds a capture to the query

func (*QueryBuilder) AddPattern

func (qb *QueryBuilder) AddPattern(pattern string) *QueryBuilder

AddPattern adds a pattern to the query

func (*QueryBuilder) Build

func (qb *QueryBuilder) Build() string

Build builds the final query string

type QueryMatch

type QueryMatch struct {
	Node        *sitter.Node           `json:"-"`
	Text        string                 `json:"text"`
	StartLine   uint32                 `json:"start_line"`
	StartColumn uint32                 `json:"start_column"`
	EndLine     uint32                 `json:"end_line"`
	EndColumn   uint32                 `json:"end_column"`
	Captures    map[string]string      `json:"captures,omitempty"`
	Context     string                 `json:"context,omitempty"`
	Metadata    map[string]interface{} `json:"metadata,omitempty"`
}

QueryMatch represents a single match from a query

type QueryPattern

type QueryPattern struct {
	Language Language  `json:"language"`
	Type     QueryType `json:"type"`
	Pattern  string    `json:"pattern"`
	Captures []string  `json:"captures"`
}

QueryPattern represents a tree-sitter query pattern for a specific language

type QueryResult

type QueryResult struct {
	Type        QueryType              `json:"type"`
	Language    Language               `json:"language"`
	Matches     []QueryMatch           `json:"matches"`
	ExecuteTime time.Duration          `json:"execute_time"`
	NodeCount   int                    `json:"node_count"`
	Error       string                 `json:"error,omitempty"`
	Metadata    map[string]interface{} `json:"metadata,omitempty"`
}

QueryResult represents the result of a tree-sitter query

type QuerySystem

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

QuerySystem manages tree-sitter queries for multiple languages

func NewQuerySystem

func NewQuerySystem(parser *TreeSitterParser) *QuerySystem

NewQuerySystem creates a new query system

func (*QuerySystem) AddCustomPattern

func (qs *QuerySystem) AddCustomPattern(language Language, queryType QueryType, pattern string, captures []string) error

AddCustomPattern adds a custom query pattern

func (*QuerySystem) ClearCache

func (qs *QuerySystem) ClearCache()

ClearCache clears the query result cache

func (*QuerySystem) ExecuteCustomQuery

func (qs *QuerySystem) ExecuteCustomQuery(result *ParseResult, queryPattern string) (*QueryResult, error)

ExecuteCustomQuery executes a custom query pattern

func (*QuerySystem) ExecuteQuery

func (qs *QuerySystem) ExecuteQuery(result *ParseResult, queryType QueryType) (*QueryResult, error)

ExecuteQuery executes a specific query type on a parse result

func (*QuerySystem) FindNodesByName

func (qs *QuerySystem) FindNodesByName(result *ParseResult, name string) ([]QueryMatch, error)

FindNodesByName finds all nodes with a specific name

func (*QuerySystem) FindNodesByType

func (qs *QuerySystem) FindNodesByType(result *ParseResult, nodeType string) ([]QueryMatch, error)

FindNodesByType finds all nodes of a specific type

func (*QuerySystem) GetAllDefinitions

func (qs *QuerySystem) GetAllDefinitions(result *ParseResult) (map[QueryType][]QueryMatch, error)

GetAllDefinitions returns all definitions using the appropriate query types

func (*QuerySystem) GetAvailableQueries

func (qs *QuerySystem) GetAvailableQueries(language Language) []QueryType

GetAvailableQueries returns available query types for a language

func (*QuerySystem) GetCacheStats

func (qs *QuerySystem) GetCacheStats() map[string]interface{}

GetCacheStats returns cache statistics

func (*QuerySystem) GetPattern

func (qs *QuerySystem) GetPattern(language Language, queryType QueryType) (*QueryPattern, error)

GetPattern returns a query pattern for a language and query type

type QueryType

type QueryType string

QueryType represents different types of tree-sitter queries

const (
	QueryFunctions  QueryType = "functions"
	QueryMethods    QueryType = "methods"
	QueryClasses    QueryType = "classes"
	QueryStructs    QueryType = "structs"
	QueryInterfaces QueryType = "interfaces"
	QueryTypes      QueryType = "types"
	QueryVariables  QueryType = "variables"
	QueryConstants  QueryType = "constants"
	QueryImports    QueryType = "imports"
	QueryComments   QueryType = "comments"
	QueryDocStrings QueryType = "docstrings"
	QueryErrors     QueryType = "errors"
	QueryAll        QueryType = "all"
)

type TreeSitterParser

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

TreeSitterParser provides multi-language AST parsing capabilities

func NewTreeSitterParser

func NewTreeSitterParser(config ParserConfig) (*TreeSitterParser, error)

NewTreeSitterParser creates a new tree-sitter parser instance

func (*TreeSitterParser) ClearCache

func (tsp *TreeSitterParser) ClearCache()

ClearCache clears the parser cache

func (*TreeSitterParser) DetectLanguage

func (tsp *TreeSitterParser) DetectLanguage(filePath string) Language

DetectLanguage detects the programming language from file extension

func (*TreeSitterParser) FindNodesOfType

func (tsp *TreeSitterParser) FindNodesOfType(root *sitter.Node, nodeType string) []*sitter.Node

FindNodesOfType finds all nodes of a specific type

func (*TreeSitterParser) GetCacheStats

func (tsp *TreeSitterParser) GetCacheStats() map[string]interface{}

GetCacheStats returns cache statistics

func (*TreeSitterParser) GetChildByType

func (tsp *TreeSitterParser) GetChildByType(node *sitter.Node, nodeType string) *sitter.Node

GetChildByType finds the first child node of a specific type

func (*TreeSitterParser) GetChildrenByType

func (tsp *TreeSitterParser) GetChildrenByType(node *sitter.Node, nodeType string) []*sitter.Node

GetChildrenByType finds all child nodes of a specific type

func (*TreeSitterParser) GetNodeLocation

func (tsp *TreeSitterParser) GetNodeLocation(node *sitter.Node) (startLine, startCol, endLine, endCol uint32)

GetNodeLocation returns the line and column information for a node

func (*TreeSitterParser) GetNodeText

func (tsp *TreeSitterParser) GetNodeText(node *sitter.Node, content string) string

GetNodeText returns the text content of a node

func (*TreeSitterParser) GetSupportedLanguages

func (tsp *TreeSitterParser) GetSupportedLanguages() []Language

GetSupportedLanguages returns a list of supported languages

func (*TreeSitterParser) IsLanguageSupported

func (tsp *TreeSitterParser) IsLanguageSupported(language Language) bool

IsLanguageSupported checks if a language is supported

func (*TreeSitterParser) NodeToString

func (tsp *TreeSitterParser) NodeToString(node *sitter.Node, content string, maxDepth int) string

NodeToString provides a debug representation of a node

func (*TreeSitterParser) ParseContent

func (tsp *TreeSitterParser) ParseContent(content string, language Language, filePath string) (*ParseResult, error)

ParseContent parses the given content with the specified language

func (*TreeSitterParser) ParseFile

func (tsp *TreeSitterParser) ParseFile(filePath string) (*ParseResult, error)

ParseFile parses a file and returns the AST

func (*TreeSitterParser) TraverseTree

func (tsp *TreeSitterParser) TraverseTree(root *sitter.Node, visitor func(*sitter.Node, int) bool)

TraverseTree traverses the AST and calls the visitor function for each node

Jump to

Keyboard shortcuts

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