mirror of
https://github.com/golang/go
synced 2024-11-18 23:14:43 -07:00
5e3480f0e0
Now when a file is deleted we force the file's packages to refresh go/packages metadata, and kick off diagnostics. I made a couple other changes to watched file handling: - Kick off diagnostics in a goroutine to match how DidChange works. This will allow us to work through big sets of file changes faster, and will save duplicated work once type checking can be canceled. - Don't assume a watched file is only part of one view. Two interesting cases we don't handle yet: - If the deleted file was the only file in the package, we don't currently update diagnostics for dependent packages. This requires rejiggering how diagnostics are invoked a bit. - If the deleted file is still open in the editor and then later closed, we don't trigger metadata/diagnostics refresh on DidClose. Updates golang/go#31553 Change-Id: I65768614c24d9800ffea149ccdbdbd3cb7b2f3d8 Reviewed-on: https://go-review.googlesource.com/c/tools/+/193121 Run-TryBot: Rebecca Stambler <rstambler@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Ian Cottrell <iancottrell@google.com>
101 lines
2.7 KiB
Go
101 lines
2.7 KiB
Go
// Copyright 2019 The Go Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package lsp
|
|
|
|
import (
|
|
"context"
|
|
|
|
"golang.org/x/tools/internal/lsp/protocol"
|
|
"golang.org/x/tools/internal/lsp/source"
|
|
"golang.org/x/tools/internal/lsp/telemetry"
|
|
"golang.org/x/tools/internal/span"
|
|
"golang.org/x/tools/internal/telemetry/log"
|
|
"golang.org/x/tools/internal/telemetry/trace"
|
|
)
|
|
|
|
func (s *Server) didChangeWatchedFiles(ctx context.Context, params *protocol.DidChangeWatchedFilesParams) error {
|
|
options := s.session.Options()
|
|
if !options.WatchFileChanges {
|
|
return nil
|
|
}
|
|
|
|
for _, change := range params.Changes {
|
|
uri := span.NewURI(change.URI)
|
|
|
|
ctx := telemetry.File.With(ctx, uri)
|
|
|
|
for _, view := range s.session.Views() {
|
|
gof, _ := view.FindFile(ctx, uri).(source.GoFile)
|
|
|
|
// If we have never seen this file before, there is nothing to do.
|
|
if gof == nil {
|
|
continue
|
|
}
|
|
|
|
// If client has this file open, don't do anything. The client's contents
|
|
// must remain the source of truth.
|
|
if s.session.IsOpen(uri) {
|
|
break
|
|
}
|
|
|
|
switch change.Type {
|
|
case protocol.Changed:
|
|
log.Print(ctx, "watched file changed", telemetry.File)
|
|
|
|
s.session.DidChangeOutOfBand(ctx, gof, change.Type)
|
|
|
|
// Refresh diagnostics to reflect updated file contents.
|
|
go func(view source.View) {
|
|
ctx := view.BackgroundContext()
|
|
ctx, done := trace.StartSpan(ctx, "lsp:background-worker")
|
|
defer done()
|
|
s.Diagnostics(ctx, view, uri)
|
|
}(view)
|
|
case protocol.Created:
|
|
log.Print(ctx, "watched file created", telemetry.File)
|
|
case protocol.Deleted:
|
|
log.Print(ctx, "watched file deleted", telemetry.File)
|
|
|
|
pkg, err := gof.GetPackage(ctx)
|
|
if err != nil {
|
|
log.Error(ctx, "didChangeWatchedFiles: GetPackage", err, telemetry.File)
|
|
continue
|
|
}
|
|
|
|
// Find a different file in the same package we can use to
|
|
// trigger diagnostics.
|
|
var otherFile source.GoFile
|
|
for _, pgh := range pkg.GetHandles() {
|
|
ident := pgh.File().Identity()
|
|
if ident.URI == gof.URI() {
|
|
continue
|
|
}
|
|
otherFile, _ = view.FindFile(ctx, ident.URI).(source.GoFile)
|
|
if otherFile != nil {
|
|
break
|
|
}
|
|
}
|
|
|
|
s.session.DidChangeOutOfBand(ctx, gof, change.Type)
|
|
|
|
if otherFile != nil {
|
|
// Refresh diagnostics to reflect updated file contents.
|
|
go func(view source.View) {
|
|
ctx := view.BackgroundContext()
|
|
ctx, done := trace.StartSpan(ctx, "lsp:background-worker")
|
|
defer done()
|
|
s.Diagnostics(ctx, view, otherFile.URI())
|
|
}(view)
|
|
} else {
|
|
// TODO: Handle case when there is no other file (i.e. deleted
|
|
// file was the only file in the package).
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|