Skip to content

Commit cd28922

Browse files
committed
fix: fix typo in scanner
1 parent 00b1fe8 commit cd28922

File tree

2 files changed

+21
-15
lines changed

2 files changed

+21
-15
lines changed

internal/pkgindex/scanner.go

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,15 @@ import (
1616
)
1717

1818
const (
19-
// 1080 is a number of sub-directories in $GOROOT/src for Go 1.22.
20-
resultsPreallocSize = 1080
19+
resultsPreallocSize = 300
2120
)
2221

22+
type scanResult struct {
23+
hasPkg bool
24+
item monaco.CompletionItem
25+
children []string
26+
}
27+
2328
// GoRootScanner scans Go SDK directory and provides information about Go version and standard packages list.
2429
type GoRootScanner struct {
2530
goRoot string
@@ -68,9 +73,6 @@ func (s *GoRootScanner) start() ([]monaco.CompletionItem, error) {
6873
}
6974

7075
q.add(pkgName)
71-
// go s.wg.Go(func() error {
72-
// return s.visitPackage(childCtx, rootDir, pkgName)
73-
// })
7476
}
7577

7678
results := make([]monaco.CompletionItem, 0, resultsPreallocSize)
@@ -80,22 +82,24 @@ func (s *GoRootScanner) start() ([]monaco.CompletionItem, error) {
8082
break
8183
}
8284

83-
item, nextPkgs, err := s.visitPackage(rootDir, pkgName)
85+
result, err := s.visitPackage(rootDir, pkgName)
8486
if err != nil {
8587
return nil, err
8688
}
8789

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+
}
9094
}
9195

9296
return results, nil
9397
}
9498

95-
func (s *GoRootScanner) visitPackage(rootDir string, importPath string) (monaco.CompletionItem, []string, error) {
99+
func (s *GoRootScanner) visitPackage(rootDir string, importPath string) (scanResult, error) {
96100
entries, err := os.ReadDir(filepath.Join(rootDir, importPath))
97101
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)
99103
}
100104

101105
var nextPkgs []string
@@ -118,7 +122,7 @@ func (s *GoRootScanner) visitPackage(rootDir string, importPath string) (monaco.
118122
}
119123

120124
if len(sourceFiles) == 0 {
121-
return monaco.CompletionItem{}, nextPkgs, nil
125+
return scanResult{children: nextPkgs}, nil
122126
}
123127

124128
item, err := ParseImportCompletionItem(context.Background(), PackageParseParams{
@@ -127,10 +131,14 @@ func (s *GoRootScanner) visitPackage(rootDir string, importPath string) (monaco.
127131
Files: sourceFiles,
128132
})
129133
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)
131135
}
132136

133-
return item, nextPkgs, nil
137+
return scanResult{
138+
hasPkg: true,
139+
item: item,
140+
children: nextPkgs,
141+
}, nil
134142
}
135143

136144
func checkVersion(root string) (string, error) {

tools/pkgindexer/main.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"log"
88
"os"
99
"path/filepath"
10-
"runtime"
1110

1211
"github.com/spf13/cobra"
1312
"github.com/x1unix/go-playground/internal/pkgindex"
@@ -62,7 +61,6 @@ func main() {
6261
}
6362

6463
func runErr(flags Flags) error {
65-
runtime.GOMAXPROCS(1)
6664
scanner := pkgindex.NewGoRootScanner(flags.goRoot)
6765
results, err := scanner.Scan()
6866
if err != nil {

0 commit comments

Comments
 (0)