2019-08-19 10:53:51 -06:00
|
|
|
// 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.
|
2019-10-20 22:00:20 -06:00
|
|
|
|
2019-08-19 10:53:51 -06:00
|
|
|
package lsp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"golang.org/x/tools/internal/lsp/protocol"
|
2019-09-03 14:07:13 -06:00
|
|
|
"golang.org/x/tools/internal/lsp/source"
|
|
|
|
"golang.org/x/tools/internal/lsp/telemetry"
|
2019-08-19 10:53:51 -06:00
|
|
|
"golang.org/x/tools/internal/span"
|
|
|
|
"golang.org/x/tools/internal/telemetry/log"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (s *Server) didChangeWatchedFiles(ctx context.Context, params *protocol.DidChangeWatchedFilesParams) error {
|
|
|
|
for _, change := range params.Changes {
|
|
|
|
uri := span.NewURI(change.URI)
|
2019-09-03 14:07:13 -06:00
|
|
|
ctx := telemetry.File.With(ctx, uri)
|
|
|
|
|
|
|
|
for _, view := range s.session.Views() {
|
2019-10-15 16:07:52 -06:00
|
|
|
if !view.Options().WatchFileChanges {
|
2019-09-03 14:07:13 -06:00
|
|
|
continue
|
2019-08-19 10:53:51 -06:00
|
|
|
}
|
2019-11-13 11:14:21 -07:00
|
|
|
action := toFileAction(change.Type)
|
|
|
|
switch action {
|
|
|
|
case source.Change, source.Create:
|
2019-10-15 16:07:52 -06:00
|
|
|
// 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
|
|
|
|
}
|
2019-11-13 11:14:21 -07:00
|
|
|
if s.session.DidChangeOutOfBand(ctx, uri, action) {
|
2019-10-15 16:07:52 -06:00
|
|
|
// If we had been tracking the given file,
|
|
|
|
// recompute diagnostics to reflect updated file contents.
|
2019-12-10 10:29:37 -07:00
|
|
|
f, err := view.GetFile(ctx, uri)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return s.diagnose(view.Snapshot(), f)
|
2019-10-15 16:07:52 -06:00
|
|
|
}
|
2019-11-13 11:14:21 -07:00
|
|
|
case source.Delete:
|
2019-10-15 16:07:52 -06:00
|
|
|
f := view.FindFile(ctx, uri)
|
|
|
|
// If we have never seen this file before, there is nothing to do.
|
|
|
|
if f == nil {
|
|
|
|
continue
|
|
|
|
}
|
2019-11-20 12:26:02 -07:00
|
|
|
snapshot := view.Snapshot()
|
|
|
|
fh := snapshot.Handle(ctx, f)
|
2019-11-29 23:17:57 -07:00
|
|
|
phs, err := snapshot.PackageHandles(ctx, fh)
|
2019-09-03 14:07:13 -06:00
|
|
|
if err != nil {
|
2019-10-15 16:07:52 -06:00
|
|
|
log.Error(ctx, "didChangeWatchedFiles: CheckPackageHandles", err, telemetry.File)
|
|
|
|
continue
|
|
|
|
}
|
2019-11-29 23:17:57 -07:00
|
|
|
ph, err := source.WidestCheckPackageHandle(phs)
|
2019-10-15 16:07:52 -06:00
|
|
|
if err != nil {
|
|
|
|
log.Error(ctx, "didChangeWatchedFiles: WidestCheckPackageHandle", err, telemetry.File)
|
2019-09-03 14:07:13 -06:00
|
|
|
continue
|
|
|
|
}
|
2019-09-09 17:26:26 -06:00
|
|
|
// Find a different file in the same package we can use to trigger diagnostics.
|
|
|
|
// TODO(rstambler): Allow diagnostics to be called per-package to avoid this.
|
2019-09-27 11:17:59 -06:00
|
|
|
var otherFile source.File
|
2019-11-29 23:17:57 -07:00
|
|
|
for _, pgh := range ph.CompiledGoFiles() {
|
|
|
|
if pgh.File().Identity().URI == f.URI() {
|
2019-09-03 14:07:13 -06:00
|
|
|
continue
|
|
|
|
}
|
2019-11-29 23:17:57 -07:00
|
|
|
if f := view.FindFile(ctx, pgh.File().Identity().URI); f != nil && s.session.IsOpen(f.URI()) {
|
2019-10-15 16:07:52 -06:00
|
|
|
otherFile = f
|
2019-09-03 14:07:13 -06:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2019-10-15 16:07:52 -06:00
|
|
|
|
|
|
|
// Notify the view of the deletion of the file.
|
2019-11-13 11:14:21 -07:00
|
|
|
s.session.DidChangeOutOfBand(ctx, uri, action)
|
2019-09-03 14:07:13 -06:00
|
|
|
|
2019-09-17 12:36:39 -06:00
|
|
|
// If this was the only file in the package, clear its diagnostics.
|
|
|
|
if otherFile == nil {
|
2019-12-05 14:37:47 -07:00
|
|
|
if err := s.client.PublishDiagnostics(ctx, &protocol.PublishDiagnosticsParams{
|
|
|
|
URI: protocol.NewURI(uri),
|
|
|
|
Version: fh.Identity().Version,
|
|
|
|
}); err != nil {
|
2019-09-17 12:36:39 -06:00
|
|
|
log.Error(ctx, "failed to clear diagnostics", err, telemetry.URI.Of(uri))
|
|
|
|
}
|
|
|
|
return nil
|
2019-09-03 14:07:13 -06:00
|
|
|
}
|
2019-10-15 16:07:52 -06:00
|
|
|
|
|
|
|
// Refresh diagnostics for the package the file belonged to.
|
2019-12-10 10:29:37 -07:00
|
|
|
go s.diagnoseFile(view.Snapshot(), otherFile)
|
2019-09-03 14:07:13 -06:00
|
|
|
}
|
2019-08-19 10:53:51 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2019-11-13 11:14:21 -07:00
|
|
|
|
|
|
|
func toFileAction(ct protocol.FileChangeType) source.FileAction {
|
|
|
|
switch ct {
|
|
|
|
case protocol.Changed:
|
|
|
|
return source.Change
|
|
|
|
case protocol.Created:
|
|
|
|
return source.Create
|
|
|
|
case protocol.Deleted:
|
|
|
|
return source.Delete
|
|
|
|
}
|
|
|
|
return source.UnknownFileAction
|
|
|
|
}
|