зеркало из https://github.com/golang/tools.git
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 <rfindley@google.com> gopls-CI: kokoro <noreply+kokoro@google.com> Trust: Robert Findley <rfindley@google.com> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Rebecca Stambler <rstambler@golang.org>
This commit is contained in:
Родитель
b2746f1d7a
Коммит
0a3dcccdcf
|
@ -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
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
Загрузка…
Ссылка в новой задаче