Skip to content

Commit 9f358ff

Browse files
author
smulet
committed
feat: 支持解析外部依赖文件
- 新增默认解析配置 - 识别外部包目录 - 修复外部函数 FileLine
1 parent 7ef5337 commit 9f358ff

File tree

4 files changed

+43
-9
lines changed

4 files changed

+43
-9
lines changed

lang/golang/parser/ctx.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ func (p *GoParser) referCodes(ctx *fileContext, id *Identity, depth int) (err er
106106
continue
107107
}
108108
// println("search file", fpath)
109-
_, e = p.searchOnFile(file, pkg.Fset, bs, id.ModPath, pkg.ID, impts, id.Name)
109+
_, e = p.searchOnFile(file, pkg.Fset, bs, id.ModPath, pkg.ID, impts, id.Name, pkg.Dir)
110110
if e != nil {
111111
err = e
112112
continue

lang/golang/parser/option.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@ type Options struct {
2929
BuildFlags []string
3030
}
3131

32+
// DefaultOptions returns default parsing options
33+
func DefaultOptions() Options {
34+
return Options{
35+
ReferCodeDepth: 1, // Enable external dependency parsing by default
36+
}
37+
}
38+
3239
// type Option func(options *Options)
3340

3441
// func WithReferCodeDepth(depth int) Option {

lang/golang/parser/parser.go

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -527,7 +527,7 @@ func (p *GoParser) searchName(name string) (ids []Identity, err error) {
527527
if err != nil {
528528
return err
529529
}
530-
tids, e := p.searchOnFile(file, fset, fcontent, m.Name, pkg, impts, name)
530+
tids, e := p.searchOnFile(file, fset, fcontent, m.Name, pkg, impts, name, "")
531531
if e != nil {
532532
err = e
533533
return nil
@@ -553,7 +553,13 @@ func (p *GoParser) exportFileLine(fset *token.FileSet, decl ast.Node) (ret FileL
553553
return
554554
}
555555

556-
func (p *GoParser) searchOnFile(file *ast.File, fset *token.FileSet, fcontent []byte, mod string, pkg string, impt *importInfo, name string) (ids []Identity, err error) {
556+
func (p *GoParser) searchOnFile(file *ast.File, fset *token.FileSet, fcontent []byte, mod string, pkg string, impt *importInfo, name string, pkgDir string) (ids []Identity, err error) {
557+
isExternal := pkgDir != ""
558+
homeDir := p.homePageDir
559+
if isExternal {
560+
homeDir = pkgDir
561+
}
562+
557563
for _, decl := range file.Decls {
558564
// println(string(GetRawContent(fset, fcontent, decl)))
559565
switch decl := decl.(type) {
@@ -562,7 +568,7 @@ func (p *GoParser) searchOnFile(file *ast.File, fset *token.FileSet, fcontent []
562568
var receiver *Receiver
563569
if decl.Recv != nil && strings.Contains(name, ".") {
564570
var m = map[string]Identity{}
565-
tname, isPointer := p.mockTypes(decl.Recv.List[0].Type, m, fcontent, fset, getRelativeOrBasePath(p.homePageDir, fset, decl.Pos()), mod, pkg, impt)
571+
tname, isPointer := p.mockTypes(decl.Recv.List[0].Type, m, fcontent, fset, getRelativeOrBasePath(homeDir, fset, decl.Pos()), mod, pkg, impt)
566572
if tname == "" {
567573
fmt.Fprintf(os.Stderr, "Error: cannot get type from receiver %v", decl.Recv.List[0].Type)
568574
continue
@@ -584,7 +590,12 @@ func (p *GoParser) searchOnFile(file *ast.File, fset *token.FileSet, fcontent []
584590
ids = append(ids, newIdentity(mod, pkg, name))
585591
fn := p.newFunc(mod, pkg, name)
586592
fn.Content = string(GetRawContent(fset, fcontent, decl, p.opts.CollectComment))
587-
fn.FileLine = p.exportFileLine(fset, decl)
593+
if isExternal {
594+
fn.FileLine.File = fset.Position(decl.Pos()).Filename
595+
} else {
596+
fn.FileLine.File = getRelativeOrBasePath(homeDir, fset, decl.Pos())
597+
}
598+
fn.FileLine.Line = fset.Position(decl.Pos()).Line
588599
fn.IsMethod = decl.Recv != nil
589600
fn.Receiver = receiver
590601
// if decl.Type.Params != nil {
@@ -612,7 +623,12 @@ func (p *GoParser) searchOnFile(file *ast.File, fset *token.FileSet, fcontent []
612623
if spec.Name.Name == name {
613624
st = p.newType(mod, pkg, spec.Name.Name)
614625
st.Content = string(GetRawContent(fset, fcontent, spec, p.opts.CollectComment))
615-
st.FileLine = p.exportFileLine(fset, spec)
626+
if isExternal {
627+
st.FileLine.File = fset.Position(spec.Pos()).Filename
628+
} else {
629+
st.FileLine.File = getRelativeOrBasePath(homeDir, fset, spec.Pos())
630+
}
631+
st.FileLine.Line = fset.Position(spec.Pos()).Line
616632
st.TypeKind = getTypeKind(spec.Type)
617633
ids = append(ids, newIdentity(mod, pkg, name))
618634
}
@@ -628,7 +644,12 @@ func (p *GoParser) searchOnFile(file *ast.File, fset *token.FileSet, fcontent []
628644
ids = append(ids, newIdentity(mod, pkg, name))
629645
fn := p.newFunc(mod, pkg, name)
630646
fn.Content = string(GetRawContent(fset, fcontent, m, p.opts.CollectComment))
631-
fn.FileLine = p.exportFileLine(fset, m)
647+
if isExternal {
648+
fn.FileLine.File = fset.Position(m.Pos()).Filename
649+
} else {
650+
fn.FileLine.File = getRelativeOrBasePath(homeDir, fset, m.Pos())
651+
}
652+
fn.FileLine.Line = fset.Position(m.Pos()).Line
632653
fn.IsMethod = true
633654
fn.IsInterfaceMethod = true
634655
fn.Receiver = &Receiver{
@@ -659,7 +680,12 @@ func (p *GoParser) searchOnFile(file *ast.File, fset *token.FileSet, fcontent []
659680
ids = append(ids, newIdentity(mod, pkg, name))
660681
v := p.newVar(mod, pkg, name, decl.Tok == token.CONST)
661682
v.Content = string(GetRawContent(fset, fcontent, spec, p.opts.CollectComment))
662-
v.FileLine = p.exportFileLine(fset, spec)
683+
if isExternal {
684+
v.FileLine.File = fset.Position(spec.Pos()).Filename
685+
} else {
686+
v.FileLine.File = getRelativeOrBasePath(homeDir, fset, spec.Pos())
687+
}
688+
v.FileLine.Line = fset.Position(spec.Pos()).Line
663689
if spec.Type != nil {
664690
var m = map[string]Identity{}
665691
// NOTICE: collect all types

lang/parse.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,8 @@ func collectSymbol(ctx context.Context, cli *lsp.LSPClient, repoPath string, opt
200200
}
201201

202202
func callGoParser(ctx context.Context, repoPath string, opts collect.CollectOption) (*uniast.Repository, error) {
203-
goopts := parser.Options{}
203+
// Use default options (ReferCodeDepth: 1 by default)
204+
goopts := parser.DefaultOptions()
204205
if opts.LoadExternalSymbol {
205206
goopts.ReferCodeDepth = 1
206207
}

0 commit comments

Comments
 (0)