mirror of
https://github.com/golang/go
synced 2024-11-19 03:24:40 -07:00
2c3de6a5ae
We split aquiring a "handle" from reading a files contents so that we can do the former eagerly and the latter lazily. We also "version" the handles so that the same file at different versions is a different handle. Change-Id: I06cc346d4b4c77d784aa454702c54689f2f177e0 Reviewed-on: https://go-review.googlesource.com/c/tools/+/179917 Reviewed-by: Rebecca Stambler <rstambler@golang.org>
115 lines
3.6 KiB
Go
115 lines
3.6 KiB
Go
// 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"
|
|
|
|
"golang.org/x/tools/internal/jsonrpc2"
|
|
"golang.org/x/tools/internal/lsp/protocol"
|
|
"golang.org/x/tools/internal/span"
|
|
)
|
|
|
|
func (s *Server) didOpen(ctx context.Context, params *protocol.DidOpenTextDocumentParams) error {
|
|
uri := span.NewURI(params.TextDocument.URI)
|
|
s.session.DidOpen(uri)
|
|
return s.cacheAndDiagnose(ctx, uri, []byte(params.TextDocument.Text))
|
|
}
|
|
|
|
func (s *Server) didChange(ctx context.Context, params *protocol.DidChangeTextDocumentParams) error {
|
|
if len(params.ContentChanges) < 1 {
|
|
return jsonrpc2.NewErrorf(jsonrpc2.CodeInternalError, "no content changes provided")
|
|
}
|
|
|
|
var text string
|
|
switch s.textDocumentSyncKind {
|
|
case protocol.Incremental:
|
|
var err error
|
|
text, err = s.applyChanges(ctx, params)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
case protocol.Full:
|
|
// We expect the full content of file, i.e. a single change with no range.
|
|
change := params.ContentChanges[0]
|
|
if change.RangeLength != 0 {
|
|
return jsonrpc2.NewErrorf(jsonrpc2.CodeInternalError, "unexpected change range provided")
|
|
}
|
|
text = change.Text
|
|
}
|
|
return s.cacheAndDiagnose(ctx, span.NewURI(params.TextDocument.URI), []byte(text))
|
|
}
|
|
|
|
func (s *Server) cacheAndDiagnose(ctx context.Context, uri span.URI, content []byte) error {
|
|
view := s.session.ViewOf(uri)
|
|
if err := view.SetContent(ctx, uri, content); err != nil {
|
|
return err
|
|
}
|
|
go func() {
|
|
ctx := view.BackgroundContext()
|
|
s.Diagnostics(ctx, view, uri)
|
|
}()
|
|
return nil
|
|
}
|
|
|
|
func (s *Server) applyChanges(ctx context.Context, params *protocol.DidChangeTextDocumentParams) (string, error) {
|
|
if len(params.ContentChanges) == 1 && params.ContentChanges[0].Range == nil {
|
|
// If range is empty, we expect the full content of file, i.e. a single change with no range.
|
|
change := params.ContentChanges[0]
|
|
if change.RangeLength != 0 {
|
|
return "", jsonrpc2.NewErrorf(jsonrpc2.CodeInternalError, "unexpected change range provided")
|
|
}
|
|
return change.Text, nil
|
|
}
|
|
|
|
uri := span.NewURI(params.TextDocument.URI)
|
|
fc := s.session.GetFile(uri).Read(ctx)
|
|
if fc.Error != nil {
|
|
return "", jsonrpc2.NewErrorf(jsonrpc2.CodeInternalError, "file not found")
|
|
}
|
|
content := fc.Data
|
|
fset := s.session.Cache().FileSet()
|
|
filename, err := uri.Filename()
|
|
if err != nil {
|
|
return "", jsonrpc2.NewErrorf(jsonrpc2.CodeInternalError, "no filename for %s", uri)
|
|
}
|
|
for _, change := range params.ContentChanges {
|
|
// Update column mapper along with the content.
|
|
m := protocol.NewColumnMapper(uri, filename, fset, nil, content)
|
|
|
|
spn, err := m.RangeSpan(*change.Range)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
if !spn.HasOffset() {
|
|
return "", jsonrpc2.NewErrorf(jsonrpc2.CodeInternalError, "invalid range for content change")
|
|
}
|
|
start, end := spn.Start().Offset(), spn.End().Offset()
|
|
if end < start {
|
|
return "", jsonrpc2.NewErrorf(jsonrpc2.CodeInternalError, "invalid range for content change")
|
|
}
|
|
var buf bytes.Buffer
|
|
buf.Write(content[:start])
|
|
buf.WriteString(change.Text)
|
|
buf.Write(content[end:])
|
|
content = buf.Bytes()
|
|
}
|
|
return string(content), nil
|
|
}
|
|
|
|
func (s *Server) didSave(ctx context.Context, params *protocol.DidSaveTextDocumentParams) error {
|
|
uri := span.NewURI(params.TextDocument.URI)
|
|
s.session.DidSave(uri)
|
|
return nil // ignore
|
|
}
|
|
|
|
func (s *Server) didClose(ctx context.Context, params *protocol.DidCloseTextDocumentParams) error {
|
|
uri := span.NewURI(params.TextDocument.URI)
|
|
s.session.DidClose(uri)
|
|
view := s.session.ViewOf(uri)
|
|
return view.SetContent(ctx, uri, nil)
|
|
}
|