@@ -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
441468func (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}
0 commit comments