Skip to content

Commit d4c13e6

Browse files
committed
gopls/internal/lsp/source: eliminate Snapshot.FileSet
Eliminate remaining uses of the global fileset API. For golang/go#57987 Change-Id: Ida5a7c556bf48d07a90966aa4f5623e64cc48378 Reviewed-on: https://go-review.googlesource.com/c/tools/+/468215 TryBot-Result: Gopher Robot <[email protected]> gopls-CI: kokoro <[email protected]> Run-TryBot: Robert Findley <[email protected]> Reviewed-by: Alan Donovan <[email protected]>
1 parent 0bd0228 commit d4c13e6

File tree

9 files changed

+10
-18
lines changed

9 files changed

+10
-18
lines changed

gopls/internal/lsp/cache/check.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,7 @@ func doTypeCheck(ctx context.Context, snapshot *snapshot, inputs typeCheckInputs
449449
pkg := &syntaxPackage{
450450
id: inputs.id,
451451
mode: inputs.mode,
452-
fset: snapshot.FileSet(), // must match parse call below (snapshot.ParseGo for now)
452+
fset: snapshot.view.fset, // must match parse call below (snapshot.ParseGo for now)
453453
types: types.NewPackage(string(inputs.pkgPath), string(inputs.name)),
454454
importMap: new(importMap),
455455
typesInfo: &types.Info{

gopls/internal/lsp/cache/parse.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func (s *snapshot) ParseGo(ctx context.Context, fh source.FileHandle, mode sourc
6464
// cache miss?
6565
if !hit {
6666
promise, release := s.store.Promise(key, func(ctx context.Context, arg interface{}) interface{} {
67-
parsed, err := parseGoImpl(ctx, arg.(*snapshot).FileSet(), fh, mode)
67+
parsed, err := parseGoImpl(ctx, arg.(*snapshot).view.fset, fh, mode)
6868
return parseGoResult{parsed, err}
6969
})
7070

gopls/internal/lsp/cache/snapshot.go

-4
Original file line numberDiff line numberDiff line change
@@ -261,10 +261,6 @@ func (s *snapshot) BackgroundContext() context.Context {
261261
return s.backgroundCtx
262262
}
263263

264-
func (s *snapshot) FileSet() *token.FileSet {
265-
return s.view.fset
266-
}
267-
268264
func (s *snapshot) ModFiles() []span.URI {
269265
var uris []span.URI
270266
for modURI := range s.workspaceModFiles {

gopls/internal/lsp/command.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -743,7 +743,8 @@ func (c *commandHandler) ListImports(ctx context.Context, args command.URIArg) (
743743
if err != nil {
744744
return err
745745
}
746-
for _, group := range astutil.Imports(deps.snapshot.FileSet(), pgf.File) {
746+
fset := source.SingletonFileSet(pgf.Tok)
747+
for _, group := range astutil.Imports(fset, pgf.File) {
747748
for _, imp := range group {
748749
if imp.Path == nil {
749750
continue

gopls/internal/lsp/source/fix.go

+3
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ type (
2626
// suggested fixes with their diagnostics, so we have to compute them
2727
// separately. Such analyzers should provide a function with a signature of
2828
// SuggestedFixFunc.
29+
//
30+
// The returned FileSet must map all token.Pos found in the suggested text
31+
// edits.
2932
SuggestedFixFunc func(ctx context.Context, snapshot Snapshot, fh FileHandle, pRng protocol.Range) (*token.FileSet, *analysis.SuggestedFix, error)
3033
singleFileFixFunc func(fset *token.FileSet, start, end token.Pos, src []byte, file *ast.File, pkg *types.Package, info *types.Info) (*analysis.SuggestedFix, error)
3134
)

gopls/internal/lsp/source/format.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ func Format(ctx context.Context, snapshot Snapshot, fh FileHandle) ([]protocol.T
3333
return nil, fmt.Errorf("can't format %q: file is generated", fh.URI().Filename())
3434
}
3535

36-
fset := snapshot.FileSet()
3736
pgf, err := snapshot.ParseGo(ctx, fh, ParseFull)
3837
if err != nil {
3938
return nil, err
@@ -54,6 +53,7 @@ func Format(ctx context.Context, snapshot Snapshot, fh FileHandle) ([]protocol.T
5453
// This should be acceptable for all users, who likely be prompted to rebuild
5554
// the LSP server on each Go release.
5655
buf := &bytes.Buffer{}
56+
fset := SingletonFileSet(pgf.Tok)
5757
if err := format.Node(buf, fset, pgf.File); err != nil {
5858
return nil, err
5959
}

gopls/internal/lsp/source/stub.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ func stubSuggestedFixFunc(ctx context.Context, snapshot Snapshot, fh FileHandle,
125125
NewText: []byte(edit.New),
126126
})
127127
}
128-
return snapshot.FileSet(), // to match snapshot.ParseGo above
128+
return SingletonFileSet(parsedConcreteFile.Tok), // edits use parsedConcreteFile.Tok
129129
&analysis.SuggestedFix{TextEdits: edits},
130130
nil
131131
}

gopls/internal/lsp/source/types_format.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@ func (s *signature) Params() []string {
8787
// NewBuiltinSignature returns signature for the builtin object with a given
8888
// name, if a builtin object with the name exists.
8989
func NewBuiltinSignature(ctx context.Context, s Snapshot, name string) (*signature, error) {
90-
fset := s.FileSet()
9190
builtin, err := s.BuiltinFile(ctx)
9291
if err != nil {
9392
return nil, err
@@ -111,6 +110,7 @@ func NewBuiltinSignature(ctx context.Context, s Snapshot, name string) (*signatu
111110
variadic = true
112111
}
113112
}
113+
fset := SingletonFileSet(builtin.Tok)
114114
params, _ := formatFieldList(ctx, fset, decl.Type.Params, variadic)
115115
results, needResultParens := formatFieldList(ctx, fset, decl.Type.Results, false)
116116
d := decl.Doc.Text()

gopls/internal/lsp/source/view.go

-8
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,6 @@ type Snapshot interface {
6262
// on behalf of this snapshot.
6363
BackgroundContext() context.Context
6464

65-
// Fileset returns the Fileset used to parse all the Go files in this snapshot.
66-
//
67-
// If the files are known to belong to a specific Package, use
68-
// Package.FileSet instead. (We plan to eliminate the
69-
// Snapshot's cache of parsed files, and thus the need for a
70-
// snapshot-wide FileSet.)
71-
FileSet() *token.FileSet
72-
7365
// ValidBuildConfiguration returns true if there is some error in the
7466
// user's workspace. In particular, if they are both outside of a module
7567
// and their GOPATH.

0 commit comments

Comments
 (0)