2019-04-17 13:37:20 -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 (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
2019-08-02 17:45:56 -06:00
|
|
|
"fmt"
|
2019-04-17 13:37:20 -06:00
|
|
|
|
|
|
|
"golang.org/x/tools/internal/jsonrpc2"
|
|
|
|
"golang.org/x/tools/internal/lsp/protocol"
|
2019-06-14 13:08:02 -06:00
|
|
|
"golang.org/x/tools/internal/lsp/source"
|
2019-04-17 13:37:20 -06:00
|
|
|
"golang.org/x/tools/internal/span"
|
2019-08-06 13:13:11 -06:00
|
|
|
errors "golang.org/x/xerrors"
|
2019-04-17 13:37:20 -06:00
|
|
|
)
|
|
|
|
|
2019-05-14 14:23:46 -06:00
|
|
|
func (s *Server) didOpen(ctx context.Context, params *protocol.DidOpenTextDocumentParams) error {
|
2020-01-22 19:58:50 -07:00
|
|
|
_, err := s.didModifyFiles(ctx, []source.FileModification{
|
|
|
|
{
|
|
|
|
URI: span.NewURI(params.TextDocument.URI),
|
|
|
|
Action: source.Open,
|
|
|
|
Version: params.TextDocument.Version,
|
|
|
|
Text: []byte(params.TextDocument.Text),
|
|
|
|
LanguageID: params.TextDocument.LanguageID,
|
|
|
|
},
|
2019-12-17 14:53:57 -07:00
|
|
|
})
|
2020-01-11 21:59:57 -07:00
|
|
|
return err
|
2019-12-04 16:45:25 -07:00
|
|
|
}
|
2019-06-28 17:32:53 -06:00
|
|
|
|
2019-12-04 16:45:25 -07:00
|
|
|
func (s *Server) didChange(ctx context.Context, params *protocol.DidChangeTextDocumentParams) error {
|
|
|
|
uri := span.NewURI(params.TextDocument.URI)
|
|
|
|
text, err := s.changedText(ctx, uri, params.ContentChanges)
|
2019-11-15 10:43:45 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-01-11 21:59:57 -07:00
|
|
|
c := source.FileModification{
|
2019-12-04 16:45:25 -07:00
|
|
|
URI: uri,
|
|
|
|
Action: source.Change,
|
|
|
|
Version: params.TextDocument.Version,
|
|
|
|
Text: text,
|
2019-12-10 10:29:37 -07:00
|
|
|
}
|
2020-01-22 19:58:50 -07:00
|
|
|
snapshots, err := s.didModifyFiles(ctx, []source.FileModification{c})
|
2019-11-15 10:43:45 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-01-22 19:58:50 -07:00
|
|
|
snapshot := snapshots[uri]
|
|
|
|
if snapshot == nil {
|
|
|
|
return errors.Errorf("no snapshot for %s", uri)
|
|
|
|
}
|
2019-12-10 10:29:37 -07:00
|
|
|
// Ideally, we should be able to specify that a generated file should be opened as read-only.
|
|
|
|
// Tell the user that they should not be editing a generated file.
|
2020-01-11 21:59:57 -07:00
|
|
|
if s.wasFirstChange(uri) && source.IsGenerated(ctx, snapshot, uri) {
|
|
|
|
if err := s.client.ShowMessage(ctx, &protocol.ShowMessageParams{
|
2019-12-10 10:29:37 -07:00
|
|
|
Message: fmt.Sprintf("Do not edit this file! %s is a generated file.", uri.Filename()),
|
|
|
|
Type: protocol.Warning,
|
2020-01-11 21:59:57 -07:00
|
|
|
}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-01-09 16:18:27 -07:00
|
|
|
}
|
|
|
|
return nil
|
2019-12-17 14:53:57 -07:00
|
|
|
}
|
|
|
|
|
2020-01-09 20:45:06 -07:00
|
|
|
func (s *Server) didChangeWatchedFiles(ctx context.Context, params *protocol.DidChangeWatchedFilesParams) error {
|
2020-01-22 19:58:50 -07:00
|
|
|
var modifications []source.FileModification
|
2020-01-09 20:45:06 -07:00
|
|
|
for _, change := range params.Changes {
|
|
|
|
uri := span.NewURI(change.URI)
|
|
|
|
|
|
|
|
// Do nothing if the file is open in the editor.
|
|
|
|
// The editor is the source of truth.
|
|
|
|
if s.session.IsOpen(uri) {
|
|
|
|
continue
|
|
|
|
}
|
2020-01-22 19:58:50 -07:00
|
|
|
modifications = append(modifications, source.FileModification{
|
|
|
|
URI: uri,
|
|
|
|
Action: changeTypeToFileAction(change.Type),
|
|
|
|
OnDisk: true,
|
2020-01-09 20:45:06 -07:00
|
|
|
})
|
|
|
|
}
|
2020-01-22 19:58:50 -07:00
|
|
|
_, err := s.didModifyFiles(ctx, modifications)
|
|
|
|
return err
|
2020-01-09 20:45:06 -07:00
|
|
|
}
|
|
|
|
|
2019-12-17 14:53:57 -07:00
|
|
|
func (s *Server) didSave(ctx context.Context, params *protocol.DidSaveTextDocumentParams) error {
|
|
|
|
c := source.FileModification{
|
|
|
|
URI: span.NewURI(params.TextDocument.URI),
|
|
|
|
Action: source.Save,
|
|
|
|
Version: params.TextDocument.Version,
|
|
|
|
}
|
|
|
|
if params.Text != nil {
|
|
|
|
c.Text = []byte(*params.Text)
|
|
|
|
}
|
2020-01-22 19:58:50 -07:00
|
|
|
_, err := s.didModifyFiles(ctx, []source.FileModification{c})
|
2019-12-17 14:53:57 -07:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) didClose(ctx context.Context, params *protocol.DidCloseTextDocumentParams) error {
|
2020-01-22 19:58:50 -07:00
|
|
|
_, err := s.didModifyFiles(ctx, []source.FileModification{
|
|
|
|
{
|
|
|
|
URI: span.NewURI(params.TextDocument.URI),
|
|
|
|
Action: source.Close,
|
|
|
|
Version: -1,
|
|
|
|
Text: nil,
|
|
|
|
},
|
2019-12-17 14:53:57 -07:00
|
|
|
})
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-01-22 19:58:50 -07:00
|
|
|
func (s *Server) didModifyFiles(ctx context.Context, modifications []source.FileModification) (map[span.URI]source.Snapshot, error) {
|
|
|
|
snapshots, err := s.session.DidModifyFiles(ctx, modifications)
|
2020-01-11 21:59:57 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-01-23 11:58:25 -07:00
|
|
|
snapshotByURI := make(map[span.URI]source.Snapshot)
|
2020-01-22 19:58:50 -07:00
|
|
|
for _, c := range modifications {
|
2020-01-23 11:58:25 -07:00
|
|
|
snapshotByURI[c.URI] = nil
|
2020-01-11 21:59:57 -07:00
|
|
|
}
|
2020-01-22 19:58:50 -07:00
|
|
|
// Avoid diagnosing the same snapshot twice.
|
2020-01-23 11:58:25 -07:00
|
|
|
snapshotSet := make(map[source.Snapshot][]span.URI)
|
|
|
|
for uri := range snapshotByURI {
|
2020-01-22 19:58:50 -07:00
|
|
|
view, err := s.session.ViewOf(uri)
|
2020-01-11 21:59:57 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-01-22 19:58:50 -07:00
|
|
|
var snapshot source.Snapshot
|
|
|
|
for _, s := range snapshots {
|
|
|
|
if s.View() == view {
|
|
|
|
if snapshot != nil {
|
|
|
|
return nil, errors.Errorf("duplicate snapshots for the same view")
|
|
|
|
}
|
|
|
|
snapshot = s
|
|
|
|
}
|
2020-01-11 21:59:57 -07:00
|
|
|
}
|
2020-01-23 11:58:25 -07:00
|
|
|
if snapshot == nil {
|
|
|
|
return nil, errors.Errorf("no snapshot for %s", uri)
|
|
|
|
}
|
|
|
|
snapshotByURI[uri] = snapshot
|
|
|
|
snapshotSet[snapshot] = append(snapshotSet[snapshot], uri)
|
2020-01-11 21:59:57 -07:00
|
|
|
}
|
2020-01-23 11:58:25 -07:00
|
|
|
for snapshot, uris := range snapshotSet {
|
|
|
|
for _, uri := range uris {
|
|
|
|
fh, err := snapshot.GetFile(uri)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// If a modification comes in for a go.mod file,
|
|
|
|
// and the view was never properly initialized,
|
|
|
|
// try to recreate the associated view.
|
|
|
|
switch fh.Identity().Kind {
|
|
|
|
case source.Mod:
|
|
|
|
newSnapshot, err := snapshot.View().Rebuild(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// Update the snapshot to the rebuilt one.
|
|
|
|
snapshot = newSnapshot
|
|
|
|
snapshotByURI[uri] = snapshot
|
|
|
|
}
|
|
|
|
}
|
2020-01-22 19:58:50 -07:00
|
|
|
go s.diagnoseSnapshot(snapshot)
|
2019-12-17 14:53:57 -07:00
|
|
|
}
|
2020-01-23 11:58:25 -07:00
|
|
|
return snapshotByURI, nil
|
2019-04-17 13:37:20 -06:00
|
|
|
}
|
|
|
|
|
2019-11-21 22:52:09 -07:00
|
|
|
func (s *Server) wasFirstChange(uri span.URI) bool {
|
|
|
|
if s.changedFiles == nil {
|
|
|
|
s.changedFiles = make(map[span.URI]struct{})
|
|
|
|
}
|
|
|
|
_, ok := s.changedFiles[uri]
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
2019-12-04 16:45:25 -07:00
|
|
|
func (s *Server) changedText(ctx context.Context, uri span.URI, changes []protocol.TextDocumentContentChangeEvent) ([]byte, error) {
|
|
|
|
if len(changes) == 0 {
|
|
|
|
return nil, jsonrpc2.NewErrorf(jsonrpc2.CodeInternalError, "no content changes provided")
|
2019-06-17 11:07:16 -06:00
|
|
|
}
|
2019-12-04 16:45:25 -07:00
|
|
|
|
|
|
|
// Check if the client sent the full content of the file.
|
|
|
|
// We accept a full content change even if the server expected incremental changes.
|
|
|
|
if len(changes) == 1 && changes[0].Range == nil && changes[0].RangeLength == 0 {
|
|
|
|
return []byte(changes[0].Text), nil
|
2019-04-17 13:37:20 -06:00
|
|
|
}
|
2019-12-04 16:45:25 -07:00
|
|
|
return s.applyIncrementalChanges(ctx, uri, changes)
|
2019-06-17 11:07:16 -06:00
|
|
|
}
|
2019-04-17 13:37:20 -06:00
|
|
|
|
2019-12-04 16:45:25 -07:00
|
|
|
func (s *Server) applyIncrementalChanges(ctx context.Context, uri span.URI, changes []protocol.TextDocumentContentChangeEvent) ([]byte, error) {
|
2020-01-06 16:08:39 -07:00
|
|
|
content, _, err := s.session.GetFile(uri).Read(ctx)
|
2019-06-03 23:04:18 -06:00
|
|
|
if err != nil {
|
2019-12-04 16:45:25 -07:00
|
|
|
return nil, jsonrpc2.NewErrorf(jsonrpc2.CodeInternalError, "file not found (%v)", err)
|
2019-04-17 13:37:20 -06:00
|
|
|
}
|
2019-06-17 11:07:16 -06:00
|
|
|
for _, change := range changes {
|
2019-12-04 16:45:25 -07:00
|
|
|
// Make sure to update column mapper along with the content.
|
2019-09-09 22:36:39 -06:00
|
|
|
converter := span.NewContentConverter(uri.Filename(), content)
|
|
|
|
m := &protocol.ColumnMapper{
|
|
|
|
URI: uri,
|
|
|
|
Converter: converter,
|
|
|
|
Content: content,
|
|
|
|
}
|
2019-12-04 16:45:25 -07:00
|
|
|
if change.Range == nil {
|
|
|
|
return nil, jsonrpc2.NewErrorf(jsonrpc2.CodeInternalError, "unexpected nil range for change")
|
|
|
|
}
|
|
|
|
spn, err := m.RangeSpan(*change.Range)
|
2019-04-17 13:37:20 -06:00
|
|
|
if err != nil {
|
2019-12-04 16:45:25 -07:00
|
|
|
return nil, err
|
2019-04-17 13:37:20 -06:00
|
|
|
}
|
|
|
|
if !spn.HasOffset() {
|
2019-12-04 16:45:25 -07:00
|
|
|
return nil, jsonrpc2.NewErrorf(jsonrpc2.CodeInternalError, "invalid range for content change")
|
2019-04-17 13:37:20 -06:00
|
|
|
}
|
|
|
|
start, end := spn.Start().Offset(), spn.End().Offset()
|
2019-05-02 20:35:49 -06:00
|
|
|
if end < start {
|
2019-12-04 16:45:25 -07:00
|
|
|
return nil, jsonrpc2.NewErrorf(jsonrpc2.CodeInternalError, "invalid range for content change")
|
2019-04-17 13:37:20 -06:00
|
|
|
}
|
|
|
|
var buf bytes.Buffer
|
|
|
|
buf.Write(content[:start])
|
|
|
|
buf.WriteString(change.Text)
|
|
|
|
buf.Write(content[end:])
|
|
|
|
content = buf.Bytes()
|
|
|
|
}
|
2019-12-04 16:45:25 -07:00
|
|
|
return content, nil
|
2019-04-17 13:37:20 -06:00
|
|
|
}
|
2020-01-09 20:45:06 -07:00
|
|
|
|
|
|
|
func changeTypeToFileAction(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
|
|
|
|
}
|