mirror of
https://github.com/golang/go
synced 2024-11-18 23:14:43 -07:00
73cd2cc3b5
Running staticcheck on the entire workspace causes a slowdown, and most likely users don't want to see staticcheck reports for every subdirectory of their workspace. Only run staticcheck on open files. Also, fixed a staticcheck warning that showed up along the way. Filed golang/go#35718 to remind ourselves to fix all of the staticcheck warnings that showed up when we ran gopls with staticcheck on x/tools. Finally, made sure that we don't send empty diagnostics when diagnosing the snapshot on start-up, as that is not necessary. Change-Id: Ic51d1abfc80b1b53397057f06a4cfd7e2dc930f9 Reviewed-on: https://go-review.googlesource.com/c/tools/+/208098 Run-TryBot: Rebecca Stambler <rstambler@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Ian Cottrell <iancottrell@google.com>
160 lines
4.9 KiB
Go
160 lines
4.9 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"
|
|
"fmt"
|
|
|
|
"golang.org/x/tools/internal/jsonrpc2"
|
|
"golang.org/x/tools/internal/lsp/protocol"
|
|
"golang.org/x/tools/internal/lsp/source"
|
|
"golang.org/x/tools/internal/lsp/telemetry"
|
|
"golang.org/x/tools/internal/span"
|
|
errors "golang.org/x/xerrors"
|
|
)
|
|
|
|
func (s *Server) didOpen(ctx context.Context, params *protocol.DidOpenTextDocumentParams) error {
|
|
uri := span.NewURI(params.TextDocument.URI)
|
|
text := []byte(params.TextDocument.Text)
|
|
version := params.TextDocument.Version
|
|
|
|
// Confirm that the file's language ID is related to Go.
|
|
fileKind := source.DetectLanguage(params.TextDocument.LanguageID, uri.Filename())
|
|
|
|
// Open the file.
|
|
s.session.DidOpen(ctx, uri, fileKind, version, text)
|
|
|
|
view, err := s.session.ViewOf(uri)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
snapshot := view.Snapshot()
|
|
|
|
// Run diagnostics on the newly-opened file.
|
|
go s.diagnoseFile(snapshot, uri)
|
|
|
|
return nil
|
|
}
|
|
|
|
func (s *Server) didChange(ctx context.Context, params *protocol.DidChangeTextDocumentParams) error {
|
|
options := s.session.Options()
|
|
if len(params.ContentChanges) < 1 {
|
|
return jsonrpc2.NewErrorf(jsonrpc2.CodeInternalError, "no content changes provided")
|
|
}
|
|
|
|
uri := span.NewURI(params.TextDocument.URI)
|
|
|
|
// Check if the client sent the full content of the file.
|
|
// We accept a full content change even if the server expected incremental changes.
|
|
text, isFullChange := fullChange(params.ContentChanges)
|
|
|
|
// We only accept an incremental change if the server expected it.
|
|
if !isFullChange {
|
|
switch options.TextDocumentSyncKind {
|
|
case protocol.Full:
|
|
return errors.Errorf("expected a full content change, received incremental changes for %s", uri)
|
|
case protocol.Incremental:
|
|
// Determine the new file content.
|
|
var err error
|
|
text, err = s.applyChanges(ctx, uri, params.ContentChanges)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
}
|
|
// Cache the new file content and send fresh diagnostics.
|
|
view, err := s.session.ViewOf(uri)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
view.SetContent(ctx, uri, params.TextDocument.Version, []byte(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(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,
|
|
})
|
|
}
|
|
// Run diagnostics on the newly-changed file.
|
|
go s.diagnoseFile(view.Snapshot(), uri)
|
|
|
|
return nil
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
func fullChange(changes []protocol.TextDocumentContentChangeEvent) (string, bool) {
|
|
if len(changes) > 1 {
|
|
return "", false
|
|
}
|
|
// The length of the changes must be 1 at this point.
|
|
// TODO: This breaks if you insert a character at the beginning of the file.
|
|
if changes[0].Range == nil && changes[0].RangeLength == 0 {
|
|
return changes[0].Text, true
|
|
}
|
|
|
|
return "", false
|
|
}
|
|
|
|
func (s *Server) applyChanges(ctx context.Context, uri span.URI, changes []protocol.TextDocumentContentChangeEvent) (string, error) {
|
|
content, _, err := s.session.GetFile(uri, source.UnknownKind).Read(ctx)
|
|
if err != nil {
|
|
return "", jsonrpc2.NewErrorf(jsonrpc2.CodeInternalError, "file not found (%v)", err)
|
|
}
|
|
for _, change := range changes {
|
|
// Update column mapper along with the content.
|
|
converter := span.NewContentConverter(uri.Filename(), content)
|
|
m := &protocol.ColumnMapper{
|
|
URI: uri,
|
|
Converter: converter,
|
|
Content: content,
|
|
}
|
|
|
|
spn, err := m.RangeSpan(*change.Range) // Could Range be nil here?
|
|
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 {
|
|
s.session.DidSave(span.NewURI(params.TextDocument.URI), params.TextDocument.Version)
|
|
return nil
|
|
}
|
|
|
|
func (s *Server) didClose(ctx context.Context, params *protocol.DidCloseTextDocumentParams) error {
|
|
uri := span.NewURI(params.TextDocument.URI)
|
|
ctx = telemetry.URI.With(ctx, uri)
|
|
s.session.DidClose(uri)
|
|
view, err := s.session.ViewOf(uri)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
view.SetContent(ctx, uri, -1, nil)
|
|
return nil
|
|
}
|