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.
|
|
|
|
|
|
|
|
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-02-19 19:11:15 -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-05-15 10:24:49 -06:00
|
|
|
client: client,
|
2019-07-10 19:01:12 -06:00
|
|
|
session: cache.NewSession(ctx),
|
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-03-29 12:16:59 -06:00
|
|
|
s := &Server{}
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// 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 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
|
|
|
|
|
|
|
initializedMu sync.Mutex
|
2019-07-27 12:59:14 -06:00
|
|
|
state serverState
|
2018-09-27 09:28:20 -06:00
|
|
|
|
2019-04-04 17:33:08 -06:00
|
|
|
// Configurations.
|
|
|
|
// TODO(rstambler): Separate these into their own struct?
|
|
|
|
usePlaceholders bool
|
2019-07-02 16:37:59 -06:00
|
|
|
hoverKind source.HoverKind
|
2019-06-27 11:50:01 -06:00
|
|
|
useDeepCompletions bool
|
2019-07-02 15:31:31 -06:00
|
|
|
wantCompletionDocumentation bool
|
2019-04-17 12:31:17 -06:00
|
|
|
insertTextFormat protocol.InsertTextFormat
|
2019-04-01 12:56:49 -06:00
|
|
|
configurationSupported bool
|
|
|
|
dynamicConfigurationSupported bool
|
2019-04-17 12:47:10 -06:00
|
|
|
preferredContentFormat protocol.MarkupKind
|
2019-05-31 21:31:58 -06:00
|
|
|
disabledAnalyses map[string]struct{}
|
2019-06-27 19:26:42 -06:00
|
|
|
wantSuggestedFixes bool
|
|
|
|
|
|
|
|
supportedCodeActions map[protocol.CodeActionKind]bool
|
2018-11-20 14:05:10 -07:00
|
|
|
|
2019-03-04 15:50:16 -07:00
|
|
|
textDocumentSyncKind protocol.TextDocumentSyncKind
|
|
|
|
|
2019-05-15 10:24:49 -06:00
|
|
|
session source.Session
|
2019-03-14 15:19:01 -06:00
|
|
|
|
|
|
|
// undelivered is a cache of any diagnostics that the server
|
|
|
|
// failed to deliver for some reason.
|
|
|
|
undeliveredMu sync.Mutex
|
|
|
|
undelivered map[span.URI][]source.Diagnostic
|
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
|
|
|
|
|
|
|
func (s *Server) Initialize(ctx context.Context, params *protocol.InitializeParams) (*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-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-03-28 19:06:01 -06:00
|
|
|
func (s *Server) DidChangeConfiguration(context.Context, *protocol.DidChangeConfigurationParams) error {
|
2018-09-24 16:05:51 -06:00
|
|
|
return notImplemented("DidChangeConfiguration")
|
|
|
|
}
|
|
|
|
|
2019-03-28 19:06:01 -06:00
|
|
|
func (s *Server) DidChangeWatchedFiles(context.Context, *protocol.DidChangeWatchedFilesParams) error {
|
2018-09-24 16:05:51 -06:00
|
|
|
return notImplemented("DidChangeWatchedFiles")
|
|
|
|
}
|
|
|
|
|
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-03-28 19:06:01 -06:00
|
|
|
func (s *Server) ExecuteCommand(context.Context, *protocol.ExecuteCommandParams) (interface{}, error) {
|
2018-09-24 16:05:51 -06:00
|
|
|
return nil, notImplemented("ExecuteCommand")
|
|
|
|
}
|
|
|
|
|
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-03-28 19:06:01 -06:00
|
|
|
func (s *Server) Hover(ctx context.Context, params *protocol.TextDocumentPositionParams) (*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-03-28 19:06:01 -06:00
|
|
|
func (s *Server) SignatureHelp(ctx context.Context, params *protocol.TextDocumentPositionParams) (*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-03-28 19:06:01 -06:00
|
|
|
func (s *Server) Definition(ctx context.Context, params *protocol.TextDocumentPositionParams) ([]protocol.Location, error) {
|
2019-04-17 13:37:20 -06:00
|
|
|
return s.definition(ctx, params)
|
2018-09-24 16:05:51 -06:00
|
|
|
}
|
|
|
|
|
2019-03-28 19:06:01 -06:00
|
|
|
func (s *Server) TypeDefinition(ctx context.Context, params *protocol.TextDocumentPositionParams) ([]protocol.Location, error) {
|
2019-04-17 13:37:20 -06:00
|
|
|
return s.typeDefinition(ctx, params)
|
2018-09-24 16:05:51 -06:00
|
|
|
}
|
|
|
|
|
2019-03-28 19:06:01 -06:00
|
|
|
func (s *Server) Implementation(context.Context, *protocol.TextDocumentPositionParams) ([]protocol.Location, error) {
|
2018-09-24 16:05:51 -06:00
|
|
|
return nil, notImplemented("Implementation")
|
|
|
|
}
|
|
|
|
|
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-03-28 19:06:01 -06:00
|
|
|
func (s *Server) DocumentHighlight(ctx context.Context, params *protocol.TextDocumentPositionParams) ([]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-03-28 19:06:01 -06: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-04-23 08:05:23 -06:00
|
|
|
func (s *Server) Declaration(context.Context, *protocol.TextDocumentPositionParams) ([]protocol.DeclarationLink, error) {
|
|
|
|
return nil, notImplemented("Declaration")
|
|
|
|
}
|
|
|
|
|
2019-04-22 03:59:53 -06:00
|
|
|
func (s *Server) FoldingRange(context.Context, *protocol.FoldingRangeParams) ([]protocol.FoldingRange, error) {
|
|
|
|
return nil, notImplemented("FoldingRange")
|
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")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) PrepareRename(context.Context, *protocol.TextDocumentPositionParams) (*protocol.Range, error) {
|
|
|
|
return nil, notImplemented("PrepareRename")
|
|
|
|
}
|
|
|
|
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|