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 {
|
2019-12-04 16:45:25 -07:00
|
|
|
// Confirm that the file's language ID is related to Go.
|
2019-05-17 10:15:22 -06:00
|
|
|
uri := span.NewURI(params.TextDocument.URI)
|
2019-12-10 10:29:37 -07:00
|
|
|
if err := s.session.DidModifyFile(ctx, source.FileModification{
|
2019-12-04 16:45:25 -07:00
|
|
|
URI: uri,
|
|
|
|
Action: source.Open,
|
|
|
|
Version: params.TextDocument.Version,
|
|
|
|
Text: []byte(params.TextDocument.Text),
|
|
|
|
LanguageID: params.TextDocument.LanguageID,
|
2019-12-10 10:29:37 -07:00
|
|
|
}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
view, err := s.session.ViewOf(uri)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
f, err := view.GetFile(ctx, uri)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Always run diagnostics when a file is opened.
|
|
|
|
return s.diagnose(view.Snapshot(), f)
|
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) didSave(ctx context.Context, params *protocol.DidSaveTextDocumentParams) error {
|
2019-12-10 10:45:31 -07:00
|
|
|
c := source.FileModification{
|
2019-12-04 16:45:25 -07:00
|
|
|
URI: span.NewURI(params.TextDocument.URI),
|
|
|
|
Action: source.Save,
|
|
|
|
Version: params.TextDocument.Version,
|
2019-12-10 10:45:31 -07:00
|
|
|
}
|
|
|
|
if params.Text != nil {
|
|
|
|
c.Text = []byte(*params.Text)
|
|
|
|
}
|
2019-12-10 10:29:37 -07:00
|
|
|
return s.session.DidModifyFile(ctx, c)
|
2019-12-04 16:45:25 -07:00
|
|
|
}
|
2019-06-27 12:59:09 -06:00
|
|
|
|
2019-12-04 16:45:25 -07:00
|
|
|
func (s *Server) didClose(ctx context.Context, params *protocol.DidCloseTextDocumentParams) error {
|
2019-12-10 10:29:37 -07:00
|
|
|
return s.session.DidModifyFile(ctx, source.FileModification{
|
2019-12-04 16:45:25 -07:00
|
|
|
URI: span.NewURI(params.TextDocument.URI),
|
|
|
|
Action: source.Close,
|
|
|
|
Version: -1,
|
|
|
|
Text: nil,
|
|
|
|
})
|
|
|
|
}
|
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
|
|
|
|
}
|
2019-12-10 10:29:37 -07:00
|
|
|
if err := s.session.DidModifyFile(ctx, 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
|
|
|
}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
view, err := s.session.ViewOf(uri)
|
2019-11-15 10:43:45 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
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.
|
|
|
|
if s.wasFirstChange(uri) && source.IsGenerated(ctx, view, uri) {
|
|
|
|
s.client.ShowMessage(ctx, &protocol.ShowMessageParams{
|
|
|
|
Message: fmt.Sprintf("Do not edit this file! %s is a generated file.", uri.Filename()),
|
|
|
|
Type: protocol.Warning,
|
|
|
|
})
|
2019-08-12 16:50:01 -06:00
|
|
|
}
|
2019-12-10 10:29:37 -07:00
|
|
|
f, err := view.GetFile(ctx, uri)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-12-04 16:45:25 -07:00
|
|
|
}
|
2019-12-10 10:29:37 -07:00
|
|
|
// Always update diagnostics after a file change.
|
|
|
|
return s.diagnose(view.Snapshot(), f)
|
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-11-19 14:59:25 -07:00
|
|
|
|
2019-12-04 16:45:25 -07:00
|
|
|
// We only accept an incremental change if that's what the server expects.
|
|
|
|
if s.session.Options().TextDocumentSyncKind == protocol.Full {
|
|
|
|
return nil, errors.Errorf("expected a full content change, received incremental changes for %s", uri)
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2019-09-18 23:21:54 -06:00
|
|
|
content, _, err := s.session.GetFile(uri, source.UnknownKind).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
|
|
|
}
|