xonecas.io
62159
Sy

symb / commit 9f01e43

feat: adds treesiter info as yaml to the context

xonecas2026-02-14 9f01e435d37f8c361740d6654e23a17b6dfe08f9 parent 29dd107

16 files changed

This diff is too big to show in full. The first 1000 lines are below, and every file is listed with its counts.

AGENTS.md+1−0
@@ -17,3 +17,4 @@
1717 - [Lipgloss](https://github.com/charmbracelet/lipgloss).
1818 - Use Makefile commands to build, lint and test.
1919 - Use `journalctl -u ollama --no-pager -n 100` to see ollama logs
20+- Data directory: `~/.config/symb/` (cache.db, credentials.json, logs/)
cmd/symb/main.go+42−12
@@ -18,6 +18,7 @@ import (
1818 "github.com/xonecas/symb/internal/mcptools"
1919 "github.com/xonecas/symb/internal/provider"
2020 "github.com/xonecas/symb/internal/store"
21+ "github.com/xonecas/symb/internal/treesitter"
2122 "github.com/xonecas/symb/internal/tui"
2223 )
2324
@@ -49,31 +50,46 @@ func main() {
4950 }
5051 defer prov.Close()
5152
52- proxy, lspManager, webCache := setupServices(cfg, creds)
53- defer proxy.Close()
54- defer lspManager.StopAll(context.Background())
55- if webCache != nil {
56- defer webCache.Close()
53+ svc := setupServices(cfg, creds)
54+ defer svc.proxy.Close()
55+ defer svc.lspManager.StopAll(context.Background())
56+ if svc.webCache != nil {
57+ defer svc.webCache.Close()
5758 }
5859
59- tools, err := proxy.ListTools(context.Background())
60+ tools, err := svc.proxy.ListTools(context.Background())
6061 if err != nil {
6162 fmt.Printf("Warning: Failed to list tools: %v\n", err)
6263 tools = []mcp.Tool{}
6364 }
6465
6566 sessionID := newSessionID()
66- if webCache != nil {
67- if err := webCache.CreateSession(sessionID); err != nil {
67+ if svc.webCache != nil {
68+ if err := svc.webCache.CreateSession(sessionID); err != nil {
6869 fmt.Printf("Warning: failed to create session: %v\n", err)
6970 }
7071 }
7172
73+ // Build tree-sitter project symbol index.
74+ cwd, err := os.Getwd()
75+ if err != nil {
76+ fmt.Printf("Warning: failed to get working directory: %v\n", err)
77+ cwd = "."
78+ }
79+ tsIndex := treesitter.NewIndex(cwd)
80+ if err := tsIndex.Build(); err != nil {
81+ log.Warn().Err(err).Msg("tree-sitter index build failed")
82+ }
83+
84+ // Wire index into Read/Edit handlers for incremental updates.
85+ svc.readHandler.SetTSIndex(tsIndex)
86+ svc.editHandler.SetTSIndex(tsIndex)
87+
7288 p := tea.NewProgram(
73- tui.New(prov, proxy, tools, providerCfg.Model, webCache, sessionID),
89+ tui.New(prov, svc.proxy, tools, providerCfg.Model, svc.webCache, sessionID, tsIndex),
7490 tea.WithFilter(tui.MouseEventFilter),
7591 )
76- lspManager.SetCallback(func(absPath string, lines map[int]int) {
92+ svc.lspManager.SetCallback(func(absPath string, lines map[int]int) {
7793 p.Send(tui.LSPDiagnosticsMsg{FilePath: absPath, Lines: lines})
7894 })
7995
@@ -115,7 +131,15 @@ func resolveProvider(cfg *config.Config, registry *provider.Registry) (string, c
115131 return name, pcfg
116132 }
117133
118-func setupServices(cfg *config.Config, creds *config.Credentials) (*mcp.Proxy, *lsp.Manager, *store.Cache) {
134+type services struct {
135+ proxy *mcp.Proxy
136+ lspManager *lsp.Manager
137+ webCache *store.Cache
138+ readHandler *mcptools.ReadHandler
139+ editHandler *mcptools.EditHandler
140+}
141+
142+func setupServices(cfg *config.Config, creds *config.Credentials) services {
119143 var mcpClient mcp.UpstreamClient
120144 if cfg.MCP.Upstream != "" {
121145 mcpClient = mcp.NewClient(cfg.MCP.Upstream)
@@ -143,7 +167,13 @@ func setupServices(cfg *config.Config, creds *config.Credentials) (*mcp.Proxy, *
143167 exaKey := creds.GetAPIKey("exa_ai")
144168 proxy.RegisterTool(mcptools.NewWebSearchTool(), mcptools.MakeWebSearchHandler(webCache, exaKey, ""))
145169
146- return proxy, lspManager, webCache
170+ return services{
171+ proxy: proxy,
172+ lspManager: lspManager,
173+ webCache: webCache,
174+ readHandler: readHandler,
175+ editHandler: editHandler,
176+ }
147177 }
148178
149179 func openWebCache(cfg *config.Config) *store.Cache {
docs/DESIGN.md+6−2
@@ -45,6 +45,9 @@ prepends to system prompt. Checks `~/.config/symb/AGENTS.md` too.
4545 - symb.md: symb specific prompt text
4646 - <model name>-<segment name>.md: model specific instructions
4747 - base: common model instructions. and template tags to insert symb and model specific segments.
48+ - Ensure proper ordering of prompt
49+ - Fix language, system prompt should be presented as model afnostic, the LLM shouldn't know anythign about other promots.
50+ - Consider token count and optimize where possible
4851
4952 ### Providers (`internal/provider`)
5053
@@ -89,12 +92,13 @@ Retry-After parsing.
8992 - each message includes all tool calls
9093 - opening the app opens a new session (more controls later). Same behaviour
9194
92-## Features waiting implementation for current version:
93-
9495 ### Tree-Sitter Context
9596
9697 Parse project with tree-sitter for structural awareness. Feed relevant
9798 symbols/scope to LLM as auto-context instead of whole files.
99+**Uses 4k tokens on a small project like this** -- ohoh... Maybe return updated treesiter information with Grep matches for less token usage overall? Context inhection then can reduced/compacted?
100+
101+## Features waiting implementation for current version:
98102
99103 ### Undo!
100104
go.mod+1−0
@@ -39,6 +39,7 @@ require (
3939 github.com/ncruces/go-strftime v1.0.0 // indirect
4040 github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
4141 github.com/rivo/uniseg v0.4.7 // indirect
42+ github.com/smacker/go-tree-sitter v0.0.0-20240827094217-dd81d9e9be82 // indirect
4243 github.com/sourcegraph/jsonrpc2 v0.2.1 // indirect
4344 github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect
4445 golang.org/x/exp v0.0.0-20251023183803-a4bb9ffd2546 // indirect
go.sum+2−0
@@ -78,6 +78,8 @@ github.com/rs/zerolog v1.34.0 h1:k43nTLIwcTVQAncfCw4KZ2VY6ukYoZaBPNOE8txlOeY=
7878 github.com/rs/zerolog v1.34.0/go.mod h1:bJsvje4Z08ROH4Nhs5iH600c3IkWhwp44iRc54W6wYQ=
7979 github.com/sashabaranov/go-openai v1.41.2 h1:vfPRBZNMpnqu8ELsclWcAvF19lDNgh1t6TVfFFOPiSM=
8080 github.com/sashabaranov/go-openai v1.41.2/go.mod h1:lj5b/K+zjTSFxVLijLSTDZuP7adOgerWeFyZLUhAKRg=
81+github.com/smacker/go-tree-sitter v0.0.0-20240827094217-dd81d9e9be82 h1:6C8qej6f1bStuePVkLSFxoU22XBS165D3klxlzRg8F4=
82+github.com/smacker/go-tree-sitter v0.0.0-20240827094217-dd81d9e9be82/go.mod h1:xe4pgH49k4SsmkQq5OT8abwhWmnzkhpgnXeekbx2efw=
8183 github.com/sourcegraph/jsonrpc2 v0.2.1 h1:2GtljixMQYUYCmIg7W9aF2dFmniq/mOr2T9tFRh6zSQ=
8284 github.com/sourcegraph/jsonrpc2 v0.2.1/go.mod h1:ZafdZgk/axhT1cvZAPOhw+95nz2I/Ra5qMlU4gTRwIo=
8385 github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e h1:JVG44RsyaB9T2KIHavMF/ppJZNG9ZpyihvCd0w101no=
internal/llm/prompt.go+18−6
@@ -7,6 +7,8 @@ import (
77 "os"
88 "path/filepath"
99 "strings"
10+
11+ "github.com/xonecas/symb/internal/treesitter"
1012 )
1113
1214 // Embedded prompt files
@@ -94,17 +96,27 @@ func LoadAgentInstructions() string {
9496 }
9597
9698 // BuildSystemPrompt constructs the complete system prompt by combining
97-// the model-specific base prompt with any AGENTS.md instructions.
98-func BuildSystemPrompt(modelID string) string {
99+// the model-specific base prompt with any AGENTS.md instructions and
100+// optionally a tree-sitter project symbol outline.
101+func BuildSystemPrompt(modelID string, idx *treesitter.Index) string {
99102 basePrompt := SelectPrompt(modelID)
100103 agentInstructions := LoadAgentInstructions()
101104
102- if agentInstructions == "" {
103- return basePrompt
105+ var parts []string
106+ if agentInstructions != "" {
107+ parts = append(parts, agentInstructions)
108+ }
109+
110+ // Append tree-sitter project outline if available.
111+ if idx != nil {
112+ outline := treesitter.FormatOutline(idx.Snapshot())
113+ if outline != "" {
114+ parts = append(parts, outline)
115+ }
104116 }
105117
106- // Prepend agent instructions to base prompt
107- return agentInstructions + "\n\n---\n\n" + basePrompt
118+ parts = append(parts, basePrompt)
119+ return strings.Join(parts, "\n\n---\n\n")
108120 }
109121
110122 // readFileIfExists reads a file if it exists, returns empty string otherwise.
internal/mcptools/edit.go+11−0
@@ -12,6 +12,7 @@ import (
1212 "github.com/xonecas/symb/internal/hashline"
1313 "github.com/xonecas/symb/internal/lsp"
1414 "github.com/xonecas/symb/internal/mcp"
15+ "github.com/xonecas/symb/internal/treesitter"
1516 )
1617
1718 // EditArgs represents arguments for the Edit tool.
@@ -110,6 +111,7 @@ After each edit you receive fresh hashes — use those for subsequent edits, not
110111 type EditHandler struct {
111112 tracker *FileReadTracker
112113 lspManager *lsp.Manager
114+ tsIndex *treesitter.Index
113115 }
114116
115117 // NewEditHandler creates a handler for the Edit tool.
@@ -117,6 +119,9 @@ func NewEditHandler(tracker *FileReadTracker, lspManager *lsp.Manager) *EditHand
117119 return &EditHandler{tracker: tracker, lspManager: lspManager}
118120 }
119121
122+// SetTSIndex sets the tree-sitter index for incremental updates on edit.
123+func (h *EditHandler) SetTSIndex(idx *treesitter.Index) { h.tsIndex = idx }
124+
120125 // Handle implements the mcp.ToolHandler interface.
121126 func (h *EditHandler) Handle(ctx context.Context, arguments json.RawMessage) (*mcp.ToolResult, error) {
122127 var args EditArgs
@@ -199,6 +204,9 @@ func (h *EditHandler) applyEdit(ctx context.Context, absPath string, args EditAr
199204 diags := h.lspManager.NotifyAndWait(ctx, absPath, 5*time.Second)
200205 text += lsp.FormatDiagnostics(args.File, diags)
201206 }
207+ if h.tsIndex != nil {
208+ h.tsIndex.UpdateFile(absPath)
209+ }
202210
203211 return &mcp.ToolResult{
204212 Content: []mcp.ContentBlock{{Type: "text", Text: text}},
@@ -231,6 +239,9 @@ func (h *EditHandler) handleCreate(ctx context.Context, absPath, displayPath str
231239 diags := h.lspManager.NotifyAndWait(ctx, absPath, 5*time.Second)
232240 text += lsp.FormatDiagnostics(displayPath, diags)
233241 }
242+ if h.tsIndex != nil {
243+ h.tsIndex.UpdateFile(absPath)
244+ }
234245
235246 return &mcp.ToolResult{
236247 Content: []mcp.ContentBlock{{Type: "text", Text: text}},
internal/mcptools/open.go+8−0
@@ -11,6 +11,7 @@ import (
1111 "github.com/xonecas/symb/internal/hashline"
1212 "github.com/xonecas/symb/internal/lsp"
1313 "github.com/xonecas/symb/internal/mcp"
14+ "github.com/xonecas/symb/internal/treesitter"
1415 )
1516
1617 // ReadArgs represents arguments for the Read tool.
@@ -41,6 +42,7 @@ func NewReadTool() mcp.Tool {
4142 type ReadHandler struct {
4243 tracker *FileReadTracker
4344 lspManager *lsp.Manager
45+ tsIndex *treesitter.Index
4446 }
4547
4648 // NewReadHandler creates a handler for the Read tool.
@@ -48,6 +50,9 @@ func NewReadHandler(tracker *FileReadTracker, lspManager *lsp.Manager) *ReadHand
4850 return &ReadHandler{tracker: tracker, lspManager: lspManager}
4951 }
5052
53+// SetTSIndex sets the tree-sitter index for incremental updates on read.
54+func (h *ReadHandler) SetTSIndex(idx *treesitter.Index) { h.tsIndex = idx }
55+
5156 // Handle implements the mcp.ToolHandler interface.
5257 func (h *ReadHandler) Handle(_ context.Context, arguments json.RawMessage) (*mcp.ToolResult, error) {
5358 var args ReadArgs
@@ -72,6 +77,9 @@ func (h *ReadHandler) Handle(_ context.Context, arguments json.RawMessage) (*mcp
7277 if h.lspManager != nil {
7378 go h.lspManager.TouchFile(context.Background(), absPath)
7479 }
80+ if h.tsIndex != nil {
81+ go h.tsIndex.UpdateFile(absPath)
82+ }
7583
7684 lines := strings.Split(string(content), "\n")
7785 selectedContent, startLine, err := extractRange(lines, string(content), args.Start, args.End)
internal/treesitter/context.go+136−0
@@ -0,0 +1,136 @@
1+package treesitter
2+
3+import (
4+ "fmt"
5+ "sort"
6+ "strings"
7+)
8+
9+// MaxOutlineBytes caps the outline to avoid consuming too much of the LLM
10+// context window. ~16KB ≈ 4-5K tokens, enough for ~100 Go files.
11+const MaxOutlineBytes = 16 * 1024
12+
13+// FormatOutline produces a compact YAML-like project outline for LLM system
14+// prompt injection. Groups methods by receiver type under each file.
15+// Output is capped at MaxOutlineBytes to protect the context window.
16+//
17+// Example output:
18+//
19+// # Project Symbols
20+// internal/mcp/proxy.go:
21+// Proxy: RegisterTool, CallTool, ListTools
22+// fn: NewProxy, parseRetryAfter
23+// type: ToolHandler
24+func FormatOutline(snap map[string][]Symbol) string {
25+ if len(snap) == 0 {
26+ return ""
27+ }
28+
29+ paths := make([]string, 0, len(snap))
30+ for p := range snap {
31+ paths = append(paths, p)
32+ }
33+ sort.Strings(paths)
34+
35+ var b strings.Builder
36+ b.WriteString("# Project Symbols\n")
37+
38+ for _, path := range paths {
39+ syms := snap[path]
40+ text := formatFileCompact(syms)
41+ if text == "" {
42+ continue
43+ }
44+ entry := fmt.Sprintf("%s:\n%s", path, text)
45+ if b.Len()+len(entry) > MaxOutlineBytes {
46+ fmt.Fprintf(&b, "# ... truncated (%d files total)\n", len(paths))
47+ break
48+ }
49+ b.WriteString(entry)
50+ }
51+ return b.String()
52+}
53+
54+// fileGroups collects symbols into categories for compact rendering.
55+type fileGroups struct {
56+ methods map[string][]string // receiver -> method names
57+ funcs []string
58+ types []string
59+ consts []string
60+ vars []string
61+}
62+
63+func newFileGroups() *fileGroups {
64+ return &fileGroups{methods: make(map[string][]string)}
65+}
66+
67+func (g *fileGroups) add(s Symbol) {
68+ switch s.Kind {
69+ case KindPackage, KindImport:
70+ // skip
71+ case KindFunction:
72+ g.funcs = append(g.funcs, s.Name)
73+ case KindMethod:
74+ recv := s.Receiver
75+ if recv == "" {
76+ recv = "?"
77+ }
78+ g.methods[recv] = append(g.methods[recv], s.Name)
79+ case KindStruct:
80+ g.types = append(g.types, s.Name+" (struct)")
81+ case KindInterface:
82+ g.types = append(g.types, s.Name+" (interface)")
83+ case KindType:
84+ g.types = append(g.types, s.Name)
85+ case KindConst:
86+ g.consts = append(g.consts, s.Name)
87+ case KindVar:
88+ g.vars = append(g.vars, s.Name)
89+ }
90+}
91+
92+func (g *fileGroups) empty() bool {
93+ return len(g.funcs) == 0 && len(g.methods) == 0 &&
94+ len(g.types) == 0 && len(g.consts) == 0 && len(g.vars) == 0
95+}
96+
97+func (g *fileGroups) render() string {
98+ var b strings.Builder
99+
100+ if len(g.types) > 0 {
101+ fmt.Fprintf(&b, " type: %s\n", strings.Join(g.types, ", "))
102+ }
103+
104+ recvs := make([]string, 0, len(g.methods))
105+ for r := range g.methods {
106+ recvs = append(recvs, r)
107+ }
108+ sort.Strings(recvs)
109+ for _, recv := range recvs {
110+ fmt.Fprintf(&b, " %s: %s\n", recv, strings.Join(g.methods[recv], ", "))
111+ }
112+
113+ if len(g.funcs) > 0 {
114+ fmt.Fprintf(&b, " fn: %s\n", strings.Join(g.funcs, ", "))
115+ }
116+ if len(g.consts) > 0 {
117+ fmt.Fprintf(&b, " const: %s\n", strings.Join(g.consts, ", "))
118+ }
119+ if len(g.vars) > 0 {
120+ fmt.Fprintf(&b, " var: %s\n", strings.Join(g.vars, ", "))
121+ }
122+
123+ return b.String()
124+}
125+
126+// formatFileCompact produces a compact per-file representation.
127+func formatFileCompact(syms []Symbol) string {
128+ g := newFileGroups()
129+ for _, s := range syms {
130+ g.add(s)
131+ }
132+ if g.empty() {
133+ return ""
134+ }
135+ return g.render()
136+}
internal/treesitter/index.go+124−0
@@ -0,0 +1,124 @@
1+package treesitter
2+
3+import (
4+ "os"
5+ "path/filepath"
6+ "sync"
7+
8+ "github.com/xonecas/symb/internal/filesearch"
9+)
10+
11+// Index holds a project-wide symbol map built from tree-sitter parsing.
12+type Index struct {
13+ mu sync.RWMutex
14+ files map[string][]Symbol // relPath -> symbols
15+ root string
16+}
17+
18+// NewIndex creates an empty index rooted at dir.
19+func NewIndex(root string) *Index {
20+ return &Index{
21+ files: make(map[string][]Symbol),
22+ root: root,
23+ }
24+}
25+
26+// Build walks the project tree, parsing every supported file.
27+// Respects .gitignore via filesearch.GitignoreMatcher.
28+func (idx *Index) Build() error {
29+ gitignorePath := filepath.Join(idx.root, ".gitignore")
30+ matcher, err := filesearch.NewGitignoreMatcher(gitignorePath)
31+ if err != nil {
32+ matcher, _ = filesearch.NewGitignoreMatcher("")
33+ }
34+
35+ idx.mu.Lock()
36+ defer idx.mu.Unlock()
37+
38+ return filepath.WalkDir(idx.root, func(path string, d os.DirEntry, walkErr error) error {
39+ if walkErr != nil {
40+ return nil
41+ }
42+ rel, err := filepath.Rel(idx.root, path)
43+ if err != nil {
44+ return nil
45+ }
46+
47+ // Skip .git and gitignored paths.
48+ if d.IsDir() {
49+ if d.Name() == ".git" {
50+ return filepath.SkipDir
51+ }
52+ if matcher.Matches(rel, true) {
53+ return filepath.SkipDir
54+ }
55+ return nil
56+ }
57+ if matcher.Matches(rel, false) {
58+ return nil
59+ }
60+ if !Supported(path) {
61+ return nil
62+ }
63+
64+ // Skip large files (>1MB).
65+ info, err := d.Info()
66+ if err != nil || info.Size() > 1<<20 {
67+ return nil
68+ }
69+
70+ syms, err := ParseFile(path)
71+ if err != nil || len(syms) == 0 {
72+ return nil
73+ }
74+ idx.files[rel] = syms
75+ return nil
76+ })
77+}
78+
79+// UpdateFile re-parses a single file and updates the index.
80+func (idx *Index) UpdateFile(absPath string) {
81+ rel, err := filepath.Rel(idx.root, absPath)
82+ if err != nil || !Supported(absPath) {
83+ return
84+ }
85+ syms, err := ParseFile(absPath)
86+
87+ idx.mu.Lock()
88+ defer idx.mu.Unlock()
89+
90+ if err != nil || len(syms) == 0 {
91+ delete(idx.files, rel)
92+ return
93+ }
94+ idx.files[rel] = syms
95+}
96+
97+// Files returns a snapshot of all indexed file paths (sorted is not guaranteed).
98+func (idx *Index) Files() []string {
99+ idx.mu.RLock()
100+ defer idx.mu.RUnlock()
101+ paths := make([]string, 0, len(idx.files))
102+ for p := range idx.files {
103+ paths = append(paths, p)
104+ }
105+ return paths
106+}
107+
108+// Symbols returns symbols for a given relative path.
109+func (idx *Index) Symbols(relPath string) []Symbol {
110+ idx.mu.RLock()
111+ defer idx.mu.RUnlock()
112+ return idx.files[relPath]
113+}
114+
115+// Snapshot returns a copy of the full index map.
116+func (idx *Index) Snapshot() map[string][]Symbol {
117+ idx.mu.RLock()
118+ defer idx.mu.RUnlock()
119+ out := make(map[string][]Symbol, len(idx.files))
120+ for k, v := range idx.files {
121+ out[k] = v
122+ }
123+ return out
124+}
internal/treesitter/index_test.go+33−0
@@ -0,0 +1,33 @@
1+package treesitter
2+
3+import (
4+ "os"
5+ "testing"
6+)
7+
8+func TestIndexBuild(t *testing.T) {
9+ cwd, err := os.Getwd()
10+ if err != nil {
11+ t.Fatal(err)
12+ }
13+ // Walk up to project root (internal/treesitter -> project root)
14+ root := cwd + "/../.."
15+
16+ idx := NewIndex(root)
17+ if err := idx.Build(); err != nil {
18+ t.Fatalf("Build: %v", err)
19+ }
20+
21+ files := idx.Files()
22+ if len(files) == 0 {
23+ t.Fatal("no files indexed")
24+ }
25+ t.Logf("Indexed %d files", len(files))
26+
27+ snap := idx.Snapshot()
28+ outline := FormatOutline(snap)
29+ if outline == "" {
30+ t.Fatal("empty outline")
31+ }
32+ t.Logf("Outline (%d bytes):\n%s", len(outline), outline)
33+}
internal/treesitter/parser.go+313−0
@@ -0,0 +1,313 @@
1+package treesitter
2+
3+import (
4+ "context"
5+ "os"
6+ "path/filepath"
7+ "strings"
8+
9+ sitter "github.com/smacker/go-tree-sitter"
10+ "github.com/smacker/go-tree-sitter/golang"
11+)
12+
13+// langForExt returns the tree-sitter language for a file extension, or nil.
14+func langForExt(ext string) *sitter.Language {
15+ switch ext {
16+ case ".go":
17+ return golang.GetLanguage()
18+ default:
19+ return nil
20+ }
21+}
22+
23+// Supported returns true if the file extension has a tree-sitter grammar.
24+func Supported(path string) bool {
25+ return langForExt(strings.ToLower(filepath.Ext(path))) != nil
26+}
27+
28+// ParseFile reads and parses a file, returning its top-level symbols.
29+func ParseFile(path string) ([]Symbol, error) {
30+ src, err := os.ReadFile(path)
31+ if err != nil {
32+ return nil, err
33+ }
34+ return ParseSource(path, src)
35+}
36+
37+// ParseSource parses source bytes and returns top-level symbols.
38+func ParseSource(path string, src []byte) ([]Symbol, error) {
39+ lang := langForExt(strings.ToLower(filepath.Ext(path)))
40+ if lang == nil {
41+ return nil, nil
42+ }
43+
44+ parser := sitter.NewParser()
45+ defer parser.Close()
46+ parser.SetLanguage(lang)
47+
48+ tree, err := parser.ParseCtx(context.Background(), nil, src)
49+ if err != nil {
50+ return nil, err
51+ }
52+ defer tree.Close()
53+
54+ return extractGo(tree.RootNode(), src), nil
55+}
56+
57+// extractGo walks a Go AST root and extracts top-level symbols.
58+func extractGo(root *sitter.Node, src []byte) []Symbol {
59+ var syms []Symbol
60+ count := int(root.ChildCount())
61+
62+ for i := 0; i < count; i++ {
63+ child := root.Child(i)
64+ switch child.Type() {
65+ case "package_clause":
66+ // package_identifier is a named child, not a field.
67+ if nc := child.NamedChild(0); nc != nil && nc.Type() == "package_identifier" {
68+ syms = append(syms, Symbol{
69+ Name: content(nc, src),
70+ Kind: KindPackage,
71+ StartLine: line(child),
72+ EndLine: endLine(child),
73+ })
74+ }
75+
76+ case "import_declaration":
77+ syms = append(syms, extractImport(child, src))
78+
79+ case "function_declaration":
80+ syms = append(syms, extractFunc(child, src))
81+
82+ case "method_declaration":
83+ syms = append(syms, extractMethod(child, src))
84+
85+ case "type_declaration":
86+ syms = append(syms, extractTypeDecl(child, src)...)
87+
88+ case "const_declaration":
89+ syms = append(syms, extractConstVar(child, src, KindConst)...)
90+
91+ case "var_declaration":
92+ syms = append(syms, extractConstVar(child, src, KindVar)...)
93+ }
94+ }
95+ return syms
96+}
97+
98+func extractImport(node *sitter.Node, src []byte) Symbol {
99+ return Symbol{
100+ Name: strings.TrimSpace(content(node, src)),
101+ Kind: KindImport,
102+ StartLine: line(node),
103+ EndLine: endLine(node),
104+ }
105+}
106+
107+func extractFunc(node *sitter.Node, src []byte) Symbol {
108+ name := node.ChildByFieldName("name")
109+ params := node.ChildByFieldName("parameters")
110+ result := node.ChildByFieldName("result")
111+
112+ sym := Symbol{
113+ Kind: KindFunction,
114+ StartLine: line(node),
115+ EndLine: endLine(node),
116+ }
117+ if name != nil {
118+ sym.Name = content(name, src)
119+ }
120+ sym.Signature = buildFuncSig("", sym.Name, params, result, src)
121+ return sym
122+}
123+
124+func extractMethod(node *sitter.Node, src []byte) Symbol {
125+ name := node.ChildByFieldName("name")
126+ receiver := node.ChildByFieldName("receiver")
127+ params := node.ChildByFieldName("parameters")
128+ result := node.ChildByFieldName("result")
129+
130+ sym := Symbol{
131+ Kind: KindMethod,
132+ StartLine: line(node),
133+ EndLine: endLine(node),
134+ }
135+ if name != nil {
136+ sym.Name = content(name, src)
137+ }
138+
139+ var recvStr string
140+ if receiver != nil {
141+ recvStr = content(receiver, src)
142+ // Extract just the type name from receiver for Receiver field.
143+ sym.Receiver = extractReceiverType(receiver, src)
144+ }
145+ sym.Signature = buildFuncSig(recvStr, sym.Name, params, result, src)
146+ return sym
147+}
148+
149+func extractTypeDecl(node *sitter.Node, src []byte) []Symbol {
150+ var syms []Symbol
151+ count := int(node.ChildCount())
152+ for i := 0; i < count; i++ {
153+ child := node.Child(i)
154+ switch child.Type() {
155+ case "type_spec":
156+ syms = append(syms, extractTypeSpec(child, src))
157+ case "type_alias":
158+ syms = append(syms, extractTypeSpec(child, src))
159+ }
160+ }
161+ return syms
162+}
163+
164+func extractTypeSpec(node *sitter.Node, src []byte) Symbol {
165+ name := node.ChildByFieldName("name")
166+ typeNode := node.ChildByFieldName("type")
167+
168+ sym := Symbol{
169+ Kind: KindType,
170+ StartLine: line(node),
171+ EndLine: endLine(node),
172+ }
173+ if name != nil {
174+ sym.Name = content(name, src)
175+ }
176+ if typeNode != nil {
177+ switch typeNode.Type() {
178+ case "struct_type":
179+ sym.Kind = KindStruct
180+ sym.Children = extractStructFields(typeNode, src)
181+ case "interface_type":
182+ sym.Kind = KindInterface
183+ sym.Children = extractInterfaceMethods(typeNode, src)
184+ }
185+ sym.Signature = "type " + sym.Name + " " + typeNode.Type()
186+ }
187+ return sym
188+}
189+
190+func extractStructFields(node *sitter.Node, src []byte) []Symbol {
191+ var fields []Symbol
192+ body := node.ChildByFieldName("body")
193+ if body == nil {
194+ // struct_type has field_declaration_list as child
195+ for i := 0; i < int(node.ChildCount()); i++ {
196+ child := node.Child(i)
197+ if child.Type() == "field_declaration_list" {
198+ body = child
199+ break
200+ }
201+ }
202+ }
203+ if body == nil {
204+ return nil
205+ }
206+ for i := 0; i < int(body.ChildCount()); i++ {
207+ child := body.Child(i)
208+ if child.Type() == "field_declaration" {
209+ nameNode := child.ChildByFieldName("name")
210+ typeNode := child.ChildByFieldName("type")
211+ if nameNode != nil {
212+ f := Symbol{
213+ Name: content(nameNode, src),
214+ Kind: KindVar,
215+ StartLine: line(child),
216+ EndLine: endLine(child),
217+ }
218+ if typeNode != nil {
219+ f.Signature = content(nameNode, src) + " " + content(typeNode, src)
220+ }
221+ fields = append(fields, f)
222+ }
223+ }
224+ }
225+ return fields
226+}
227+
228+func extractInterfaceMethods(node *sitter.Node, src []byte) []Symbol {
229+ var methods []Symbol
230+ for i := 0; i < int(node.ChildCount()); i++ {
231+ child := node.Child(i)
232+ if child.Type() == "method_elem" || child.Type() == "method_spec" {
233+ nameNode := child.ChildByFieldName("name")
234+ if nameNode != nil {
235+ m := Symbol{
236+ Name: content(nameNode, src),
237+ Kind: KindMethod,
238+ StartLine: line(child),
239+ EndLine: endLine(child),
240+ Signature: content(child, src),
241+ }
242+ methods = append(methods, m)
243+ }
244+ }
245+ }
246+ return methods
247+}
248+
249+func extractConstVar(node *sitter.Node, src []byte, kind SymbolKind) []Symbol {
250+ var syms []Symbol
251+ count := int(node.ChildCount())
252+ for i := 0; i < count; i++ {
253+ child := node.Child(i)
254+ if child.Type() == "const_spec" || child.Type() == "var_spec" {
255+ nameNode := child.ChildByFieldName("name")
256+ if nameNode != nil {
257+ syms = append(syms, Symbol{
258+ Name: content(nameNode, src),
259+ Kind: kind,
260+ StartLine: line(child),
261+ EndLine: endLine(child),
262+ })
263+ }
264+ }
265+ }
266+ return syms
267+}
268+
269+func extractReceiverType(receiver *sitter.Node, src []byte) string {
270+ // Walk into parameter_list -> parameter_declaration -> type
271+ for i := 0; i < int(receiver.ChildCount()); i++ {
272+ child := receiver.Child(i)
273+ if child.Type() == "parameter_declaration" {
274+ typeNode := child.ChildByFieldName("type")
275+ if typeNode != nil {
276+ return content(typeNode, src)
277+ }
278+ }
279+ }
280+ return ""
281+}
282+
283+func buildFuncSig(receiver, name string, params, result *sitter.Node, src []byte) string {
284+ var b strings.Builder
285+ b.WriteString("func ")
286+ if receiver != "" {
287+ b.WriteString(receiver)
288+ b.WriteByte(' ')
289+ }
290+ b.WriteString(name)
291+ if params != nil {
292+ b.WriteString(content(params, src))
293+ }
294+ if result != nil {
295+ b.WriteByte(' ')
296+ b.WriteString(content(result, src))
297+ }
298+ return b.String()
299+}
300+
301+// helpers
302+
303+func content(node *sitter.Node, src []byte) string {
304+ return node.Content(src)
305+}
306+
307+func line(node *sitter.Node) int {
308+ return int(node.StartPoint().Row) + 1 // 1-indexed
309+}
310+
311+func endLine(node *sitter.Node) int {
312+ return int(node.EndPoint().Row) + 1
313+}
internal/treesitter/parser_test.go+115−0
@@ -0,0 +1,115 @@
1+package treesitter
2+
3+import (
4+ "strings"
5+ "testing"
6+)
7+
8+func TestParseSource_Go(t *testing.T) {
9+ src := []byte(`package main
10+
11+import "fmt"
12+
13+const Version = "1.0"
14+
15+var Debug bool
16+
17+type Server struct {
18+ addr string
19+ port int
20+}
21+
22+type Handler interface {
23+ Handle(req string) string
24+}
25+
26+func main() {
27+ fmt.Println("hello")
28+}
29+
30+func (s *Server) Start() error {
31+ return nil
32+}
33+`)
34+
35+ syms, err := ParseSource("test.go", src)
36+ if err != nil {
37+ t.Fatalf("ParseSource: %v", err)
38+ }
39+
40+ // Check we got the expected symbols (name+kind pairs to handle duplicates like "main")
41+ type symKey struct {
42+ name string
43+ kind SymbolKind
44+ }
45+ want := []symKey{
46+ {"main", KindPackage},
47+ {"Version", KindConst},
48+ {"Debug", KindVar},
49+ {"Server", KindStruct},
50+ {"Handler", KindInterface},
51+ }
52+
53+ got := make(map[symKey]bool)
54+ for _, s := range syms {
55+ got[symKey{s.Name, s.Kind}] = true
56+ }
57+
58+ for _, w := range want {
59+ if !got[w] {
60+ t.Errorf("missing symbol %q (kind=%v)", w.name, w.kind)
61+ }
62+ }
63+
64+ // Check functions/methods
65+ var hasMainFunc, hasStartMethod bool
66+ for _, s := range syms {
67+ if s.Kind == KindFunction && s.Name == "main" {
68+ hasMainFunc = true
69+ }
70+ if s.Kind == KindMethod && s.Name == "Start" && s.Receiver == "*Server" {
71+ hasStartMethod = true
72+ }
73+ }
74+ if !hasMainFunc {
75+ t.Error("missing func main")
76+ }
77+ if !hasStartMethod {
78+ t.Error("missing method Start on *Server")
79+ }
80+}
81+
82+func TestParseSource_Unsupported(t *testing.T) {
83+ syms, err := ParseSource("test.py", []byte("print('hello')"))
84+ if err != nil {
85+ t.Fatalf("unexpected error: %v", err)
86+ }
87+ if len(syms) != 0 {
88+ t.Errorf("expected no symbols for unsupported language, got %d", len(syms))
89+ }
90+}
91+
92+func TestFormatOutline(t *testing.T) {
93+ snap := map[string][]Symbol{
94+ "main.go": {
95+ {Name: "main", Kind: KindPackage},
96+ {Name: "main", Kind: KindFunction},
97+ {Name: "Server", Kind: KindStruct},
98+ {Name: "Start", Kind: KindMethod, Receiver: "*Server"},
99+ },
100+ }
101+ out := FormatOutline(snap)
102+ if out == "" {
103+ t.Fatal("empty outline")
104+ }
105+ // New compact format checks
106+ if !strings.Contains(out, "fn: main") {
107+ t.Errorf("missing fn: main in outline:\n%s", out)
108+ }
109+ if !strings.Contains(out, "Server (struct)") {
110+ t.Errorf("missing Server (struct) in outline:\n%s", out)
111+ }
112+ if !strings.Contains(out, "*Server: Start") {
113+ t.Errorf("missing *Server: Start in outline:\n%s", out)
114+ }
115+}
internal/treesitter/symbols.go+55−0
@@ -0,0 +1,55 @@
1+// Package treesitter provides tree-sitter based code parsing for structural
2+// symbol extraction. Used to build project-wide context for LLM awareness.
3+package treesitter
Cut short: this commit's diff is over 1000 lines.
internal/tui/golden_test.go+1−1
Cut short: this commit's diff is over 1000 lines.
internal/tui/tui.go+3−2
Cut short: this commit's diff is over 1000 lines.