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.
|
|
|
|
|
|
|
|
package lsp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-09-09 17:26:26 -06:00
|
|
|
"sort"
|
2019-08-19 10:53:51 -06:00
|
|
|
|
|
|
|
"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 {
|
2019-09-05 22:17:36 -06:00
|
|
|
options := s.session.Options()
|
|
|
|
if !options.WatchFileChanges {
|
2019-08-19 10:53:51 -06:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
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-09-27 11:17:59 -06:00
|
|
|
f := view.FindFile(ctx, uri)
|
2019-08-19 10:53:51 -06:00
|
|
|
|
|
|
|
// If we have never seen this file before, there is nothing to do.
|
2019-09-27 11:17:59 -06:00
|
|
|
if f == nil {
|
2019-09-03 14:07:13 -06:00
|
|
|
continue
|
2019-08-19 10:53:51 -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-09-03 14:07:13 -06:00
|
|
|
switch change.Type {
|
|
|
|
case protocol.Changed:
|
|
|
|
log.Print(ctx, "watched file changed", telemetry.File)
|
|
|
|
|
2019-09-23 18:06:15 -06:00
|
|
|
s.session.DidChangeOutOfBand(ctx, uri, change.Type)
|
2019-09-03 14:07:13 -06:00
|
|
|
|
|
|
|
// Refresh diagnostics to reflect updated file contents.
|
2019-09-17 12:36:39 -06:00
|
|
|
go s.diagnostics(view, uri)
|
2019-09-03 14:07:13 -06:00
|
|
|
case protocol.Created:
|
|
|
|
log.Print(ctx, "watched file created", telemetry.File)
|
|
|
|
case protocol.Deleted:
|
|
|
|
log.Print(ctx, "watched file deleted", telemetry.File)
|
2019-08-19 10:53:51 -06:00
|
|
|
|
2019-09-27 11:17:59 -06:00
|
|
|
_, cphs, err := view.CheckPackageHandles(ctx, f)
|
2019-09-03 14:07:13 -06:00
|
|
|
if err != nil {
|
|
|
|
log.Error(ctx, "didChangeWatchedFiles: GetPackage", err, telemetry.File)
|
|
|
|
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-09-09 17:26:26 -06:00
|
|
|
sort.Slice(cphs, func(i, j int) bool {
|
|
|
|
return len(cphs[i].Files()) > len(cphs[j].Files())
|
|
|
|
})
|
|
|
|
for _, ph := range cphs[0].Files() {
|
|
|
|
if len(cphs) > 1 && contains(cphs[1], ph.File()) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
ident := ph.File().Identity()
|
2019-09-27 11:17:59 -06:00
|
|
|
if ident.URI == f.URI() {
|
2019-09-03 14:07:13 -06:00
|
|
|
continue
|
|
|
|
}
|
2019-09-27 11:17:59 -06:00
|
|
|
otherFile := view.FindFile(ctx, ident.URI)
|
2019-09-03 14:07:13 -06:00
|
|
|
if otherFile != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2019-09-23 18:06:15 -06:00
|
|
|
s.session.DidChangeOutOfBand(ctx, uri, change.Type)
|
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 {
|
|
|
|
if err := s.publishDiagnostics(ctx, uri, []source.Diagnostic{}); err != nil {
|
|
|
|
log.Error(ctx, "failed to clear diagnostics", err, telemetry.URI.Of(uri))
|
|
|
|
}
|
|
|
|
return nil
|
2019-09-03 14:07:13 -06:00
|
|
|
}
|
2019-09-18 14:54:31 -06:00
|
|
|
go s.diagnostics(view, otherFile.URI())
|
2019-09-03 14:07:13 -06:00
|
|
|
}
|
2019-08-19 10:53:51 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2019-09-09 17:26:26 -06:00
|
|
|
|
|
|
|
func contains(cph source.CheckPackageHandle, fh source.FileHandle) bool {
|
|
|
|
for _, ph := range cph.Files() {
|
|
|
|
if ph.File().Identity().URI == fh.Identity().URI {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|