Skip to content

Commit ac41720

Browse files
committed
internal/lsp: run packages.Load only if imports are added or changed
Previously, we would reload if a user's import list decreased or simply changed order. This is not necessary. Now, we only re-run if a new import needs to be loaded. Updates golang/go#35388 Change-Id: I47874afe773dddb835ac27b18895e7a082950dc7 Reviewed-on: https://go-review.googlesource.com/c/tools/+/209057 Reviewed-by: Heschi Kreinick <[email protected]>
1 parent 427c522 commit ac41720

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

internal/lsp/cache/load.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,13 +96,17 @@ func (c *cache) shouldLoad(ctx context.Context, s *snapshot, originalFH, current
9696
if original.Name.Name != current.Name.Name {
9797
return true
9898
}
99-
// If the package's imports have changed, re-run `go list`.
100-
if len(original.Imports) != len(current.Imports) {
99+
// If the package's imports have increased, definitely re-run `go list`.
100+
if len(original.Imports) < len(current.Imports) {
101101
return true
102102
}
103-
for i, importSpec := range original.Imports {
104-
// TODO: Handle the case where the imports have just been re-ordered.
105-
if importSpec.Path.Value != current.Imports[i].Path.Value {
103+
importSet := make(map[string]struct{})
104+
for _, importSpec := range original.Imports {
105+
importSet[importSpec.Path.Value] = struct{}{}
106+
}
107+
// If any of the current imports were not in the original imports.
108+
for _, importSpec := range current.Imports {
109+
if _, ok := importSet[importSpec.Path.Value]; !ok {
106110
return true
107111
}
108112
}

0 commit comments

Comments
 (0)