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-07-14 21:08:10 -06:00
|
|
|
"golang.org/x/tools/internal/lsp/telemetry"
|
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-04 16:45:25 -07:00
|
|
|
return s.didModifyFile(ctx, source.FileModification{
|
|
|
|
URI: uri,
|
|
|
|
Action: source.Open,
|
|
|
|
Version: params.TextDocument.Version,
|
|
|
|
Text: []byte(params.TextDocument.Text),
|
|
|
|
LanguageID: params.TextDocument.LanguageID,
|
|
|
|
})
|
|
|
|
}
|
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)
|
|
|
|
}
|
|
|
|
return s.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 {
|
|
|
|
return s.didModifyFile(ctx, source.FileModification{
|
|
|
|
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-04 16:45:25 -07:00
|
|
|
return s.didModifyFile(ctx, source.FileModification{
|
|
|
|
URI: uri,
|
|
|
|
Action: source.Change,
|
|
|
|
Version: params.TextDocument.Version,
|
|
|
|
Text: text,
|
|
|
|
})
|
2019-05-14 14:23:46 -06:00
|
|
|
}
|
|
|
|
|
2019-12-04 16:45:25 -07:00
|
|
|
// didModifyFile propagates the information about the file modification
|
|
|
|
// to the cache layer and runs diagnostics.
|
|
|
|
//
|
|
|
|
// TODO(rstambler): This function should be mostly unnecessary once we unify the methods
|
|
|
|
// for making changes to a file in internal/lsp/cache.
|
|
|
|
func (s *Server) didModifyFile(ctx context.Context, c source.FileModification) error {
|
|
|
|
ctx = telemetry.URI.With(ctx, c.URI)
|
2019-06-17 11:07:16 -06:00
|
|
|
|
2019-12-04 16:45:25 -07:00
|
|
|
view, err := s.session.ViewOf(c.URI)
|
2019-11-15 10:43:45 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-12-04 16:45:25 -07:00
|
|
|
switch c.Action {
|
|
|
|
case source.Open:
|
|
|
|
kind := source.DetectLanguage(c.LanguageID, c.URI.Filename())
|
|
|
|
if kind == source.UnknownKind {
|
|
|
|
return errors.Errorf("didModifyFile: unknown file kind for %s", c.URI)
|
|
|
|
}
|
|
|
|
if err := s.session.DidOpen(ctx, c.URI, kind, c.Version, c.Text); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
case source.Change:
|
|
|
|
view.SetContent(ctx, c.URI, c.Version, c.Text)
|
|
|
|
|
|
|
|
// 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(c.URI) && source.IsGenerated(ctx, view, c.URI) {
|
|
|
|
s.client.ShowMessage(ctx, &protocol.ShowMessageParams{
|
|
|
|
Message: fmt.Sprintf("Do not edit this file! %s is a generated file.", c.URI.Filename()),
|
|
|
|
Type: protocol.Warning,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
case source.Save:
|
|
|
|
s.session.DidSave(c.URI, c.Version)
|
|
|
|
case source.Close:
|
|
|
|
s.session.DidClose(c.URI)
|
|
|
|
view.SetContent(ctx, c.URI, c.Version, c.Text)
|
2019-08-12 16:50:01 -06:00
|
|
|
}
|
2019-09-17 12:36:39 -06:00
|
|
|
|
2019-12-04 16:45:25 -07:00
|
|
|
// We should run diagnostics after opening or changing a file.
|
|
|
|
switch c.Action {
|
|
|
|
case source.Open, source.Change:
|
|
|
|
go s.diagnoseFile(view.Snapshot(), c.URI)
|
|
|
|
}
|
2019-05-14 14:23:46 -06:00
|
|
|
return 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-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
|
|
|
}
|