Skip to content

Commit 5f3e7a3

Browse files
author
simuleite
committed
Add UniAST Index for search_symbol & get_file_structure
1 parent 94ff0ec commit 5f3e7a3

3 files changed

Lines changed: 93 additions & 1 deletion

File tree

lang/golang/parser/parser.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,7 @@ func (p *GoParser) ParseRepo() (Repository, error) {
295295
}
296296
p.associateStructWithMethods()
297297
p.associateImplements()
298+
p.buildNameToLocations()
298299
fmt.Fprintf(os.Stderr, "total call packages.Load %d times\n", loadCount)
299300
return p.getRepo(), nil
300301
}
@@ -362,6 +363,74 @@ func (p *GoParser) getRepo() Repository {
362363
return p.repo
363364
}
364365

366+
// buildNameToLocations 构建 name → files 反向索引
367+
// 解析完成后调用,一次遍历 Package 填充
368+
func (p *GoParser) buildNameToLocations() {
369+
if p.repo.NameToLocations == nil {
370+
p.repo.NameToLocations = make(map[string]NameLocations)
371+
}
372+
373+
for _, mod := range p.repo.Modules {
374+
for _, pkg := range mod.Packages {
375+
// Functions
376+
for name, fn := range pkg.Functions {
377+
if fn.File == "" {
378+
continue
379+
}
380+
loc := p.repo.NameToLocations[name]
381+
// 去重
382+
exists := false
383+
for _, f := range loc.Files {
384+
if f == fn.File {
385+
exists = true
386+
break
387+
}
388+
}
389+
if !exists {
390+
loc.Files = append(loc.Files, fn.File)
391+
}
392+
p.repo.NameToLocations[name] = loc
393+
}
394+
// Types
395+
for name, t := range pkg.Types {
396+
if t.FileLine.File == "" {
397+
continue
398+
}
399+
loc := p.repo.NameToLocations[name]
400+
exists := false
401+
for _, f := range loc.Files {
402+
if f == t.FileLine.File {
403+
exists = true
404+
break
405+
}
406+
}
407+
if !exists {
408+
loc.Files = append(loc.Files, t.FileLine.File)
409+
}
410+
p.repo.NameToLocations[name] = loc
411+
}
412+
// Vars
413+
for name, v := range pkg.Vars {
414+
if v.FileLine.File == "" {
415+
continue
416+
}
417+
loc := p.repo.NameToLocations[name]
418+
exists := false
419+
for _, f := range loc.Files {
420+
if f == v.FileLine.File {
421+
exists = true
422+
break
423+
}
424+
}
425+
if !exists {
426+
loc.Files = append(loc.Files, v.FileLine.File)
427+
}
428+
p.repo.NameToLocations[name] = loc
429+
}
430+
}
431+
}
432+
}
433+
365434
// ToABS converts a local package path to absolute path
366435
// If the path is not a local package, return empty string
367436
// func (p *goParser) pkgPathToABS(path PkgPath) string {

lang/golang/parser/pkg.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,9 @@ func (p *GoParser) loadPackages(mod *Module, dir string, pkgPath PkgPath) (err e
276276
if f.Package == "" {
277277
f.Package = pkg.ID
278278
f.Imports = imports.Origins
279+
// [新增] 填充 ModPath 和 PkgPath
280+
f.ModPath = mod.Name
281+
f.PkgPath = pkg.ID
279282
}
280283
if err := p.parseFile(ctx, file); err != nil {
281284
return err

lang/uniast/ast.go

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,16 @@ type Repository struct {
9595
Path string // repo absolute path
9696
Modules map[string]*Module // module name => module
9797
Graph NodeGraph // node id => node
98+
99+
// [新增] name → files 反向索引
100+
// 加速 search_symbol API,无需独立 .idx 文件
101+
NameToLocations map[string]NameLocations `json:"NameToLocations,omitempty"`
102+
}
103+
104+
// NameLocations represents all locations of a symbol name
105+
// [新增] 用于反向索引 name → files
106+
type NameLocations struct {
107+
Files []string `json:"Files,omitempty"`
98108
}
99109

100110
func (r Repository) ID() string {
@@ -126,7 +136,14 @@ func NewRepository(name string) Repository {
126136
type File struct {
127137
Path string
128138
Imports []Import `json:",omitempty"`
129-
Package PkgPath `json:",omitempty"`
139+
140+
// Package 兼容旧字段
141+
Package PkgPath `json:",omitempty"`
142+
143+
// [新增] Identity fields for O(1) lookup
144+
// 解析时直接赋值,无需查询
145+
ModPath ModPath `json:"ModPath,omitempty"`
146+
PkgPath PkgPath `json:"PkgPath,omitempty"`
130147
}
131148

132149
type Import struct {
@@ -569,6 +586,9 @@ type FileLine struct {
569586
// NOTICE: start line. line number start from 1
570587
Line int
571588

589+
// [新增] end line number (1-based)
590+
EndLine int `json:"EndLine,omitempty"`
591+
572592
// start offset in file
573593
StartOffset int
574594

0 commit comments

Comments
 (0)