Skip to content

Commit

Permalink
internal/lsp/cache: only invalidate metadata for go.mod files on save
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
stamblerre committed Nov 20, 2020
1 parent a738094 commit 6d15148
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 4 deletions.
4 changes: 4 additions & 0 deletions internal/lsp/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
4 changes: 4 additions & 0 deletions internal/lsp/cache/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ func (c *closedFile) VersionedFileIdentity() source.VersionedFileIdentity {
}
}

func (c *closedFile) Saved() bool {
return true
}

func (c *closedFile) Session() string {
return ""
}
Expand Down
5 changes: 4 additions & 1 deletion internal/lsp/cache/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 2 additions & 3 deletions internal/lsp/source/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 6d15148

Please sign in to comment.