Skip to content

Commit 5e47a3d

Browse files
committed
gopls/internal/cache: move Snapshot.Symbols to symbols.go
Pure code movement; no behavior change. Change-Id: I7ef073d58355f9ac8853cc0c1e33e0af3f9a391f Reviewed-on: https://go-review.googlesource.com/c/tools/+/634795 Reviewed-by: Peter Weinberger <pjw@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
1 parent 3689f0b commit 5e47a3d

File tree

2 files changed

+64
-62
lines changed

2 files changed

+64
-62
lines changed

gopls/internal/cache/snapshot.go

-62
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,12 @@ import (
1717
"path"
1818
"path/filepath"
1919
"regexp"
20-
"runtime"
2120
"slices"
2221
"sort"
2322
"strconv"
2423
"strings"
2524
"sync"
2625

27-
"golang.org/x/sync/errgroup"
2826
"golang.org/x/tools/go/types/objectpath"
2927
"golang.org/x/tools/gopls/internal/cache/metadata"
3028
"golang.org/x/tools/gopls/internal/cache/methodsets"
@@ -961,66 +959,6 @@ func (s *Snapshot) isWorkspacePackage(id PackageID) bool {
961959
return ok
962960
}
963961

964-
// Symbols extracts and returns symbol information for every file contained in
965-
// a loaded package. It awaits snapshot loading.
966-
//
967-
// If workspaceOnly is set, this only includes symbols from files in a
968-
// workspace package. Otherwise, it returns symbols from all loaded packages.
969-
//
970-
// TODO(rfindley): move to symbols.go.
971-
func (s *Snapshot) Symbols(ctx context.Context, workspaceOnly bool) (map[protocol.DocumentURI][]Symbol, error) {
972-
var (
973-
meta []*metadata.Package
974-
err error
975-
)
976-
if workspaceOnly {
977-
meta, err = s.WorkspaceMetadata(ctx)
978-
} else {
979-
meta, err = s.AllMetadata(ctx)
980-
}
981-
if err != nil {
982-
return nil, fmt.Errorf("loading metadata: %v", err)
983-
}
984-
985-
goFiles := make(map[protocol.DocumentURI]struct{})
986-
for _, mp := range meta {
987-
for _, uri := range mp.GoFiles {
988-
goFiles[uri] = struct{}{}
989-
}
990-
for _, uri := range mp.CompiledGoFiles {
991-
goFiles[uri] = struct{}{}
992-
}
993-
}
994-
995-
// Symbolize them in parallel.
996-
var (
997-
group errgroup.Group
998-
nprocs = 2 * runtime.GOMAXPROCS(-1) // symbolize is a mix of I/O and CPU
999-
resultMu sync.Mutex
1000-
result = make(map[protocol.DocumentURI][]Symbol)
1001-
)
1002-
group.SetLimit(nprocs)
1003-
for uri := range goFiles {
1004-
uri := uri
1005-
group.Go(func() error {
1006-
symbols, err := s.symbolize(ctx, uri)
1007-
if err != nil {
1008-
return err
1009-
}
1010-
resultMu.Lock()
1011-
result[uri] = symbols
1012-
resultMu.Unlock()
1013-
return nil
1014-
})
1015-
}
1016-
// Keep going on errors, but log the first failure.
1017-
// Partial results are better than no symbol results.
1018-
if err := group.Wait(); err != nil {
1019-
event.Error(ctx, "getting snapshot symbols", err)
1020-
}
1021-
return result, nil
1022-
}
1023-
1024962
// AllMetadata returns a new unordered array of metadata for
1025963
// all packages known to this snapshot, which includes the
1026964
// packages of all workspace modules plus their transitive

gopls/internal/cache/symbols.go

+64
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,21 @@ package cache
66

77
import (
88
"context"
9+
"fmt"
910
"go/ast"
1011
"go/token"
1112
"go/types"
13+
"runtime"
1214
"strings"
15+
"sync"
1316

17+
"golang.org/x/sync/errgroup"
18+
"golang.org/x/tools/gopls/internal/cache/metadata"
1419
"golang.org/x/tools/gopls/internal/cache/parsego"
1520
"golang.org/x/tools/gopls/internal/file"
1621
"golang.org/x/tools/gopls/internal/protocol"
1722
"golang.org/x/tools/gopls/internal/util/astutil"
23+
"golang.org/x/tools/internal/event"
1824
)
1925

2026
// Symbol holds a precomputed symbol value. Note: we avoid using the
@@ -26,6 +32,64 @@ type Symbol struct {
2632
Range protocol.Range
2733
}
2834

35+
// Symbols extracts and returns symbol information for every file contained in
36+
// a loaded package. It awaits snapshot loading.
37+
//
38+
// If workspaceOnly is set, this only includes symbols from files in a
39+
// workspace package. Otherwise, it returns symbols from all loaded packages.
40+
func (s *Snapshot) Symbols(ctx context.Context, workspaceOnly bool) (map[protocol.DocumentURI][]Symbol, error) {
41+
var (
42+
meta []*metadata.Package
43+
err error
44+
)
45+
if workspaceOnly {
46+
meta, err = s.WorkspaceMetadata(ctx)
47+
} else {
48+
meta, err = s.AllMetadata(ctx)
49+
}
50+
if err != nil {
51+
return nil, fmt.Errorf("loading metadata: %v", err)
52+
}
53+
54+
goFiles := make(map[protocol.DocumentURI]struct{})
55+
for _, mp := range meta {
56+
for _, uri := range mp.GoFiles {
57+
goFiles[uri] = struct{}{}
58+
}
59+
for _, uri := range mp.CompiledGoFiles {
60+
goFiles[uri] = struct{}{}
61+
}
62+
}
63+
64+
// Symbolize them in parallel.
65+
var (
66+
group errgroup.Group
67+
nprocs = 2 * runtime.GOMAXPROCS(-1) // symbolize is a mix of I/O and CPU
68+
resultMu sync.Mutex
69+
result = make(map[protocol.DocumentURI][]Symbol)
70+
)
71+
group.SetLimit(nprocs)
72+
for uri := range goFiles {
73+
uri := uri
74+
group.Go(func() error {
75+
symbols, err := s.symbolize(ctx, uri)
76+
if err != nil {
77+
return err
78+
}
79+
resultMu.Lock()
80+
result[uri] = symbols
81+
resultMu.Unlock()
82+
return nil
83+
})
84+
}
85+
// Keep going on errors, but log the first failure.
86+
// Partial results are better than no symbol results.
87+
if err := group.Wait(); err != nil {
88+
event.Error(ctx, "getting snapshot symbols", err)
89+
}
90+
return result, nil
91+
}
92+
2993
// symbolize returns the result of symbolizing the file identified by uri, using a cache.
3094
func (s *Snapshot) symbolize(ctx context.Context, uri protocol.DocumentURI) ([]Symbol, error) {
3195

0 commit comments

Comments
 (0)