@@ -16,10 +16,15 @@ import (
16
16
)
17
17
18
18
const (
19
- // 1080 is a number of sub-directories in $GOROOT/src for Go 1.22.
20
- resultsPreallocSize = 1080
19
+ resultsPreallocSize = 300
21
20
)
22
21
22
+ type scanResult struct {
23
+ hasPkg bool
24
+ item monaco.CompletionItem
25
+ children []string
26
+ }
27
+
23
28
// GoRootScanner scans Go SDK directory and provides information about Go version and standard packages list.
24
29
type GoRootScanner struct {
25
30
goRoot string
@@ -68,9 +73,6 @@ func (s *GoRootScanner) start() ([]monaco.CompletionItem, error) {
68
73
}
69
74
70
75
q .add (pkgName )
71
- // go s.wg.Go(func() error {
72
- // return s.visitPackage(childCtx, rootDir, pkgName)
73
- // })
74
76
}
75
77
76
78
results := make ([]monaco.CompletionItem , 0 , resultsPreallocSize )
@@ -80,22 +82,24 @@ func (s *GoRootScanner) start() ([]monaco.CompletionItem, error) {
80
82
break
81
83
}
82
84
83
- item , nextPkgs , err := s .visitPackage (rootDir , pkgName )
85
+ result , err := s .visitPackage (rootDir , pkgName )
84
86
if err != nil {
85
87
return nil , err
86
88
}
87
89
88
- q .add (nextPkgs ... )
89
- results = append (results , item )
90
+ q .add (result .children ... )
91
+ if result .hasPkg {
92
+ results = append (results , result .item )
93
+ }
90
94
}
91
95
92
96
return results , nil
93
97
}
94
98
95
- func (s * GoRootScanner ) visitPackage (rootDir string , importPath string ) (monaco. CompletionItem , [] string , error ) {
99
+ func (s * GoRootScanner ) visitPackage (rootDir string , importPath string ) (scanResult , error ) {
96
100
entries , err := os .ReadDir (filepath .Join (rootDir , importPath ))
97
101
if err != nil {
98
- return monaco. CompletionItem {}, nil , fmt .Errorf ("failed to open package directory %q: %w" , importPath , err )
102
+ return scanResult {} , fmt .Errorf ("failed to open package directory %q: %w" , importPath , err )
99
103
}
100
104
101
105
var nextPkgs []string
@@ -118,7 +122,7 @@ func (s *GoRootScanner) visitPackage(rootDir string, importPath string) (monaco.
118
122
}
119
123
120
124
if len (sourceFiles ) == 0 {
121
- return monaco. CompletionItem {}, nextPkgs , nil
125
+ return scanResult { children : nextPkgs } , nil
122
126
}
123
127
124
128
item , err := ParseImportCompletionItem (context .Background (), PackageParseParams {
@@ -127,10 +131,14 @@ func (s *GoRootScanner) visitPackage(rootDir string, importPath string) (monaco.
127
131
Files : sourceFiles ,
128
132
})
129
133
if err != nil {
130
- return item , nil , fmt .Errorf ("failed to parse package %q: %w" , importPath , err )
134
+ return scanResult {} , fmt .Errorf ("failed to parse package %q: %w" , importPath , err )
131
135
}
132
136
133
- return item , nextPkgs , nil
137
+ return scanResult {
138
+ hasPkg : true ,
139
+ item : item ,
140
+ children : nextPkgs ,
141
+ }, nil
134
142
}
135
143
136
144
func checkVersion (root string ) (string , error ) {
0 commit comments