Skip to content

Commit 6d15148

Browse files
committed
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>
1 parent a738094 commit 6d15148

File tree

4 files changed

+14
-4
lines changed

4 files changed

+14
-4
lines changed

internal/lsp/cache/cache.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ type fileHandle struct {
5959
err error
6060
}
6161

62+
func (h *fileHandle) Saved() bool {
63+
return true
64+
}
65+
6266
func (c *Cache) GetFile(ctx context.Context, uri span.URI) (source.FileHandle, error) {
6367
return c.getFile(ctx, uri)
6468
}

internal/lsp/cache/session.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,10 @@ func (c *closedFile) VersionedFileIdentity() source.VersionedFileIdentity {
106106
}
107107
}
108108

109+
func (c *closedFile) Saved() bool {
110+
return true
111+
}
112+
109113
func (c *closedFile) Session() string {
110114
return ""
111115
}

internal/lsp/cache/snapshot.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1415,7 +1415,10 @@ func (s *snapshot) shouldInvalidateMetadata(ctx context.Context, newSnapshot *sn
14151415
}
14161416
// If a go.mod in the workspace has been changed, invalidate metadata.
14171417
if kind := originalFH.Kind(); kind == source.Mod {
1418-
return source.InDir(filepath.Dir(s.view.rootURI.Filename()), filepath.Dir(originalFH.URI().Filename()))
1418+
if !source.InDir(filepath.Dir(s.view.rootURI.Filename()), originalFH.URI().Filename()) {
1419+
return false
1420+
}
1421+
return currentFH.Saved()
14191422
}
14201423
// Get the original and current parsed files in order to check package name
14211424
// and imports. Use the new snapshot to parse to avoid modifying the

internal/lsp/source/view.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -306,9 +306,6 @@ type Session interface {
306306
// Overlay is the type for a file held in memory on a session.
307307
type Overlay interface {
308308
VersionedFileHandle
309-
310-
// Saved returns whether this overlay has been saved to disk.
311-
Saved() bool
312309
}
313310

314311
// FileModification represents a modification to a file.
@@ -438,6 +435,8 @@ type FileHandle interface {
438435
// Read reads the contents of a file.
439436
// If the file is not available, returns a nil slice and an error.
440437
Read() ([]byte, error)
438+
// Saved reports whether the file has the same content on disk.
439+
Saved() bool
441440
}
442441

443442
// FileIdentity uniquely identifies a file at a version from a FileSystem.

0 commit comments

Comments
 (0)