Documentation
¶
Overview ¶
Package scope provides the scope graph data model for the canopyls LSP server. A scope graph represents lexical scoping structure: definitions introduced in each scope, references that need resolution, and parent/child nesting.
Index ¶
- Constants
- func GetMeta[T any](d *Definition, key string) (T, bool)
- func ResolveAll(root *Scope)
- func ResolveAllGraph(root *Scope, graph *Graph)
- func ResolvePythonImport(importPath string, projectRoot string) string
- func ResolveTypeScriptImport(importPath string, sourceFile string, projectRoot string) string
- func SetMeta(d *Definition, key string, value any)
- type Definition
- type GoImportResolver
- type Graph
- type Location
- type Ref
- type Rules
- type Scope
- type ScopeKind
Constants ¶
const ( DefFunction = "function" DefMethod = "method" DefVariable = "variable" DefParam = "param" DefType = "type" DefClass = "class" DefImport = "import" DefConstant = "constant" DefField = "field" DefInterface = "interface" )
DefKind constants classify the type of a definition.
Variables ¶
This section is empty.
Functions ¶
func GetMeta ¶
func GetMeta[T any](d *Definition, key string) (T, bool)
GetMeta retrieves a typed value from definition metadata. Returns zero value and false if the key is missing or wrong type.
func ResolveAll ¶
func ResolveAll(root *Scope)
ResolveAll walks the scope tree and resolves all references using only scope-chain lookup (no cross-file type resolution).
func ResolveAllGraph ¶
ResolveAllGraph walks the scope tree and resolves all references, using the graph for cross-file type-directed resolution.
func ResolvePythonImport ¶
ResolvePythonImport resolves a Python import path to a directory on disk. Searches: project root, then common site-packages locations.
func ResolveTypeScriptImport ¶
ResolveTypeScriptImport resolves a TypeScript/JavaScript import path to a file. Handles: relative paths, node_modules, @types.
func SetMeta ¶
func SetMeta(d *Definition, key string, value any)
SetMeta writes a value to definition metadata, initializing the map if needed.
Types ¶
type Definition ¶
type Definition struct {
Name string
Kind string // one of the Def* constants
TypeAnnot string // explicit type annotation if present
ImportPath string // for import definitions
Loc Location
Scope *Scope // child scope this definition creates (e.g. function body)
// Type intelligence
ReturnType string // function/method return type
BaseClasses []string // inheritance chain
Receiver string // method receiver type
TypeParams []string // generics: T, K, V
// Feed metadata (any feed can write, LSP handlers read)
Metadata map[string]any
}
Definition is a named symbol introduced into a scope.
type GoImportResolver ¶
type GoImportResolver struct {
// contains filtered or unexported fields
}
GoImportResolver resolves Go import paths to filesystem directories.
func NewGoImportResolver ¶
func NewGoImportResolver(rootDir, modulePath string) *GoImportResolver
type Graph ¶
Graph holds all scope trees for a project, indexed by file path and package import path.
func BuildFromIndex ¶
BuildFromIndex constructs a scope graph for all files in an index.
func (*Graph) AddFileScope ¶
AddFileScope stores a file-level scope for the given file path.
func (*Graph) AddPackageScope ¶
AddPackageScope stores a package-level scope for the given import path.
func (*Graph) FileScope ¶
FileScope retrieves the scope for the given file path, or nil if not found.
func (*Graph) PackageScope ¶
PackageScope retrieves the scope for the given import path, or nil if not found.
type Ref ¶
type Ref struct {
Name string
Member string // for dotted access: foo.Bar -> Member="Bar"
Loc Location
Resolved *Definition // populated by the resolution pass
}
Ref is a use of a name that needs resolution.
type Rules ¶
type Rules struct {
Language string
Query *gotreesitter.Query
}
Rules holds a compiled scope query for a language.
type Scope ¶
type Scope struct {
Kind ScopeKind
Parent *Scope
Children []*Scope
Defs []Definition
Refs []Ref
}
Scope represents a lexical scope containing definitions, references, and nested child scopes.
func BuildFileScope ¶
func BuildFileScope( tree *gotreesitter.Tree, lang *gotreesitter.Language, src []byte, rules *Rules, path string, ) *Scope
BuildFileScope constructs a scope tree from a parse tree using scope rules.
func NewScope ¶
NewScope creates a new scope of the given kind and attaches it as a child of the parent scope. If parent is nil, the scope is a root.
func (*Scope) AddDef ¶
func (s *Scope) AddDef(def Definition)
AddDef adds a definition to this scope.