Skip to content

Commit 647bec9

Browse files
authored
opt:(go) add option LoadByPackages to avoid OOM and skip exludes (#26)
1 parent 219a729 commit 647bec9

File tree

6 files changed

+44
-11
lines changed

6 files changed

+44
-11
lines changed

lang/collect/collect.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ type CollectOption struct {
3636
NoNeedComment bool
3737
NeedTest bool
3838
Excludes []string
39+
LoadByPackages bool
3940
}
4041

4142
type Collector struct {

lang/golang/parser/option.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ type Options struct {
2525
Excludes []string
2626
CollectComment bool
2727
NeedTest bool
28+
LoadByPackages bool
2829
}
2930

3031
// type Option func(options *Options)

lang/golang/parser/parser.go

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,29 @@ func (p *GoParser) ParseModule(mod *Module, dir string) (err error) {
164164
return nil
165165
})
166166

167-
return p.loadPackages(mod, dir, "./...")
167+
if p.opts.LoadByPackages {
168+
var errs []error
169+
filepath.Walk(dir, func(path string, info fs.FileInfo, e error) error {
170+
if e != nil || !info.IsDir() || shouldIgnoreDir(path) {
171+
return nil
172+
}
173+
for _, exclude := range p.exclues {
174+
if exclude.MatchString(path) {
175+
return nil
176+
}
177+
}
178+
if err := p.parsePackage(p.pkgPathFromABS(path)); err != nil {
179+
errs = append(errs, err)
180+
}
181+
return nil
182+
})
183+
if len(errs) > 0 {
184+
return fmt.Errorf("parse package failed: %v", errs)
185+
}
186+
return nil
187+
} else {
188+
return p.loadPackages(mod, dir, "./...")
189+
}
168190
}
169191

170192
// getRepo return currently parsed golang AST
@@ -249,7 +271,7 @@ func (p *GoParser) searchName(name string) (ids []Identity, err error) {
249271
if e != nil || info.IsDir() || shouldIgnoreFile(path) || shouldIgnoreDir(filepath.Dir(path)) || !strings.HasSuffix(path, ".go") {
250272
return nil
251273
}
252-
mod, _ := p.getModuleFromPath(filepath.Dir(path))
274+
mod := p.pkgPathFromABS(path)
253275
m := p.repo.Modules[mod]
254276
if m == nil {
255277
dir, _ := filepath.Rel(p.homePageDir, path)
@@ -428,24 +450,29 @@ func (p *GoParser) getModuleFromPkg(pkg PkgPath) (name string, dir string) {
428450
}
429451

430452
// path is absolute path
431-
func (p *GoParser) getModuleFromPath(path string) (name string, dir string) {
453+
func (p *GoParser) getModuleFromPath(path string) (name string, dir string, rel string) {
432454
for _, m := range p.modules {
433-
if len(m.dir) != 0 && strings.HasPrefix(path, m.dir) {
434-
return m.name, m.dir
455+
if m.dir == "" {
456+
continue
457+
}
458+
dir := filepath.Join(p.homePageDir, m.dir)
459+
if strings.HasPrefix(path, dir) {
460+
rel, _ = filepath.Rel(dir, path)
461+
return m.name, m.dir, rel
435462
}
436463
}
437-
return "", ""
464+
return "", "", ""
438465
}
439466

440467
// FromABS converts an absolute path to local mod path
441468
func (p *GoParser) pkgPathFromABS(path string) PkgPath {
442-
mod, dir := p.getModuleFromPath(path)
469+
mod, _, rel := p.getModuleFromPath(path)
443470
if mod == "" {
444471
panic("not found package from " + path)
445472
}
446-
if rel, err := filepath.Rel(dir, path); err != nil {
447-
panic("path " + path + " is not relative from mod path " + dir)
473+
if rel != "" && rel != "." {
474+
return mod + "/" + rel
448475
} else {
449-
return filepath.Join(mod, rel)
476+
return mod
450477
}
451478
}

lang/golang/parser/pkg.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ func (p *GoParser) parsePackage(pkgPath PkgPath) (err error) {
142142
}
143143
// fmt.Println("[parsePackage] mod:", mod, "dir:", dir, "pkgPath:", pkgPath, p.opts.ReferCodeDepth)
144144

145-
return p.loadPackages(lib, dir, pkgPath)
145+
return p.loadPackages(lib, filepath.Join(p.homePageDir, lib.Dir), pkgPath)
146146
}
147147

148148
var loadCount = 0

lang/parse.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,9 @@ func callGoParser(ctx context.Context, repoPath string, opts collect.CollectOpti
185185
if opts.NeedTest {
186186
goopts.NeedTest = true
187187
}
188+
if opts.LoadByPackages {
189+
goopts.LoadByPackages = true
190+
}
188191
goopts.Excludes = opts.Excludes
189192
p := parser.NewParser(repoPath, repoPath, goopts)
190193
repo, err := p.ParseRepo()

main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ func main() {
6767
flags.BoolVar(&opts.LoadExternalSymbol, "load-external-symbol", false, "load external symbols into results")
6868
flags.BoolVar(&opts.NoNeedComment, "no-need-comment", false, "do not need comment (only works for Go now)")
6969
flags.BoolVar(&opts.NeedTest, "need-test", false, "need parse test files (only works for Go now)")
70+
flags.BoolVar(&opts.LoadByPackages, "load-by-packages", false, "load by packages (only works for Go now)")
7071
flags.Var((*StringArray)(&opts.Excludes), "exclude", "exclude files or directories, support multiple values")
7172
flags.StringVar(&opts.RepoID, "repo-id", "", "specify the repo id")
7273
flagLsp := flags.String("lsp", "", "Specify the language server path.")

0 commit comments

Comments
 (0)