From 0a3dcccdcf7ac1c72c5b03aa5c6bf52603cf5e25 Mon Sep 17 00:00:00 2001 From: Rob Findley Date: Mon, 19 Oct 2020 10:31:43 -0400 Subject: [PATCH] internal/lsp/source: add a FileSource interface Rename Snapshot.GetFile to GetVersionedFile, and make the signature of GetFile consistent with the corresponding method on session and cache. This allows algorithms that depend only on file state to be expressed using this API. In a subsequent CL, this is used for building and testing the workspace module. Preeemptively add the FileSource interface for use in these algorithms. Change-Id: I550906e554fd290dcdf4cac442d5f223e0f644c1 Reviewed-on: https://go-review.googlesource.com/c/tools/+/263522 Run-TryBot: Robert Findley gopls-CI: kokoro Trust: Robert Findley TryBot-Result: Go Bot Reviewed-by: Rebecca Stambler --- internal/lsp/cache/snapshot.go | 11 ++++++++--- internal/lsp/code_action.go | 4 ++-- internal/lsp/diagnostics.go | 2 +- internal/lsp/general.go | 2 +- internal/lsp/lsp_test.go | 4 ++-- internal/lsp/mod/diagnostics.go | 4 ++-- internal/lsp/rename.go | 2 +- internal/lsp/source/diagnostics.go | 2 +- internal/lsp/source/view.go | 18 +++++++++++++++--- 9 files changed, 33 insertions(+), 16 deletions(-) diff --git a/internal/lsp/cache/snapshot.go b/internal/lsp/cache/snapshot.go index c96ad69bb1..d141c519e9 100644 --- a/internal/lsp/cache/snapshot.go +++ b/internal/lsp/cache/snapshot.go @@ -791,12 +791,12 @@ func (s *snapshot) FindFile(uri span.URI) source.VersionedFileHandle { return s.files[f.URI()] } -// GetFile returns a File for the given URI. If the file is unknown it is added -// to the managed set. +// GetVersionedFile returns a File for the given URI. If the file is unknown it +// is added to the managed set. // // GetFile succeeds even if the file does not exist. A non-nil error return // indicates some type of internal error, for example if ctx is cancelled. -func (s *snapshot) GetFile(ctx context.Context, uri span.URI) (source.VersionedFileHandle, error) { +func (s *snapshot) GetVersionedFile(ctx context.Context, uri span.URI) (source.VersionedFileHandle, error) { f, err := s.view.getFile(uri) if err != nil { return nil, err @@ -807,6 +807,11 @@ func (s *snapshot) GetFile(ctx context.Context, uri span.URI) (source.VersionedF return s.getFileLocked(ctx, f) } +// GetFile implements the fileSource interface by wrapping GetVersionedFile. +func (s *snapshot) GetFile(ctx context.Context, uri span.URI) (source.FileHandle, error) { + return s.GetVersionedFile(ctx, uri) +} + func (s *snapshot) getFileLocked(ctx context.Context, f *fileBase) (source.VersionedFileHandle, error) { if fh, ok := s.files[f.URI()]; ok { return fh, nil diff --git a/internal/lsp/code_action.go b/internal/lsp/code_action.go index 20e9f99fb4..cb45045788 100644 --- a/internal/lsp/code_action.go +++ b/internal/lsp/code_action.go @@ -282,7 +282,7 @@ func analysisFixes(ctx context.Context, snapshot source.Snapshot, pkg source.Pac Edit: protocol.WorkspaceEdit{}, } for uri, edits := range fix.Edits { - fh, err := snapshot.GetFile(ctx, uri) + fh, err := snapshot.GetVersionedFile(ctx, uri) if err != nil { return nil, nil, err } @@ -481,7 +481,7 @@ func moduleQuickFixes(ctx context.Context, snapshot source.Snapshot, fh source.V return nil, nil } var err error - modFH, err = snapshot.GetFile(ctx, modURI) + modFH, err = snapshot.GetVersionedFile(ctx, modURI) if err != nil { return nil, err } diff --git a/internal/lsp/diagnostics.go b/internal/lsp/diagnostics.go index f3e59c6dcb..7469a366fe 100644 --- a/internal/lsp/diagnostics.go +++ b/internal/lsp/diagnostics.go @@ -337,7 +337,7 @@ func errorsToDiagnostic(ctx context.Context, snapshot source.Snapshot, errors [] Severity: protocol.SeverityError, Source: e.Category, } - fh, err := snapshot.GetFile(ctx, e.URI) + fh, err := snapshot.GetVersionedFile(ctx, e.URI) if err != nil { return err } diff --git a/internal/lsp/general.go b/internal/lsp/general.go index ff76d58dac..307660babf 100644 --- a/internal/lsp/general.go +++ b/internal/lsp/general.go @@ -470,7 +470,7 @@ func (s *Server) beginFileRequest(ctx context.Context, pURI protocol.DocumentURI return nil, nil, false, func() {}, err } snapshot, release := view.Snapshot(ctx) - fh, err := snapshot.GetFile(ctx, uri) + fh, err := snapshot.GetVersionedFile(ctx, uri) if err != nil { release() return nil, nil, false, func() {}, err diff --git a/internal/lsp/lsp_test.go b/internal/lsp/lsp_test.go index b24b4362b2..d94b8f08f9 100644 --- a/internal/lsp/lsp_test.go +++ b/internal/lsp/lsp_test.go @@ -467,7 +467,7 @@ func (r *runner) SuggestedFix(t *testing.T, spn span.Span, actionKinds []string) snapshot, release := view.Snapshot(r.ctx) defer release() - fh, err := snapshot.GetFile(r.ctx, uri) + fh, err := snapshot.GetVersionedFile(r.ctx, uri) if err != nil { t.Fatal(err) } @@ -577,7 +577,7 @@ func (r *runner) FunctionExtraction(t *testing.T, start span.Span, end span.Span snapshot, release := view.Snapshot(r.ctx) defer release() - fh, err := snapshot.GetFile(r.ctx, uri) + fh, err := snapshot.GetVersionedFile(r.ctx, uri) if err != nil { t.Fatal(err) } diff --git a/internal/lsp/mod/diagnostics.go b/internal/lsp/mod/diagnostics.go index e8c453318c..8904ba2fe1 100644 --- a/internal/lsp/mod/diagnostics.go +++ b/internal/lsp/mod/diagnostics.go @@ -21,7 +21,7 @@ func Diagnostics(ctx context.Context, snapshot source.Snapshot) (map[source.Vers reports := map[source.VersionedFileIdentity][]*source.Diagnostic{} for _, uri := range snapshot.ModFiles() { - fh, err := snapshot.GetFile(ctx, uri) + fh, err := snapshot.GetVersionedFile(ctx, uri) if err != nil { return nil, err } @@ -44,7 +44,7 @@ func Diagnostics(ctx context.Context, snapshot source.Snapshot) (map[source.Vers } else { diag.Severity = protocol.SeverityWarning } - fh, err := snapshot.GetFile(ctx, e.URI) + fh, err := snapshot.GetVersionedFile(ctx, e.URI) if err != nil { return nil, err } diff --git a/internal/lsp/rename.go b/internal/lsp/rename.go index cb364a15a6..cef0638ff5 100644 --- a/internal/lsp/rename.go +++ b/internal/lsp/rename.go @@ -24,7 +24,7 @@ func (s *Server) rename(ctx context.Context, params *protocol.RenameParams) (*pr var docChanges []protocol.TextDocumentEdit for uri, e := range edits { - fh, err := snapshot.GetFile(ctx, uri) + fh, err := snapshot.GetVersionedFile(ctx, uri) if err != nil { return nil, err } diff --git a/internal/lsp/source/diagnostics.go b/internal/lsp/source/diagnostics.go index c1f5826d9b..2f0e7d624f 100644 --- a/internal/lsp/source/diagnostics.go +++ b/internal/lsp/source/diagnostics.go @@ -128,7 +128,7 @@ func pickAnalyzers(snapshot Snapshot, hadTypeErrors bool) map[string]Analyzer { } func FileDiagnostics(ctx context.Context, snapshot Snapshot, uri span.URI) (VersionedFileIdentity, []*Diagnostic, error) { - fh, err := snapshot.GetFile(ctx, uri) + fh, err := snapshot.GetVersionedFile(ctx, uri) if err != nil { return VersionedFileIdentity{}, nil, err } diff --git a/internal/lsp/source/view.go b/internal/lsp/source/view.go index 291dfe3316..c84ff065e7 100644 --- a/internal/lsp/source/view.go +++ b/internal/lsp/source/view.go @@ -44,9 +44,13 @@ type Snapshot interface { // in the given snapshot. FindFile(uri span.URI) VersionedFileHandle - // GetFile returns the FileHandle for a given URI, initializing it - // if it is not already part of the snapshot. - GetFile(ctx context.Context, uri span.URI) (VersionedFileHandle, error) + // GetVersionedFile returns the VersionedFileHandle for a given URI, + // initializing it if it is not already part of the snapshot. + GetVersionedFile(ctx context.Context, uri span.URI) (VersionedFileHandle, error) + + // GetFile returns the FileHandle for a given URI, initializing it if it is + // not already part of the snapshot. + GetFile(ctx context.Context, uri span.URI) (FileHandle, error) // AwaitInitialized waits until the snapshot's view is initialized. AwaitInitialized(ctx context.Context) @@ -203,6 +207,14 @@ type View interface { IsGoPrivatePath(path string) bool } +// A FileSource maps uris to FileHandles. This abstraction exists both for +// testability, and so that algorithms can be run equally on session and +// snapshot files. +type FileSource interface { + // GetFile returns the FileHandle for a given URI. + GetFile(ctx context.Context, uri span.URI) (FileHandle, error) +} + type BuiltinPackage struct { Package *ast.Package ParsedFile *ParsedGoFile