codemap

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Aug 13, 2026 License: MIT Imports: 15 Imported by: 0

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

View Source
const MaxDepthAll = -1

MaxDepthAll means dependency traversal should continue until exhausted.

Variables

This section is empty.

Functions

func FormatCycles

func FormatCycles(cycles [][]Node, max int) string

FormatCycles renders import cycles for humans and agents.

func FormatDependencies

func FormatDependencies(title string, nodes []Node, max int) string

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 FormatNodes

func FormatNodes(nodes []Node, max int) string

FormatNodes renders a bounded flat list of nodes, usually filter results.

func FormatTree

func FormatTree(idx *CodeIndex, max int) string

FormatTree renders a bounded outline suitable for humans and model context.

func MermaidDiagram

func MermaidDiagram(idx *CodeIndex, focus string, maxEdges int) string

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

func Projection(idx *CodeIndex, max int) string

Projection returns a compact structure summary for agent context injection.

Types

type BuildOptions

type BuildOptions struct {
	Root     string
	MaxFiles int
	Source   SymbolSource
}

BuildOptions controls a CodeIndex rebuild.

type BuilderProvider

type BuilderProvider struct{ Options BuildOptions }

BuilderProvider rebuilds the index on demand.

func (BuilderProvider) Index

func (p BuilderProvider) Index(ctx context.Context) (*CodeIndex, error)

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. This is not a file watcher yet, but it avoids rebuilding the map on every /map render or agent query while still reflecting normal edits on the next request.

func (*CachedProvider) Index

func (p *CachedProvider) Index(ctx context.Context) (*CodeIndex, error)

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) Children

func (i *CodeIndex) Children(id NodeID) []NodeID

Children returns a copy of a node's ordered child IDs.

func (*CodeIndex) Count

func (i *CodeIndex) Count() int

Count returns the number of indexed nodes.

func (*CodeIndex) Cycles

func (i *CodeIndex) Cycles(pathOrQuery string, max int) [][]Node

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

func (i *CodeIndex) Dependencies(pathOrQuery string, max int) []Node

Dependencies returns outgoing import edges for a matching file node.

func (*CodeIndex) Dependents

func (i *CodeIndex) Dependents(pathOrQuery string, max int) []Node

Dependents returns incoming import edges for a matching file node.

func (*CodeIndex) Edges

func (i *CodeIndex) Edges(kind EdgeKind) []Edge

Edges returns a copy of all edges, filtered by kind when kind is non-empty.

func (*CodeIndex) Filter

func (i *CodeIndex) Filter(query string, max int) []Node

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) Node

func (i *CodeIndex) Node(id NodeID) (Node, bool)

Node returns a node by ID.

func (*CodeIndex) Nodes

func (i *CodeIndex) Nodes() []Node

Nodes returns every node in deterministic ID order.

func (*CodeIndex) Root

func (i *CodeIndex) Root() NodeID

Root returns the root node ID for tree rendering.

func (*CodeIndex) RootPath

func (i *CodeIndex) RootPath() string

RootPath returns the absolute workspace root captured by this snapshot.

func (*CodeIndex) SymbolsForFile

func (i *CodeIndex) SymbolsForFile(path string) []Node

SymbolsForFile returns symbol children for one repo-relative or absolute path.

type Edge

type Edge struct {
	From NodeID
	To   NodeID
	Kind EdgeKind
	Meta string
}

Edge is a directed relationship between two indexed nodes.

type EdgeKind

type EdgeKind string
const (
	EdgeImports EdgeKind = "imports"
)

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.

func (FallbackSource) Symbols

func (FallbackSource) Symbols(ctx context.Context, path string) ([]lsp.Symbol, string, error)

type ImpactResult

type ImpactResult struct {
	Target               Node
	DirectDependencies   []Node
	DirectDependents     []Node
	TransitiveDependents []Node
	Cycles               [][]Node
}

ImpactResult is a bounded blast-radius query over import edges.

type LSPSource

type LSPSource struct {
	Manager *lsp.Manager
	Servers map[string][]string
	Root    string
}

LSPSource tries a live language server first and falls back to approximate regex symbols when the server is unavailable or lacks document symbols.

func (LSPSource) Symbols

func (s LSPSource) Symbols(ctx context.Context, path string) ([]lsp.Symbol, string, error)

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 NodeKind

type NodeKind string

NodeKind identifies the coarse graph layer a node belongs to.

const (
	NodeDirectory NodeKind = "directory"
	NodeFile      NodeKind = "file"
	NodeSymbol    NodeKind = "symbol"
)

type Position

type Position struct {
	Line      int
	Character int
}

Position is a zero-based source position. It mirrors LSP coordinates but keeps the codemap package independent from a specific symbol provider.

type Provider

type Provider interface {
	Index(ctx context.Context) (*CodeIndex, error)
}

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 Range

type Range struct {
	Start Position
	End   Position
}

Range is a half-open source range in zero-based line/character coordinates.

type StaticProvider

type StaticProvider struct{ Snapshot *CodeIndex }

StaticProvider returns one immutable index, useful in tests and one-shot runs.

func (StaticProvider) Index

type Stats

type Stats struct {
	LOC      int
	Files    int
	Symbols  int
	Exported int
	Private  int
}

Stats aggregates cheap structure metrics for display and projections.

type SymbolInfo

type SymbolInfo struct {
	Kind      string
	Container string
	Exported  bool
	Range     Range
}

SymbolInfo stores metadata only symbol nodes need.

type SymbolSource

type SymbolSource interface {
	Symbols(ctx context.Context, path string) ([]lsp.Symbol, string, error)
}

SymbolSource provides structural symbols for one source file. Implementations can use LSP, fallback parsers, or both.

Jump to

Keyboard shortcuts

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