2018-09-24 16:05:51 -06:00
|
|
|
// 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.
|
|
|
|
|
2019-09-27 11:17:59 -06:00
|
|
|
// Package lsp implements LSP for gopls.
|
2018-09-24 16:05:51 -06:00
|
|
|
package lsp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-01-10 18:42:25 -07:00
|
|
|
"fmt"
|
|
|
|
"net"
|
2018-09-27 09:28:20 -06:00
|
|
|
"sync"
|
2018-09-24 16:05:51 -06:00
|
|
|
|
|
|
|
"golang.org/x/tools/internal/jsonrpc2"
|
|
|
|
"golang.org/x/tools/internal/lsp/protocol"
|
2018-11-02 14:15:31 -06:00
|
|
|
"golang.org/x/tools/internal/lsp/source"
|
2019-11-21 22:52:09 -07:00
|
|
|
"golang.org/x/tools/internal/span"
|
2018-09-24 16:05:51 -06:00
|
|
|
)
|
|
|
|
|
2019-03-28 19:06:01 -06:00
|
|
|
// NewClientServer
|
2019-07-10 19:01:12 -06:00
|
|
|
func NewClientServer(ctx context.Context, cache source.Cache, client protocol.Client) (context.Context, *Server) {
|
2019-07-10 13:19:29 -06:00
|
|
|
ctx = protocol.WithClient(ctx, client)
|
2019-07-10 19:01:12 -06:00
|
|
|
return ctx, &Server{
|
2019-11-20 23:24:43 -07:00
|
|
|
client: client,
|
|
|
|
session: cache.NewSession(ctx),
|
|
|
|
delivered: make(map[span.URI]sentDiagnostics),
|
2019-02-08 14:16:57 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-28 19:06:01 -06:00
|
|
|
// NewServer starts an LSP server on the supplied stream, and waits until the
|
2018-09-24 16:05:51 -06:00
|
|
|
// stream is closed.
|
2019-07-10 19:01:12 -06:00
|
|
|
func NewServer(ctx context.Context, cache source.Cache, stream jsonrpc2.Stream) (context.Context, *Server) {
|
2019-11-20 23:24:43 -07:00
|
|
|
s := &Server{
|
|
|
|
delivered: make(map[span.URI]sentDiagnostics),
|
|
|
|
}
|
2019-07-10 19:01:12 -06:00
|
|
|
ctx, s.Conn, s.client = protocol.NewServer(ctx, stream, s)
|
|
|
|
s.session = cache.NewSession(ctx)
|
|
|
|
return ctx, s
|
2018-09-24 16:05:51 -06:00
|
|
|
}
|
|
|
|
|
2019-01-10 18:42:25 -07:00
|
|
|
// RunServerOnPort starts an LSP server on the given port and does not exit.
|
|
|
|
// This function exists for debugging purposes.
|
2019-07-10 19:01:12 -06:00
|
|
|
func RunServerOnPort(ctx context.Context, cache source.Cache, port int, h func(ctx context.Context, s *Server)) error {
|
2019-05-15 10:24:49 -06:00
|
|
|
return RunServerOnAddress(ctx, cache, fmt.Sprintf(":%v", port), h)
|
2019-02-07 15:05:20 -07:00
|
|
|
}
|
|
|
|
|
2020-01-14 16:48:43 -07:00
|
|
|
// RunServerOnAddress starts an LSP server on the given address and does not
|
|
|
|
// exit. This function exists for debugging purposes.
|
2019-07-10 19:01:12 -06:00
|
|
|
func RunServerOnAddress(ctx context.Context, cache source.Cache, addr string, h func(ctx context.Context, s *Server)) error {
|
2019-02-07 15:05:20 -07:00
|
|
|
ln, err := net.Listen("tcp", addr)
|
2019-01-10 18:42:25 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for {
|
|
|
|
conn, err := ln.Accept()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-07-10 19:01:12 -06:00
|
|
|
h(NewServer(ctx, cache, jsonrpc2.NewHeaderStream(conn, conn)))
|
2019-01-10 18:42:25 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-17 13:37:20 -06:00
|
|
|
func (s *Server) Run(ctx context.Context) error {
|
|
|
|
return s.Conn.Run(ctx)
|
|
|
|
}
|
|
|
|
|
2019-07-27 12:59:14 -06:00
|
|
|
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
|
|
|
|
)
|
|
|
|
|
2019-03-28 19:06:01 -06:00
|
|
|
type Server struct {
|
|
|
|
Conn *jsonrpc2.Conn
|
2018-09-24 16:05:51 -06:00
|
|
|
client protocol.Client
|
2018-09-27 09:28:20 -06:00
|
|
|
|
2019-07-29 21:48:11 -06:00
|
|
|
stateMu sync.Mutex
|
|
|
|
state serverState
|
2018-09-27 09:28:20 -06:00
|
|
|
|
2019-05-15 10:24:49 -06:00
|
|
|
session source.Session
|
2019-03-14 15:19:01 -06:00
|
|
|
|
2019-11-21 22:52:09 -07:00
|
|
|
// changedFiles tracks files for which there has been a textDocument/didChange.
|
|
|
|
changedFiles map[span.URI]struct{}
|
|
|
|
|
2019-09-09 11:04:12 -06:00
|
|
|
// 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
|
2019-11-20 23:24:43 -07:00
|
|
|
|
|
|
|
// delivered is a cache of the diagnostics that the server has sent.
|
|
|
|
deliveredMu sync.Mutex
|
|
|
|
delivered map[span.URI]sentDiagnostics
|
|
|
|
}
|
|
|
|
|
|
|
|
// sentDiagnostics is used to cache diagnostics that have been sent for a given file.
|
|
|
|
type sentDiagnostics struct {
|
2020-01-13 18:41:03 -07:00
|
|
|
version float64
|
|
|
|
identifier string
|
|
|
|
sorted []source.Diagnostic
|
|
|
|
withAnalysis bool
|
|
|
|
snapshotID uint64
|
2018-09-27 09:28:20 -06:00
|
|
|
}
|
|
|
|
|
2019-04-17 13:37:20 -06:00
|
|
|
// General
|
2019-03-28 19:06:01 -06:00
|
|
|
|
2019-11-17 12:29:15 -07:00
|
|
|
func (s *Server) Initialize(ctx context.Context, params *protocol.ParamInitialize) (*protocol.InitializeResult, error) {
|
2019-04-17 13:37:20 -06:00
|
|
|
return s.initialize(ctx, params)
|
2019-04-04 17:33:08 -06:00
|
|
|
}
|
|
|
|
|
2019-03-28 19:06:01 -06:00
|
|
|
func (s *Server) Initialized(ctx context.Context, params *protocol.InitializedParams) error {
|
2019-04-17 13:37:20 -06:00
|
|
|
return s.initialized(ctx, params)
|
2018-09-24 16:05:51 -06:00
|
|
|
}
|
|
|
|
|
2019-04-17 13:37:20 -06:00
|
|
|
func (s *Server) Shutdown(ctx context.Context) error {
|
|
|
|
return s.shutdown(ctx)
|
2018-09-24 16:05:51 -06:00
|
|
|
}
|
|
|
|
|
2019-03-28 19:06:01 -06:00
|
|
|
func (s *Server) Exit(ctx context.Context) error {
|
2019-04-17 13:37:20 -06:00
|
|
|
return s.exit(ctx)
|
2018-09-24 16:05:51 -06:00
|
|
|
}
|
|
|
|
|
2019-11-17 12:29:15 -07:00
|
|
|
func (s *Server) CancelRequest(ctx context.Context, params *protocol.CancelParams) error {
|
2019-11-18 14:11:56 -07:00
|
|
|
return nil
|
2019-11-17 12:29:15 -07:00
|
|
|
}
|
|
|
|
|
2019-04-17 13:37:20 -06:00
|
|
|
// Workspace
|
|
|
|
|
2019-05-06 14:44:43 -06:00
|
|
|
func (s *Server) DidChangeWorkspaceFolders(ctx context.Context, params *protocol.DidChangeWorkspaceFoldersParams) error {
|
|
|
|
return s.changeFolders(ctx, params.Event)
|
2018-09-24 16:05:51 -06:00
|
|
|
}
|
|
|
|
|
2019-11-08 11:26:32 -07:00
|
|
|
func (s *Server) DidChangeConfiguration(ctx context.Context, params *protocol.DidChangeConfigurationParams) error {
|
|
|
|
return s.updateConfiguration(ctx, params.Settings)
|
2018-09-24 16:05:51 -06:00
|
|
|
}
|
|
|
|
|
2019-08-19 10:53:51 -06:00
|
|
|
func (s *Server) DidChangeWatchedFiles(ctx context.Context, params *protocol.DidChangeWatchedFilesParams) error {
|
|
|
|
return s.didChangeWatchedFiles(ctx, params)
|
2018-09-24 16:05:51 -06:00
|
|
|
}
|
|
|
|
|
2019-04-17 13:37:20 -06:00
|
|
|
func (s *Server) Symbol(context.Context, *protocol.WorkspaceSymbolParams) ([]protocol.SymbolInformation, error) {
|
|
|
|
return nil, notImplemented("Symbol")
|
2018-09-24 16:05:51 -06:00
|
|
|
}
|
|
|
|
|
2019-09-18 23:21:54 -06:00
|
|
|
func (s *Server) ExecuteCommand(ctx context.Context, params *protocol.ExecuteCommandParams) (interface{}, error) {
|
|
|
|
return s.executeCommand(ctx, params)
|
2018-09-24 16:05:51 -06:00
|
|
|
}
|
|
|
|
|
2019-04-17 13:37:20 -06:00
|
|
|
// Text Synchronization
|
|
|
|
|
2019-03-28 19:06:01 -06:00
|
|
|
func (s *Server) DidOpen(ctx context.Context, params *protocol.DidOpenTextDocumentParams) error {
|
2019-05-14 14:23:46 -06:00
|
|
|
return s.didOpen(ctx, params)
|
2019-02-17 23:00:10 -07:00
|
|
|
}
|
|
|
|
|
2019-03-28 19:06:01 -06:00
|
|
|
func (s *Server) DidChange(ctx context.Context, params *protocol.DidChangeTextDocumentParams) error {
|
2019-04-17 13:37:20 -06:00
|
|
|
return s.didChange(ctx, params)
|
2018-09-24 16:05:51 -06:00
|
|
|
}
|
|
|
|
|
2019-03-28 19:06:01 -06:00
|
|
|
func (s *Server) WillSave(context.Context, *protocol.WillSaveTextDocumentParams) error {
|
2018-09-24 16:05:51 -06:00
|
|
|
return notImplemented("WillSave")
|
|
|
|
}
|
|
|
|
|
2019-03-28 19:06:01 -06:00
|
|
|
func (s *Server) WillSaveWaitUntil(context.Context, *protocol.WillSaveTextDocumentParams) ([]protocol.TextEdit, error) {
|
2018-09-24 16:05:51 -06:00
|
|
|
return nil, notImplemented("WillSaveWaitUntil")
|
|
|
|
}
|
|
|
|
|
2019-04-17 13:37:20 -06:00
|
|
|
func (s *Server) DidSave(ctx context.Context, params *protocol.DidSaveTextDocumentParams) error {
|
|
|
|
return s.didSave(ctx, params)
|
2018-09-24 16:05:51 -06:00
|
|
|
}
|
|
|
|
|
2019-03-28 19:06:01 -06:00
|
|
|
func (s *Server) DidClose(ctx context.Context, params *protocol.DidCloseTextDocumentParams) error {
|
2019-04-17 13:37:20 -06:00
|
|
|
return s.didClose(ctx, params)
|
2018-09-24 16:05:51 -06:00
|
|
|
}
|
|
|
|
|
2019-04-17 13:37:20 -06:00
|
|
|
// Language Features
|
|
|
|
|
2019-03-28 19:06:01 -06:00
|
|
|
func (s *Server) Completion(ctx context.Context, params *protocol.CompletionParams) (*protocol.CompletionList, error) {
|
2019-04-04 17:33:08 -06:00
|
|
|
return s.completion(ctx, params)
|
2018-09-24 16:05:51 -06:00
|
|
|
}
|
|
|
|
|
2019-07-02 15:31:31 -06:00
|
|
|
func (s *Server) Resolve(ctx context.Context, item *protocol.CompletionItem) (*protocol.CompletionItem, error) {
|
|
|
|
return nil, notImplemented("completionItem/resolve")
|
2018-09-24 16:05:51 -06:00
|
|
|
}
|
|
|
|
|
2019-09-07 15:01:26 -06:00
|
|
|
func (s *Server) Hover(ctx context.Context, params *protocol.HoverParams) (*protocol.Hover, error) {
|
2019-04-17 13:37:20 -06:00
|
|
|
return s.hover(ctx, params)
|
2018-09-24 16:05:51 -06:00
|
|
|
}
|
|
|
|
|
2019-09-07 15:01:26 -06:00
|
|
|
func (s *Server) SignatureHelp(ctx context.Context, params *protocol.SignatureHelpParams) (*protocol.SignatureHelp, error) {
|
2019-04-17 13:37:20 -06:00
|
|
|
return s.signatureHelp(ctx, params)
|
2018-09-24 16:05:51 -06:00
|
|
|
}
|
|
|
|
|
2019-11-17 12:29:15 -07:00
|
|
|
func (s *Server) Definition(ctx context.Context, params *protocol.DefinitionParams) (protocol.Definition, error) {
|
2019-04-17 13:37:20 -06:00
|
|
|
return s.definition(ctx, params)
|
2018-09-24 16:05:51 -06:00
|
|
|
}
|
|
|
|
|
2019-11-17 12:29:15 -07:00
|
|
|
func (s *Server) TypeDefinition(ctx context.Context, params *protocol.TypeDefinitionParams) (protocol.Definition, error) {
|
2019-04-17 13:37:20 -06:00
|
|
|
return s.typeDefinition(ctx, params)
|
2018-09-24 16:05:51 -06:00
|
|
|
}
|
|
|
|
|
2019-11-17 12:29:15 -07:00
|
|
|
func (s *Server) Implementation(ctx context.Context, params *protocol.ImplementationParams) (protocol.Definition, error) {
|
2019-10-28 13:16:55 -06:00
|
|
|
return s.implementation(ctx, params)
|
2018-09-24 16:05:51 -06:00
|
|
|
}
|
|
|
|
|
2019-06-07 08:04:22 -06:00
|
|
|
func (s *Server) References(ctx context.Context, params *protocol.ReferenceParams) ([]protocol.Location, error) {
|
|
|
|
return s.references(ctx, params)
|
2018-09-24 16:05:51 -06:00
|
|
|
}
|
|
|
|
|
2019-09-07 15:01:26 -06:00
|
|
|
func (s *Server) DocumentHighlight(ctx context.Context, params *protocol.DocumentHighlightParams) ([]protocol.DocumentHighlight, error) {
|
2019-04-17 13:37:20 -06:00
|
|
|
return s.documentHighlight(ctx, params)
|
2018-09-24 16:05:51 -06:00
|
|
|
}
|
|
|
|
|
2019-03-28 19:06:01 -06:00
|
|
|
func (s *Server) DocumentSymbol(ctx context.Context, params *protocol.DocumentSymbolParams) ([]protocol.DocumentSymbol, error) {
|
2019-04-17 13:37:20 -06:00
|
|
|
return s.documentSymbol(ctx, params)
|
2018-09-24 16:05:51 -06:00
|
|
|
}
|
|
|
|
|
2019-11-19 06:42:45 -07:00
|
|
|
func (s *Server) CodeAction(ctx context.Context, params *protocol.CodeActionParams) ([]protocol.CodeAction, error) {
|
2019-04-05 13:56:08 -06:00
|
|
|
return s.codeAction(ctx, params)
|
2018-09-24 16:05:51 -06:00
|
|
|
}
|
|
|
|
|
2019-03-28 19:06:01 -06:00
|
|
|
func (s *Server) CodeLens(context.Context, *protocol.CodeLensParams) ([]protocol.CodeLens, error) {
|
2018-10-19 14:03:29 -06:00
|
|
|
return nil, nil // ignore
|
2018-09-24 16:05:51 -06:00
|
|
|
}
|
|
|
|
|
2019-04-23 08:05:23 -06:00
|
|
|
func (s *Server) ResolveCodeLens(context.Context, *protocol.CodeLens) (*protocol.CodeLens, error) {
|
|
|
|
return nil, notImplemented("ResolveCodeLens")
|
2018-09-24 16:05:51 -06:00
|
|
|
}
|
|
|
|
|
2019-04-24 09:33:45 -06:00
|
|
|
func (s *Server) DocumentLink(ctx context.Context, params *protocol.DocumentLinkParams) ([]protocol.DocumentLink, error) {
|
|
|
|
return s.documentLink(ctx, params)
|
2018-09-24 16:05:51 -06:00
|
|
|
}
|
|
|
|
|
2019-04-23 08:05:23 -06:00
|
|
|
func (s *Server) ResolveDocumentLink(context.Context, *protocol.DocumentLink) (*protocol.DocumentLink, error) {
|
|
|
|
return nil, notImplemented("ResolveDocumentLink")
|
2018-09-24 16:05:51 -06:00
|
|
|
}
|
|
|
|
|
2019-03-28 19:06:01 -06:00
|
|
|
func (s *Server) DocumentColor(context.Context, *protocol.DocumentColorParams) ([]protocol.ColorInformation, error) {
|
2018-09-24 16:05:51 -06:00
|
|
|
return nil, notImplemented("DocumentColor")
|
|
|
|
}
|
|
|
|
|
2019-03-28 19:06:01 -06:00
|
|
|
func (s *Server) ColorPresentation(context.Context, *protocol.ColorPresentationParams) ([]protocol.ColorPresentation, error) {
|
2018-09-24 16:05:51 -06:00
|
|
|
return nil, notImplemented("ColorPresentation")
|
|
|
|
}
|
|
|
|
|
2019-03-28 19:06:01 -06:00
|
|
|
func (s *Server) Formatting(ctx context.Context, params *protocol.DocumentFormattingParams) ([]protocol.TextEdit, error) {
|
2019-04-17 13:37:20 -06:00
|
|
|
return s.formatting(ctx, params)
|
2018-09-24 16:05:51 -06:00
|
|
|
}
|
|
|
|
|
2019-03-28 19:06:01 -06:00
|
|
|
func (s *Server) RangeFormatting(ctx context.Context, params *protocol.DocumentRangeFormattingParams) ([]protocol.TextEdit, error) {
|
2019-05-08 03:44:01 -06:00
|
|
|
return nil, notImplemented("RangeFormatting")
|
2018-11-07 10:58:55 -07:00
|
|
|
}
|
|
|
|
|
2019-03-28 19:06:01 -06:00
|
|
|
func (s *Server) OnTypeFormatting(context.Context, *protocol.DocumentOnTypeFormattingParams) ([]protocol.TextEdit, error) {
|
2018-09-24 16:05:51 -06:00
|
|
|
return nil, notImplemented("OnTypeFormatting")
|
|
|
|
}
|
|
|
|
|
2019-06-18 08:23:37 -06:00
|
|
|
func (s *Server) Rename(ctx context.Context, params *protocol.RenameParams) (*protocol.WorkspaceEdit, error) {
|
|
|
|
return s.rename(ctx, params)
|
2018-09-24 16:05:51 -06:00
|
|
|
}
|
|
|
|
|
2019-11-17 12:29:15 -07:00
|
|
|
func (s *Server) Declaration(context.Context, *protocol.DeclarationParams) (protocol.Declaration, error) {
|
2019-04-23 08:05:23 -06:00
|
|
|
return nil, notImplemented("Declaration")
|
|
|
|
}
|
|
|
|
|
2019-08-28 19:48:29 -06:00
|
|
|
func (s *Server) FoldingRange(ctx context.Context, params *protocol.FoldingRangeParams) ([]protocol.FoldingRange, error) {
|
|
|
|
return s.foldingRange(ctx, params)
|
2018-09-24 16:05:51 -06:00
|
|
|
}
|
2018-09-27 09:28:20 -06:00
|
|
|
|
2019-04-23 08:05:23 -06:00
|
|
|
func (s *Server) LogTraceNotification(context.Context, *protocol.LogTraceParams) error {
|
|
|
|
return notImplemented("LogtraceNotification")
|
|
|
|
}
|
|
|
|
|
2019-11-17 12:29:15 -07:00
|
|
|
func (s *Server) PrepareRename(ctx context.Context, params *protocol.PrepareRenameParams) (interface{}, error) {
|
2019-08-22 11:31:03 -06:00
|
|
|
// TODO(suzmue): support sending placeholder text.
|
|
|
|
return s.prepareRename(ctx, params)
|
2019-04-23 08:05:23 -06:00
|
|
|
}
|
|
|
|
|
2019-09-07 15:01:26 -06:00
|
|
|
func (s *Server) Progress(context.Context, *protocol.ProgressParams) error {
|
|
|
|
return notImplemented("Progress")
|
|
|
|
}
|
|
|
|
|
2019-04-23 08:05:23 -06:00
|
|
|
func (s *Server) SetTraceNotification(context.Context, *protocol.SetTraceParams) error {
|
|
|
|
return notImplemented("SetTraceNotification")
|
|
|
|
}
|
2019-07-11 12:53:13 -06:00
|
|
|
|
|
|
|
func (s *Server) SelectionRange(context.Context, *protocol.SelectionRangeParams) ([]protocol.SelectionRange, error) {
|
|
|
|
return nil, notImplemented("SelectionRange")
|
|
|
|
}
|
|
|
|
|
2020-01-10 15:35:46 -07:00
|
|
|
// Nonstandard requests
|
|
|
|
func (s *Server) NonstandardRequest(ctx context.Context, method string, params interface{}) (interface{}, error) {
|
2020-01-10 15:54:47 -07:00
|
|
|
paramMap := params.(map[string]interface{})
|
|
|
|
if method == "gopls/diagnoseFiles" {
|
|
|
|
files := paramMap["files"].([]interface{})
|
|
|
|
for _, file := range files {
|
|
|
|
uri := span.URI(file.(string))
|
|
|
|
view, err := s.session.ViewOf(uri)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
s.diagnoseFile(view.Snapshot(), view.Session().GetFile(span.URI(uri)))
|
|
|
|
}
|
|
|
|
if err := s.client.PublishDiagnostics(ctx, &protocol.PublishDiagnosticsParams{
|
|
|
|
URI: "gopls://diagnostics-done",
|
|
|
|
}); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return struct{}{}, nil
|
|
|
|
}
|
2020-01-10 15:35:46 -07:00
|
|
|
return nil, notImplemented(method)
|
|
|
|
}
|
|
|
|
|
2018-09-27 09:28:20 -06:00
|
|
|
func notImplemented(method string) *jsonrpc2.Error {
|
|
|
|
return jsonrpc2.NewErrorf(jsonrpc2.CodeMethodNotFound, "method %q not yet implemented", method)
|
|
|
|
}
|