Skip to content

Commit

Permalink
gopls/internal/lsp/source: eliminate Snapshot.FileSet
Browse files Browse the repository at this point in the history
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 <gobot@golang.org>
gopls-CI: kokoro <noreply+kokoro@google.com>
Run-TryBot: Robert Findley <rfindley@google.com>
Reviewed-by: Alan Donovan <adonovan@google.com>
  • Loading branch information
findleyr committed Feb 14, 2023
1 parent 0bd0228 commit d4c13e6
Show file tree
Hide file tree
Showing 9 changed files with 10 additions and 18 deletions.
2 changes: 1 addition & 1 deletion gopls/internal/lsp/cache/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ func doTypeCheck(ctx context.Context, snapshot *snapshot, inputs typeCheckInputs
pkg := &syntaxPackage{
id: inputs.id,
mode: inputs.mode,
fset: snapshot.FileSet(), // must match parse call below (snapshot.ParseGo for now)
fset: snapshot.view.fset, // must match parse call below (snapshot.ParseGo for now)
types: types.NewPackage(string(inputs.pkgPath), string(inputs.name)),
importMap: new(importMap),
typesInfo: &types.Info{
Expand Down
2 changes: 1 addition & 1 deletion gopls/internal/lsp/cache/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func (s *snapshot) ParseGo(ctx context.Context, fh source.FileHandle, mode sourc
// cache miss?
if !hit {
promise, release := s.store.Promise(key, func(ctx context.Context, arg interface{}) interface{} {
parsed, err := parseGoImpl(ctx, arg.(*snapshot).FileSet(), fh, mode)
parsed, err := parseGoImpl(ctx, arg.(*snapshot).view.fset, fh, mode)
return parseGoResult{parsed, err}
})

Expand Down
4 changes: 0 additions & 4 deletions gopls/internal/lsp/cache/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,10 +261,6 @@ func (s *snapshot) BackgroundContext() context.Context {
return s.backgroundCtx
}

func (s *snapshot) FileSet() *token.FileSet {
return s.view.fset
}

func (s *snapshot) ModFiles() []span.URI {
var uris []span.URI
for modURI := range s.workspaceModFiles {
Expand Down
3 changes: 2 additions & 1 deletion gopls/internal/lsp/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -743,7 +743,8 @@ func (c *commandHandler) ListImports(ctx context.Context, args command.URIArg) (
if err != nil {
return err
}
for _, group := range astutil.Imports(deps.snapshot.FileSet(), pgf.File) {
fset := source.SingletonFileSet(pgf.Tok)
for _, group := range astutil.Imports(fset, pgf.File) {
for _, imp := range group {
if imp.Path == nil {
continue
Expand Down
3 changes: 3 additions & 0 deletions gopls/internal/lsp/source/fix.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ type (
// suggested fixes with their diagnostics, so we have to compute them
// separately. Such analyzers should provide a function with a signature of
// SuggestedFixFunc.
//
// The returned FileSet must map all token.Pos found in the suggested text
// edits.
SuggestedFixFunc func(ctx context.Context, snapshot Snapshot, fh FileHandle, pRng protocol.Range) (*token.FileSet, *analysis.SuggestedFix, error)
singleFileFixFunc func(fset *token.FileSet, start, end token.Pos, src []byte, file *ast.File, pkg *types.Package, info *types.Info) (*analysis.SuggestedFix, error)
)
Expand Down
2 changes: 1 addition & 1 deletion gopls/internal/lsp/source/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ func Format(ctx context.Context, snapshot Snapshot, fh FileHandle) ([]protocol.T
return nil, fmt.Errorf("can't format %q: file is generated", fh.URI().Filename())
}

fset := snapshot.FileSet()
pgf, err := snapshot.ParseGo(ctx, fh, ParseFull)
if err != nil {
return nil, err
Expand All @@ -54,6 +53,7 @@ func Format(ctx context.Context, snapshot Snapshot, fh FileHandle) ([]protocol.T
// This should be acceptable for all users, who likely be prompted to rebuild
// the LSP server on each Go release.
buf := &bytes.Buffer{}
fset := SingletonFileSet(pgf.Tok)
if err := format.Node(buf, fset, pgf.File); err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion gopls/internal/lsp/source/stub.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func stubSuggestedFixFunc(ctx context.Context, snapshot Snapshot, fh FileHandle,
NewText: []byte(edit.New),
})
}
return snapshot.FileSet(), // to match snapshot.ParseGo above
return SingletonFileSet(parsedConcreteFile.Tok), // edits use parsedConcreteFile.Tok
&analysis.SuggestedFix{TextEdits: edits},
nil
}
Expand Down
2 changes: 1 addition & 1 deletion gopls/internal/lsp/source/types_format.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ func (s *signature) Params() []string {
// NewBuiltinSignature returns signature for the builtin object with a given
// name, if a builtin object with the name exists.
func NewBuiltinSignature(ctx context.Context, s Snapshot, name string) (*signature, error) {
fset := s.FileSet()
builtin, err := s.BuiltinFile(ctx)
if err != nil {
return nil, err
Expand All @@ -111,6 +110,7 @@ func NewBuiltinSignature(ctx context.Context, s Snapshot, name string) (*signatu
variadic = true
}
}
fset := SingletonFileSet(builtin.Tok)
params, _ := formatFieldList(ctx, fset, decl.Type.Params, variadic)
results, needResultParens := formatFieldList(ctx, fset, decl.Type.Results, false)
d := decl.Doc.Text()
Expand Down
8 changes: 0 additions & 8 deletions gopls/internal/lsp/source/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,6 @@ type Snapshot interface {
// on behalf of this snapshot.
BackgroundContext() context.Context

// Fileset returns the Fileset used to parse all the Go files in this snapshot.
//
// If the files are known to belong to a specific Package, use
// Package.FileSet instead. (We plan to eliminate the
// Snapshot's cache of parsed files, and thus the need for a
// snapshot-wide FileSet.)
FileSet() *token.FileSet

// ValidBuildConfiguration returns true if there is some error in the
// user's workspace. In particular, if they are both outside of a module
// and their GOPATH.
Expand Down

0 comments on commit d4c13e6

Please sign in to comment.