Documentation
¶
Overview ¶
Package codemap builds a compact, read-only structural index of a workspace. The first slice is outline-first: directory, file, and symbol nodes. Dependency and call edges can layer onto the same stable node IDs later.
Index ¶
- Constants
- func FormatCycles(cycles [][]Node, max int) string
- func FormatDependencies(title string, nodes []Node, max int) string
- func FormatImpact(result ImpactResult, max int) string
- func FormatImpactSummary(result ImpactResult, max int) string
- func FormatNodes(nodes []Node, max int) string
- func FormatSubsystemOverview(o SubsystemOverview, max int) string
- func FormatTree(idx *CodeIndex, max int) string
- func IsTestFor(candidateRel, targetRel string) bool
- func MermaidDiagram(idx *CodeIndex, focus string, maxEdges int) string
- func Projection(idx *CodeIndex, max int) string
- type BuildOptions
- type BuilderProvider
- type CachedProvider
- type CodeIndex
- func (i *CodeIndex) Children(id NodeID) []NodeID
- func (i *CodeIndex) Count() int
- func (i *CodeIndex) Cycles(pathOrQuery string, max int) [][]Node
- func (i *CodeIndex) Dependencies(pathOrQuery string, max int) []Node
- func (i *CodeIndex) Dependents(pathOrQuery string, max int) []Node
- func (i *CodeIndex) Edges(kind EdgeKind) []Edge
- func (i *CodeIndex) Filter(query string, max int) []Node
- func (i *CodeIndex) Impact(pathOrQuery string, depth, max int) ImpactResult
- func (i *CodeIndex) Node(id NodeID) (Node, bool)
- func (i *CodeIndex) Nodes() []Node
- func (i *CodeIndex) Root() NodeID
- func (i *CodeIndex) RootPath() string
- func (i *CodeIndex) SuggestedContext(paths []string, max int) []ContextSuggestion
- func (i *CodeIndex) SymbolsForFile(path string) []Node
- type ContextSuggestion
- type Edge
- type EdgeKind
- type FallbackSource
- type ImpactResult
- type LSPSource
- type Node
- type NodeID
- type NodeKind
- type Position
- type Provider
- type Range
- type Reason
- type StaticProvider
- type Stats
- type SubsystemOverview
- type SuggestedItem
- type SymbolInfo
- type SymbolSource
Constants ¶
const MaxDepthAll = -1
MaxDepthAll means dependency traversal should continue until exhausted.
Variables ¶
This section is empty.
Functions ¶
func FormatCycles ¶
FormatCycles renders import cycles for humans and agents.
func FormatDependencies ¶
FormatDependencies renders dependency query results.
func FormatImpact ¶
func FormatImpact(result ImpactResult, max int) string
FormatImpact renders a dependency blast-radius query with direction labels.
func FormatImpactSummary ¶
func FormatImpactSummary(result ImpactResult, max int) string
FormatImpactSummary renders a compact, counts-plus-top-names version of an impact result, sized to inject into planning-turn context without spending the full FormatImpact token budget. max caps how many names are listed per group (default 3); it does not limit the counts themselves.
func FormatNodes ¶
FormatNodes renders a bounded flat list of nodes, usually filter results.
func FormatSubsystemOverview ¶
func FormatSubsystemOverview(o SubsystemOverview, max int) string
FormatSubsystemOverview renders a subsystem overview: entry points, public surface, core types, tests, and key dependencies for one directory.
func FormatTree ¶
FormatTree renders a bounded outline suitable for humans and model context.
func IsTestFor ¶
IsTestFor reports whether candidateRel looks like a test file for targetRel, using conservative per-language filename conventions. Matching is path-only (no content parsing) and deliberately approximate — false negatives are preferred over false positives across unrelated packages, so candidates must live in the same directory as the target.
func MermaidDiagram ¶
MermaidDiagram renders a bounded import graph. When focus is non-empty, the graph is narrowed to direct dependencies/dependents of the matching file.
func Projection ¶
Projection returns a compact structure summary for agent context injection.
Types ¶
type BuildOptions ¶
type BuildOptions struct {
Root string
MaxFiles int
Source SymbolSource
// WatchDebounce controls how long CachedProvider.StartWatch waits after
// the last relevant filesystem event before applying pending changes.
// Zero defaults to 300ms in production; tests can set a few
// milliseconds so watch-triggered-rebuild tests aren't slow.
WatchDebounce time.Duration
}
BuildOptions controls a CodeIndex rebuild.
type BuilderProvider ¶
type BuilderProvider struct{ Options BuildOptions }
BuilderProvider rebuilds the index on demand.
type CachedProvider ¶
type CachedProvider struct {
Options BuildOptions
// contains filtered or unexported fields
}
CachedProvider reuses the last index until supported source file paths, mtimes, sizes, or counts change. Without a running watch (see StartWatch) that check is a full workspace walk on every Index call; with one running, Index instead checks a cheap in-memory dirty flag and, when set, patches only the specific paths the watcher observed changing into a persistent buildState (see applyChanges) rather than re-walking and re-parsing the whole workspace.
func (*CachedProvider) Close ¶
func (p *CachedProvider) Close()
Close stops the watch goroutine started by StartWatch. It is a no-op when StartWatch was never called or never actually started a watch.
func (*CachedProvider) Index ¶
func (p *CachedProvider) Index(ctx context.Context) (*CodeIndex, error)
func (*CachedProvider) StartWatch ¶
func (p *CachedProvider) StartWatch(ctx context.Context) error
StartWatch enables watcher-backed cache invalidation. It is a soft-failure feature: an oversized repo, a fsnotify.NewWatcher error (e.g. a platform without inotify/kqueue, or the OS watch-descriptor limit), or any other setup problem simply leaves watchStarted false, and Index falls back to exactly today's per-call fingerprint walk. The returned error is only non-nil for a bad Options.Root; callers can safely ignore it and keep using the provider either way.
type CodeIndex ¶
type CodeIndex struct {
// contains filtered or unexported fields
}
CodeIndex is an immutable structural snapshot. Mutating rebuilds publish a new instance instead of editing this one in place.
func Build ¶
func Build(ctx context.Context, opts BuildOptions) (*CodeIndex, error)
Build walks the workspace and returns an immutable structure snapshot.
func NewIndex ¶
func NewIndex(root string, rootID NodeID, nodes map[NodeID]Node, children map[NodeID][]NodeID, edges ...[]Edge) *CodeIndex
NewIndex constructs an immutable snapshot from prebuilt nodes and edges.
func (*CodeIndex) Cycles ¶
Cycles returns import cycles, optionally narrowed to cycles involving a matching file/path query. The result is capped and deterministic for the same indexed graph.
func (*CodeIndex) Dependencies ¶
Dependencies returns outgoing import edges for a matching file node.
func (*CodeIndex) Dependents ¶
Dependents returns incoming import edges for a matching file node.
func (*CodeIndex) Edges ¶
Edges returns a copy of all edges, filtered by kind when kind is non-empty.
func (*CodeIndex) Filter ¶
Filter returns nodes whose path, name, kind, or container matches query. It is intentionally simple and deterministic; TUI fuzzy ranking can layer on later.
func (*CodeIndex) Impact ¶
func (i *CodeIndex) Impact(pathOrQuery string, depth, max int) ImpactResult
Impact returns direct dependencies, direct dependents, transitive dependents, and import cycles involving the target file. Depth limits only the transitive reverse traversal; use MaxDepthAll for an unbounded walk.
func (*CodeIndex) RootPath ¶
RootPath returns the absolute workspace root captured by this snapshot.
func (*CodeIndex) SuggestedContext ¶
func (i *CodeIndex) SuggestedContext(paths []string, max int) []ContextSuggestion
SuggestedContext returns a small, deterministic set of files related to the supplied files or directory prefixes. Changed targets rank first, followed by their imports, importers, and likely tests.
func (*CodeIndex) SymbolsForFile ¶
SymbolsForFile returns symbol children for one repo-relative or absolute path.
type ContextSuggestion ¶
ContextSuggestion is a file worth attaching as context and the reason it was selected.
type FallbackSource ¶
type FallbackSource struct{}
FallbackSource uses yottacode's offline syntax layer when available, then the conservative regex scanner. It keeps /map useful without requiring users to install language servers first.
type ImpactResult ¶
type ImpactResult struct {
Target Node
DirectDependencies []Node
DirectDependents []Node
TransitiveDependents []Node
Cycles [][]Node
// LikelyTests are files in the target's neighborhood that plausibly test
// it, per IsTestFor. LikelyDocs are indexed markdown files that mention
// the target. Both are best-effort relevance signals, not exhaustive.
LikelyTests []Node
LikelyDocs []Node
}
ImpactResult is a bounded blast-radius query over import edges.
type LSPSource ¶
LSPSource tries a live language server first and falls back to approximate regex symbols when the server is unavailable or lacks document symbols.
type Node ¶
type Node struct {
ID NodeID
Parent NodeID
Kind NodeKind
Name string
Path string
RelPath string
Language string
Symbol SymbolInfo
Stats Stats
}
Node is one immutable item in a CodeIndex snapshot.
type NodeID ¶
type NodeID string
NodeID is stable within one workspace snapshot. IDs are repo-relative where possible so TUI state and agent output remain readable and deterministic.
type Position ¶
Position is a zero-based source position. It mirrors LSP coordinates but keeps the codemap package independent from a specific symbol provider.
type Provider ¶
Provider returns the latest code-map snapshot. TUI rebuilds can swap the snapshot while agent tools keep depending on this tiny read-only surface.
type Reason ¶
type Reason string
Reason explains why a node was suggested as relevant context around a change. Multiple reasons can apply to the same node; callers combine them.
type StaticProvider ¶
type StaticProvider struct{ Snapshot *CodeIndex }
StaticProvider returns one immutable index, useful in tests and one-shot runs.
type SubsystemOverview ¶
type SubsystemOverview struct {
Dir Node
EntryPoints []Node
PublicSurface []Node
CoreTypes []Node
Tests []Node
KeyDependencies []Node
}
SubsystemOverview summarizes one directory: entry points, exported public surface, core types, tests, and key external dependencies. It answers "where do I start in this subsystem?" for `/map <dir>`.
func BuildSubsystemOverview ¶
func BuildSubsystemOverview(idx *CodeIndex, dirRelPath string, max int) (SubsystemOverview, bool)
BuildSubsystemOverview reports a subsystem overview for one repo-relative directory. ok is false when dirRelPath doesn't match an indexed directory exactly. max bounds the public-surface, core-type, and tests sections; entry points and key dependencies use their own smaller caps.
type SuggestedItem ¶
SuggestedItem is one ranked candidate for prompt context around a change.
func SuggestedContext ¶
func SuggestedContext(idx *CodeIndex, changedRelPaths []string, max int) []SuggestedItem
SuggestedContext ranks files worth attaching before asking the agent about a set of changed files: the changed files themselves, their direct import neighbors, their likely tests, and docs that mention them.
type SymbolInfo ¶
SymbolInfo stores metadata only symbol nodes need.