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-11-20 14:38:43 -07:00
|
|
|
go s.diagnostics(view.Snapshot(), uri)
|
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 14:38:43 -07:00
|
|
|
cphs, err := view.Snapshot().PackageHandles(ctx, f)
|
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
|
|
|
|
}
|
|
|
|
cph, err := source.WidestCheckPackageHandle(cphs)
|
|
|
|
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-20 14:15:00 -07:00
|
|
|
for _, ph := range cph.CompiledGoFiles() {
|
2019-10-15 16:07:52 -06:00
|
|
|
if ph.File().Identity().URI == f.URI() {
|
2019-09-03 14:07:13 -06:00
|
|
|
continue
|
|
|
|
}
|
2019-10-15 16:07:52 -06:00
|
|
|
if f := view.FindFile(ctx, ph.File().Identity().URI); f != nil && s.session.IsOpen(f.URI()) {
|
|
|
|
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-11-20 22:08:58 -07:00
|
|
|
if err := s.publishDiagnostics(ctx, source.FileIdentity{
|
|
|
|
URI: uri,
|
|
|
|
}, []source.Diagnostic{}); 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-11-20 14:38:43 -07:00
|
|
|
go s.diagnostics(view.Snapshot(), otherFile.URI())
|
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
|
|
|
|
}
|