internal/lsp: clear diagnostics for files on close, even with errors
Change-Id: I7230d560c57455c53200f4d8f2702fbbdd3f5e51 Reviewed-on: https://go-review.googlesource.com/c/tools/+/184162 Run-TryBot: Rebecca Stambler <rstambler@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Ian Cottrell <iancottrell@google.com>
This commit is contained in:
parent
d5de6b6fc5
commit
59bec04229
|
@ -116,6 +116,14 @@ func (s *Server) didClose(ctx context.Context, params *protocol.DidCloseTextDocu
|
||||||
if err := view.SetContent(ctx, uri, nil); err != nil {
|
if err := view.SetContent(ctx, uri, nil); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
clear := []span.URI{uri} // by default, clear the closed URI
|
||||||
|
defer func() {
|
||||||
|
for _, uri := range clear {
|
||||||
|
if err := s.publishDiagnostics(ctx, view, uri, []source.Diagnostic{}); err != nil {
|
||||||
|
s.session.Logger().Errorf(ctx, "failed to clear diagnostics for %s: %v", uri, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
// If the current file was the only open file for its package,
|
// If the current file was the only open file for its package,
|
||||||
// clear out all diagnostics for the package.
|
// clear out all diagnostics for the package.
|
||||||
f, err := view.GetFile(ctx, uri)
|
f, err := view.GetFile(ctx, uri)
|
||||||
|
@ -126,6 +134,7 @@ func (s *Server) didClose(ctx context.Context, params *protocol.DidCloseTextDocu
|
||||||
// For non-Go files, don't return any diagnostics.
|
// For non-Go files, don't return any diagnostics.
|
||||||
gof, ok := f.(source.GoFile)
|
gof, ok := f.(source.GoFile)
|
||||||
if !ok {
|
if !ok {
|
||||||
|
s.session.Logger().Errorf(ctx, "closing a non-Go file, no diagnostics to clear")
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
pkg := gof.GetPackage(ctx)
|
pkg := gof.GetPackage(ctx)
|
||||||
|
@ -133,27 +142,13 @@ func (s *Server) didClose(ctx context.Context, params *protocol.DidCloseTextDocu
|
||||||
s.session.Logger().Errorf(ctx, "no package available for %s", uri)
|
s.session.Logger().Errorf(ctx, "no package available for %s", uri)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
reports := make(map[span.URI][]source.Diagnostic)
|
|
||||||
clearDiagnostics := true
|
|
||||||
for _, filename := range pkg.GetFilenames() {
|
for _, filename := range pkg.GetFilenames() {
|
||||||
uri := span.NewURI(filename)
|
// If other files from this package are open, don't clear.
|
||||||
reports[uri] = []source.Diagnostic{}
|
if s.session.IsOpen(span.NewURI(filename)) {
|
||||||
if span.CompareURI(uri, gof.URI()) == 0 {
|
clear = nil
|
||||||
continue
|
return nil
|
||||||
}
|
|
||||||
// If other files from this package are open.
|
|
||||||
if s.session.IsOpen(uri) {
|
|
||||||
clearDiagnostics = false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// We still have open files for this package, so don't clear diagnostics.
|
|
||||||
if !clearDiagnostics {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
for uri, diagnostics := range reports {
|
|
||||||
if err := s.publishDiagnostics(ctx, view, uri, diagnostics); err != nil {
|
|
||||||
s.session.Logger().Errorf(ctx, "failed to clear diagnostics for %s: %v", uri, err)
|
|
||||||
}
|
}
|
||||||
|
clear = append(clear, span.FileURI(filename))
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue