xonecas.io
62159
Sy

symb / files

symb / internal / treesitter / index.go

2.6 KB
1package treesitter
2
3import (
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.
12type 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.
19func 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.
28func (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.
80func (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).
98func (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.
109func (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.
116func (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}