internal/lsp/cache: only invalidate metadata for go.mod files on save

Throwing away all of the metadata in the package means it will be
expensive to reload, so it makes sense to keep it around until a go.mod
file has been saved, indicating that the user is more likely to be done
typing. Otherwise, we will be triggering full package metadata reloads
on every keystroke.

Fixes golang/go#42529

Change-Id: Ibc4f4bc8937f8f7176da79e9fefe166468578993
Reviewed-on: https://go-review.googlesource.com/c/tools/+/271299
Trust: Rebecca Stambler <rstambler@golang.org>
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
gopls-CI: kokoro <noreply+kokoro@google.com>
Reviewed-by: Heschi Kreinick <heschi@google.com>
This commit is contained in:
Rebecca Stambler 2020-11-18 16:38:56 -05:00
parent a7380940e0
commit 6d15148156
4 changed files with 14 additions and 4 deletions

View File

@ -59,6 +59,10 @@ type fileHandle struct {
err error
}
func (h *fileHandle) Saved() bool {
return true
}
func (c *Cache) GetFile(ctx context.Context, uri span.URI) (source.FileHandle, error) {
return c.getFile(ctx, uri)
}

View File

@ -106,6 +106,10 @@ func (c *closedFile) VersionedFileIdentity() source.VersionedFileIdentity {
}
}
func (c *closedFile) Saved() bool {
return true
}
func (c *closedFile) Session() string {
return ""
}

View File

@ -1415,7 +1415,10 @@ func (s *snapshot) shouldInvalidateMetadata(ctx context.Context, newSnapshot *sn
}
// If a go.mod in the workspace has been changed, invalidate metadata.
if kind := originalFH.Kind(); kind == source.Mod {
return source.InDir(filepath.Dir(s.view.rootURI.Filename()), filepath.Dir(originalFH.URI().Filename()))
if !source.InDir(filepath.Dir(s.view.rootURI.Filename()), originalFH.URI().Filename()) {
return false
}
return currentFH.Saved()
}
// Get the original and current parsed files in order to check package name
// and imports. Use the new snapshot to parse to avoid modifying the

View File

@ -306,9 +306,6 @@ type Session interface {
// Overlay is the type for a file held in memory on a session.
type Overlay interface {
VersionedFileHandle
// Saved returns whether this overlay has been saved to disk.
Saved() bool
}
// FileModification represents a modification to a file.
@ -438,6 +435,8 @@ type FileHandle interface {
// Read reads the contents of a file.
// If the file is not available, returns a nil slice and an error.
Read() ([]byte, error)
// Saved reports whether the file has the same content on disk.
Saved() bool
}
// FileIdentity uniquely identifies a file at a version from a FileSystem.