mirror of
https://github.com/golang/go
synced 2024-11-05 17:36:15 -07:00
4d14fc9c00
This change is the first step in handling golang/go#38136. Instead of creating multiple diagnostic reports for type error analyzers, we add suggested fixes to the existing reports. To match the analyzers for FindAnalysisError, we add an ErrorMatch function to source.Analyzer. This is not an ideal solution, but it was the best one I could come up with without modifying the go/analysis API. analysisinternal could be used for this purpose, but it seemed to complicated to be worth it, and this is fairly simple. I think that go/analysis itself might need to be extended for type error analyzers, but these temporary measures will help us understand the kinds of features we need for type error analyzers. A follow-up CL might be to not add reports for type error analyzers until the end of source.Diagnostic, which would remove the need for the look-up. Fixes golang/go#38136 Change-Id: I25bc6396b09d49facecd918bf5591d2d5bdf1b3a Reviewed-on: https://go-review.googlesource.com/c/tools/+/226777 Run-TryBot: Rebecca Stambler <rstambler@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Heschi Kreinick <heschi@google.com>
168 lines
4.8 KiB
Go
168 lines
4.8 KiB
Go
// Copyright 2018 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 implements LSP for gopls.
|
|
package lsp
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"sync"
|
|
|
|
"golang.org/x/tools/internal/jsonrpc2"
|
|
"golang.org/x/tools/internal/lsp/mod"
|
|
"golang.org/x/tools/internal/lsp/protocol"
|
|
"golang.org/x/tools/internal/lsp/source"
|
|
"golang.org/x/tools/internal/span"
|
|
errors "golang.org/x/xerrors"
|
|
)
|
|
|
|
const concurrentAnalyses = 1
|
|
|
|
// NewServer creates an LSP server and binds it to handle incoming client
|
|
// messages on on the supplied stream.
|
|
func NewServer(session source.Session, client protocol.Client) *Server {
|
|
return &Server{
|
|
delivered: make(map[span.URI]sentDiagnostics),
|
|
session: session,
|
|
client: client,
|
|
diagnosticsSema: make(chan struct{}, concurrentAnalyses),
|
|
}
|
|
}
|
|
|
|
type serverState int
|
|
|
|
const (
|
|
serverCreated = serverState(iota)
|
|
serverInitializing // set once the server has received "initialize" request
|
|
serverInitialized // set once the server has received "initialized" request
|
|
serverShutDown
|
|
)
|
|
|
|
func (s serverState) String() string {
|
|
switch s {
|
|
case serverCreated:
|
|
return "created"
|
|
case serverInitializing:
|
|
return "initializing"
|
|
case serverInitialized:
|
|
return "initialized"
|
|
case serverShutDown:
|
|
return "shutDown"
|
|
}
|
|
return fmt.Sprintf("(unknown state: %d)", int(s))
|
|
}
|
|
|
|
// Server implements the protocol.Server interface.
|
|
type Server struct {
|
|
client protocol.Client
|
|
|
|
stateMu sync.Mutex
|
|
state serverState
|
|
|
|
session source.Session
|
|
|
|
// changedFiles tracks files for which there has been a textDocument/didChange.
|
|
changedFiles map[span.URI]struct{}
|
|
|
|
// folders is only valid between initialize and initialized, and holds the
|
|
// set of folders to build views for when we are ready
|
|
pendingFolders []protocol.WorkspaceFolder
|
|
|
|
// delivered is a cache of the diagnostics that the server has sent.
|
|
deliveredMu sync.Mutex
|
|
delivered map[span.URI]sentDiagnostics
|
|
|
|
// diagnosticsSema limits the concurrency of diagnostics runs, which can be expensive.
|
|
diagnosticsSema chan struct{}
|
|
|
|
// supportsWorkDoneProgress is set in the initializeRequest
|
|
// to determine if the client can support progress notifications
|
|
supportsWorkDoneProgress bool
|
|
inProgressMu sync.Mutex
|
|
inProgress map[string]func()
|
|
}
|
|
|
|
// sentDiagnostics is used to cache diagnostics that have been sent for a given file.
|
|
type sentDiagnostics struct {
|
|
version float64
|
|
identifier string
|
|
sorted []*source.Diagnostic
|
|
withAnalysis bool
|
|
snapshotID uint64
|
|
}
|
|
|
|
func (s *Server) codeLens(ctx context.Context, params *protocol.CodeLensParams) ([]protocol.CodeLens, error) {
|
|
snapshot, fh, ok, err := s.beginFileRequest(params.TextDocument.URI, source.UnknownKind)
|
|
if !ok {
|
|
return nil, err
|
|
}
|
|
switch fh.Identity().Kind {
|
|
case source.Mod:
|
|
return mod.CodeLens(ctx, snapshot, fh.Identity().URI)
|
|
case source.Go:
|
|
return source.CodeLens(ctx, snapshot, fh)
|
|
}
|
|
// Unsupported file kind for a code action.
|
|
return nil, nil
|
|
}
|
|
|
|
func (s *Server) nonstandardRequest(ctx context.Context, method string, params interface{}) (interface{}, error) {
|
|
paramMap := params.(map[string]interface{})
|
|
if method == "gopls/diagnoseFiles" {
|
|
for _, file := range paramMap["files"].([]interface{}) {
|
|
snapshot, fh, ok, err := s.beginFileRequest(protocol.DocumentURI(file.(string)), source.UnknownKind)
|
|
if !ok {
|
|
return nil, err
|
|
}
|
|
|
|
fileID, diagnostics, err := source.FileDiagnostics(ctx, snapshot, fh.Identity().URI)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if err := s.client.PublishDiagnostics(ctx, &protocol.PublishDiagnosticsParams{
|
|
URI: protocol.URIFromSpanURI(fh.Identity().URI),
|
|
Diagnostics: toProtocolDiagnostics(diagnostics),
|
|
Version: fileID.Version,
|
|
}); err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
if err := s.client.PublishDiagnostics(ctx, &protocol.PublishDiagnosticsParams{
|
|
URI: "gopls://diagnostics-done",
|
|
}); err != nil {
|
|
return nil, err
|
|
}
|
|
return struct{}{}, nil
|
|
}
|
|
return nil, notImplemented(method)
|
|
}
|
|
|
|
func (s *Server) workDoneProgressCancel(ctx context.Context, params *protocol.WorkDoneProgressCancelParams) error {
|
|
token, ok := params.Token.(string)
|
|
if !ok {
|
|
return errors.Errorf("expected params.Token to be string but got %T", params.Token)
|
|
}
|
|
s.inProgressMu.Lock()
|
|
defer s.inProgressMu.Unlock()
|
|
cancel, ok := s.inProgress[token]
|
|
if !ok {
|
|
return errors.Errorf("token %q not found in progress", token)
|
|
}
|
|
cancel()
|
|
return nil
|
|
}
|
|
|
|
func (s *Server) clearInProgress(token string) {
|
|
s.inProgressMu.Lock()
|
|
delete(s.inProgress, token)
|
|
s.inProgressMu.Unlock()
|
|
}
|
|
|
|
func notImplemented(method string) *jsonrpc2.Error {
|
|
return jsonrpc2.NewErrorf(jsonrpc2.CodeMethodNotFound, "method %q not yet implemented", method)
|
|
}
|
|
|
|
//go:generate helper/helper -d protocol/tsserver.go -o server_gen.go -u .
|