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"
|
2019-09-03 14:07:13 -06:00
|
|
|
"golang.org/x/tools/internal/telemetry/trace"
|
2019-08-19 10:53:51 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
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() {
|
|
|
|
gof, _ := view.FindFile(ctx, uri).(source.GoFile)
|
2019-08-19 10:53:51 -06:00
|
|
|
|
|
|
|
// If we have never seen this file before, there is nothing to do.
|
2019-09-03 14:07:13 -06:00
|
|
|
if gof == nil {
|
|
|
|
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)
|
|
|
|
|
|
|
|
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)
|
2019-08-19 10:53:51 -06:00
|
|
|
|
2019-09-09 17:26:26 -06:00
|
|
|
cphs, err := gof.CheckPackageHandles(ctx)
|
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-03 14:07:13 -06:00
|
|
|
var otherFile source.GoFile
|
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-03 14:07:13 -06:00
|
|
|
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).
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
}
|