From bc664416c64e8ef06e915b10964a33c9296bd9b3 Mon Sep 17 00:00:00 2001 From: Rohan Challa Date: Fri, 14 Feb 2020 11:52:17 -0500 Subject: [PATCH] internal/lsp: fix error handling when getting go.mod codelens This change has a fix for mod/codelens: check if we get an error from ParseModHandles().Upgrades(). This change also only runs codelens on save. Change-Id: I6dab7ddf3a08c650e4c670b039b1e99153ec8187 Reviewed-on: https://go-review.googlesource.com/c/tools/+/219478 Reviewed-by: Rebecca Stambler Run-TryBot: Rohan Challa TryBot-Result: Gobot Gobot --- internal/lsp/cache/snapshot.go | 8 ++++++++ internal/lsp/mod/code_lens.go | 3 +++ internal/lsp/server.go | 8 +++++--- internal/lsp/source/view.go | 6 ++++-- 4 files changed, 20 insertions(+), 5 deletions(-) diff --git a/internal/lsp/cache/snapshot.go b/internal/lsp/cache/snapshot.go index 897abc21c9..97febceb16 100644 --- a/internal/lsp/cache/snapshot.go +++ b/internal/lsp/cache/snapshot.go @@ -476,6 +476,14 @@ func (s *snapshot) IsOpen(uri span.URI) bool { return open } +func (s *snapshot) IsSaved(uri span.URI) bool { + s.mu.Lock() + defer s.mu.Unlock() + + ovl, open := s.files[uri].(*overlay) + return !open || ovl.saved +} + func (s *snapshot) awaitLoaded(ctx context.Context) error { // Do not return results until the snapshot's view has been initialized. s.view.awaitInitialized(ctx) diff --git a/internal/lsp/mod/code_lens.go b/internal/lsp/mod/code_lens.go index 3b1daa7a33..4217f82014 100644 --- a/internal/lsp/mod/code_lens.go +++ b/internal/lsp/mod/code_lens.go @@ -29,6 +29,9 @@ func CodeLens(ctx context.Context, snapshot source.Snapshot, uri span.URI) ([]pr return nil, err } f, m, upgrades, err := pmh.Upgrades(ctx) + if err != nil { + return nil, err + } var codelens []protocol.CodeLens for _, req := range f.Require { dep := req.Mod.Path diff --git a/internal/lsp/server.go b/internal/lsp/server.go index 0a719f76dd..ab1c26351a 100644 --- a/internal/lsp/server.go +++ b/internal/lsp/server.go @@ -75,8 +75,10 @@ func (s *Server) codeLens(ctx context.Context, params *protocol.CodeLensParams) if err != nil { return nil, err } - snapshot := view.Snapshot() - fh, err := snapshot.GetFile(uri) + if !view.Snapshot().IsSaved(uri) { + return nil, nil + } + fh, err := view.Snapshot().GetFile(uri) if err != nil { return nil, err } @@ -84,7 +86,7 @@ func (s *Server) codeLens(ctx context.Context, params *protocol.CodeLensParams) case source.Go: return nil, nil case source.Mod: - return mod.CodeLens(ctx, snapshot, uri) + return mod.CodeLens(ctx, view.Snapshot(), uri) } return nil, nil } diff --git a/internal/lsp/source/view.go b/internal/lsp/source/view.go index b7053f960f..7e824e33d1 100644 --- a/internal/lsp/source/view.go +++ b/internal/lsp/source/view.go @@ -33,10 +33,12 @@ type Snapshot interface { // if it is not already part of the view. GetFile(uri span.URI) (FileHandle, error) - // IsOpen returns whether the editor currently has a file open, - // and if its contents are saved on disk or not. + // IsOpen returns whether the editor currently has a file open. IsOpen(uri span.URI) bool + // IsSaved returns whether the contents are saved on disk or not. + IsSaved(uri span.URI) bool + // Analyze runs the analyses for the given package at this snapshot. Analyze(ctx context.Context, id string, analyzers []*analysis.Analyzer) ([]*Error, error)