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"
|
2018-11-07 10:58:55 -07:00
|
|
|
"go/token"
|
2018-09-27 09:28:20 -06:00
|
|
|
"os"
|
|
|
|
"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"
|
2018-09-24 16:05:51 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
// RunServer starts an LSP server on the supplied stream, and waits until the
|
|
|
|
// stream is closed.
|
|
|
|
func RunServer(ctx context.Context, stream jsonrpc2.Stream, opts ...interface{}) error {
|
2018-10-29 16:12:41 -06:00
|
|
|
s := &server{}
|
2018-09-24 16:05:51 -06:00
|
|
|
conn, client := protocol.RunServer(ctx, stream, s, opts...)
|
|
|
|
s.client = client
|
|
|
|
return conn.Wait(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
type server struct {
|
|
|
|
client protocol.Client
|
2018-09-27 09:28:20 -06:00
|
|
|
|
|
|
|
initializedMu sync.Mutex
|
|
|
|
initialized bool // set once the server has received "initialize" request
|
|
|
|
|
2018-11-20 14:05:10 -07:00
|
|
|
signatureHelpEnabled bool
|
|
|
|
snippetsSupported bool
|
|
|
|
|
2018-11-02 14:15:31 -06:00
|
|
|
view *source.View
|
2018-09-27 09:28:20 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *server) Initialize(ctx context.Context, params *protocol.InitializeParams) (*protocol.InitializeResult, error) {
|
|
|
|
s.initializedMu.Lock()
|
|
|
|
defer s.initializedMu.Unlock()
|
|
|
|
if s.initialized {
|
|
|
|
return nil, jsonrpc2.NewErrorf(jsonrpc2.CodeInvalidRequest, "server already initialized")
|
|
|
|
}
|
2018-11-02 14:15:31 -06:00
|
|
|
s.view = source.NewView()
|
2018-11-20 14:05:10 -07:00
|
|
|
s.initialized = true // mark server as initialized now
|
|
|
|
|
|
|
|
// Check if the client supports snippets in completion items.
|
|
|
|
s.snippetsSupported = params.Capabilities.TextDocument.Completion.CompletionItem.SnippetSupport
|
|
|
|
s.signatureHelpEnabled = true
|
|
|
|
|
2018-09-27 09:28:20 -06:00
|
|
|
return &protocol.InitializeResult{
|
|
|
|
Capabilities: protocol.ServerCapabilities{
|
2018-11-14 18:42:30 -07:00
|
|
|
CompletionProvider: protocol.CompletionOptions{
|
|
|
|
TriggerCharacters: []string{"."},
|
|
|
|
},
|
2018-11-12 14:53:10 -07:00
|
|
|
DefinitionProvider: true,
|
|
|
|
DocumentFormattingProvider: true,
|
|
|
|
DocumentRangeFormattingProvider: true,
|
2018-11-14 18:42:30 -07:00
|
|
|
SignatureHelpProvider: protocol.SignatureHelpOptions{
|
2018-11-20 16:18:33 -07:00
|
|
|
TriggerCharacters: []string{"(", ","},
|
2018-11-14 18:42:30 -07:00
|
|
|
},
|
2018-09-27 09:28:20 -06:00
|
|
|
TextDocumentSync: protocol.TextDocumentSyncOptions{
|
2018-09-27 16:15:45 -06:00
|
|
|
Change: float64(protocol.Full), // full contents of file sent on each update
|
|
|
|
OpenClose: true,
|
2018-09-27 09:28:20 -06:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}, nil
|
2018-09-24 16:05:51 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *server) Initialized(context.Context, *protocol.InitializedParams) error {
|
2018-09-27 09:28:20 -06:00
|
|
|
return nil // ignore
|
2018-09-24 16:05:51 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *server) Shutdown(context.Context) error {
|
2018-09-27 09:28:20 -06:00
|
|
|
s.initializedMu.Lock()
|
|
|
|
defer s.initializedMu.Unlock()
|
|
|
|
if !s.initialized {
|
|
|
|
return jsonrpc2.NewErrorf(jsonrpc2.CodeInvalidRequest, "server not initialized")
|
|
|
|
}
|
|
|
|
s.initialized = false
|
|
|
|
return nil
|
2018-09-24 16:05:51 -06:00
|
|
|
}
|
|
|
|
|
2018-09-27 09:28:20 -06:00
|
|
|
func (s *server) Exit(ctx context.Context) error {
|
|
|
|
if s.initialized {
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
os.Exit(0)
|
|
|
|
return nil
|
2018-09-24 16:05:51 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *server) DidChangeWorkspaceFolders(context.Context, *protocol.DidChangeWorkspaceFoldersParams) error {
|
|
|
|
return notImplemented("DidChangeWorkspaceFolders")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *server) DidChangeConfiguration(context.Context, *protocol.DidChangeConfigurationParams) error {
|
|
|
|
return notImplemented("DidChangeConfiguration")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *server) DidChangeWatchedFiles(context.Context, *protocol.DidChangeWatchedFilesParams) error {
|
|
|
|
return notImplemented("DidChangeWatchedFiles")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *server) Symbols(context.Context, *protocol.WorkspaceSymbolParams) ([]protocol.SymbolInformation, error) {
|
|
|
|
return nil, notImplemented("Symbols")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *server) ExecuteCommand(context.Context, *protocol.ExecuteCommandParams) (interface{}, error) {
|
|
|
|
return nil, notImplemented("ExecuteCommand")
|
|
|
|
}
|
|
|
|
|
2018-09-27 16:15:45 -06:00
|
|
|
func (s *server) DidOpen(ctx context.Context, params *protocol.DidOpenTextDocumentParams) error {
|
2018-10-19 14:03:29 -06:00
|
|
|
s.cacheAndDiagnoseFile(ctx, params.TextDocument.URI, params.TextDocument.Text)
|
2018-09-27 16:15:45 -06:00
|
|
|
return nil
|
2018-09-24 16:05:51 -06:00
|
|
|
}
|
|
|
|
|
2018-09-27 09:28:20 -06:00
|
|
|
func (s *server) DidChange(ctx context.Context, params *protocol.DidChangeTextDocumentParams) error {
|
2018-09-27 16:15:45 -06:00
|
|
|
if len(params.ContentChanges) < 1 {
|
|
|
|
return jsonrpc2.NewErrorf(jsonrpc2.CodeInternalError, "no content changes provided")
|
|
|
|
}
|
|
|
|
// We expect the full content of file, i.e. a single change with no range.
|
|
|
|
if change := params.ContentChanges[0]; change.RangeLength == 0 {
|
2018-10-19 14:03:29 -06:00
|
|
|
s.cacheAndDiagnoseFile(ctx, params.TextDocument.URI, change.Text)
|
2018-09-27 16:15:45 -06:00
|
|
|
}
|
2018-09-24 16:05:51 -06:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-10-19 14:03:29 -06:00
|
|
|
func (s *server) cacheAndDiagnoseFile(ctx context.Context, uri protocol.DocumentURI, text string) {
|
2018-11-05 15:54:12 -07:00
|
|
|
f := s.view.GetFile(source.URI(uri))
|
|
|
|
f.SetContent([]byte(text))
|
2018-10-19 14:03:29 -06:00
|
|
|
go func() {
|
2018-11-12 12:15:47 -07:00
|
|
|
f := s.view.GetFile(source.URI(uri))
|
|
|
|
reports, err := source.Diagnostics(ctx, s.view, f)
|
|
|
|
if err != nil {
|
|
|
|
return // handle error?
|
|
|
|
}
|
|
|
|
for filename, diagnostics := range reports {
|
|
|
|
s.client.PublishDiagnostics(ctx, &protocol.PublishDiagnosticsParams{
|
|
|
|
URI: protocol.DocumentURI(source.ToURI(filename)),
|
|
|
|
Diagnostics: toProtocolDiagnostics(s.view, diagnostics),
|
|
|
|
})
|
2018-10-19 14:03:29 -06:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2018-09-24 16:05:51 -06:00
|
|
|
func (s *server) WillSave(context.Context, *protocol.WillSaveTextDocumentParams) error {
|
|
|
|
return notImplemented("WillSave")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *server) WillSaveWaitUntil(context.Context, *protocol.WillSaveTextDocumentParams) ([]protocol.TextEdit, error) {
|
|
|
|
return nil, notImplemented("WillSaveWaitUntil")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *server) DidSave(context.Context, *protocol.DidSaveTextDocumentParams) error {
|
2018-10-19 14:03:29 -06:00
|
|
|
// TODO(rstambler): Should we clear the cache here?
|
|
|
|
return nil // ignore
|
2018-09-24 16:05:51 -06:00
|
|
|
}
|
|
|
|
|
2018-09-27 16:15:45 -06:00
|
|
|
func (s *server) DidClose(ctx context.Context, params *protocol.DidCloseTextDocumentParams) error {
|
2018-11-05 15:54:12 -07:00
|
|
|
s.view.GetFile(source.URI(params.TextDocument.URI)).SetContent(nil)
|
2018-09-27 16:15:45 -06:00
|
|
|
return nil
|
2018-09-24 16:05:51 -06:00
|
|
|
}
|
|
|
|
|
2018-11-05 19:23:02 -07:00
|
|
|
func (s *server) Completion(ctx context.Context, params *protocol.CompletionParams) (*protocol.CompletionList, error) {
|
2018-11-07 18:57:08 -07:00
|
|
|
f := s.view.GetFile(source.URI(params.TextDocument.URI))
|
|
|
|
tok, err := f.GetToken()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
pos := fromProtocolPosition(tok, params.Position)
|
2018-11-21 21:51:01 -07:00
|
|
|
items, prefix, err := source.Completion(ctx, f, pos)
|
2018-11-05 19:23:02 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &protocol.CompletionList{
|
|
|
|
IsIncomplete: false,
|
2018-11-21 21:51:01 -07:00
|
|
|
Items: toProtocolCompletionItems(items, prefix, params.Position, s.snippetsSupported, s.signatureHelpEnabled),
|
2018-11-05 19:23:02 -07:00
|
|
|
}, nil
|
2018-09-24 16:05:51 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *server) CompletionResolve(context.Context, *protocol.CompletionItem) (*protocol.CompletionItem, error) {
|
|
|
|
return nil, notImplemented("CompletionResolve")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *server) Hover(context.Context, *protocol.TextDocumentPositionParams) (*protocol.Hover, error) {
|
|
|
|
return nil, notImplemented("Hover")
|
|
|
|
}
|
|
|
|
|
2018-11-12 14:53:10 -07:00
|
|
|
func (s *server) SignatureHelp(ctx context.Context, params *protocol.TextDocumentPositionParams) (*protocol.SignatureHelp, error) {
|
|
|
|
f := s.view.GetFile(source.URI(params.TextDocument.URI))
|
|
|
|
tok, err := f.GetToken()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
pos := fromProtocolPosition(tok, params.Position)
|
|
|
|
info, err := source.SignatureHelp(ctx, f, pos)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return toProtocolSignatureHelp(info), nil
|
2018-09-24 16:05:51 -06:00
|
|
|
}
|
|
|
|
|
2018-11-06 12:04:07 -07:00
|
|
|
func (s *server) Definition(ctx context.Context, params *protocol.TextDocumentPositionParams) ([]protocol.Location, error) {
|
|
|
|
f := s.view.GetFile(source.URI(params.TextDocument.URI))
|
|
|
|
tok, err := f.GetToken()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
pos := fromProtocolPosition(tok, params.Position)
|
|
|
|
r, err := source.Definition(ctx, f, pos)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-11-14 19:11:16 -07:00
|
|
|
return []protocol.Location{toProtocolLocation(s.view.Config.Fset, r)}, nil
|
2018-09-24 16:05:51 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *server) TypeDefinition(context.Context, *protocol.TextDocumentPositionParams) ([]protocol.Location, error) {
|
|
|
|
return nil, notImplemented("TypeDefinition")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *server) Implementation(context.Context, *protocol.TextDocumentPositionParams) ([]protocol.Location, error) {
|
|
|
|
return nil, notImplemented("Implementation")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *server) References(context.Context, *protocol.ReferenceParams) ([]protocol.Location, error) {
|
|
|
|
return nil, notImplemented("References")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *server) DocumentHighlight(context.Context, *protocol.TextDocumentPositionParams) ([]protocol.DocumentHighlight, error) {
|
|
|
|
return nil, notImplemented("DocumentHighlight")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *server) DocumentSymbol(context.Context, *protocol.DocumentSymbolParams) ([]protocol.DocumentSymbol, error) {
|
|
|
|
return nil, notImplemented("DocumentSymbol")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *server) CodeAction(context.Context, *protocol.CodeActionParams) ([]protocol.CodeAction, error) {
|
|
|
|
return nil, notImplemented("CodeAction")
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
func (s *server) CodeLensResolve(context.Context, *protocol.CodeLens) (*protocol.CodeLens, error) {
|
|
|
|
return nil, notImplemented("CodeLensResolve")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *server) DocumentLink(context.Context, *protocol.DocumentLinkParams) ([]protocol.DocumentLink, error) {
|
2018-10-19 14:03:29 -06:00
|
|
|
return nil, nil // ignore
|
2018-09-24 16:05:51 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *server) DocumentLinkResolve(context.Context, *protocol.DocumentLink) (*protocol.DocumentLink, error) {
|
|
|
|
return nil, notImplemented("DocumentLinkResolve")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *server) DocumentColor(context.Context, *protocol.DocumentColorParams) ([]protocol.ColorInformation, error) {
|
|
|
|
return nil, notImplemented("DocumentColor")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *server) ColorPresentation(context.Context, *protocol.ColorPresentationParams) ([]protocol.ColorPresentation, error) {
|
|
|
|
return nil, notImplemented("ColorPresentation")
|
|
|
|
}
|
|
|
|
|
2018-09-27 09:28:20 -06:00
|
|
|
func (s *server) Formatting(ctx context.Context, params *protocol.DocumentFormattingParams) ([]protocol.TextEdit, error) {
|
2018-11-07 10:58:55 -07:00
|
|
|
return formatRange(ctx, s.view, params.TextDocument.URI, nil)
|
2018-09-24 16:05:51 -06:00
|
|
|
}
|
|
|
|
|
2018-09-27 16:15:45 -06:00
|
|
|
func (s *server) RangeFormatting(ctx context.Context, params *protocol.DocumentRangeFormattingParams) ([]protocol.TextEdit, error) {
|
2018-11-07 10:58:55 -07:00
|
|
|
return formatRange(ctx, s.view, params.TextDocument.URI, ¶ms.Range)
|
|
|
|
}
|
|
|
|
|
|
|
|
// formatRange formats a document with a given range.
|
|
|
|
func formatRange(ctx context.Context, v *source.View, uri protocol.DocumentURI, rng *protocol.Range) ([]protocol.TextEdit, error) {
|
|
|
|
f := v.GetFile(source.URI(uri))
|
|
|
|
tok, err := f.GetToken()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var r source.Range
|
|
|
|
if rng == nil {
|
|
|
|
r.Start = tok.Pos(0)
|
|
|
|
r.End = tok.Pos(tok.Size())
|
|
|
|
} else {
|
|
|
|
r = fromProtocolRange(tok, *rng)
|
|
|
|
}
|
2018-11-19 17:19:47 -07:00
|
|
|
content, err := f.Read()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-11-07 10:58:55 -07:00
|
|
|
edits, err := source.Format(ctx, f, r)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-11-19 17:19:47 -07:00
|
|
|
return toProtocolEdits(tok, content, edits), nil
|
2018-11-07 10:58:55 -07:00
|
|
|
}
|
|
|
|
|
2018-11-19 17:19:47 -07:00
|
|
|
func toProtocolEdits(tok *token.File, content []byte, edits []source.TextEdit) []protocol.TextEdit {
|
2018-11-07 10:58:55 -07:00
|
|
|
if edits == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2018-11-19 17:19:47 -07:00
|
|
|
// When a file ends with an empty line, the newline character is counted
|
|
|
|
// as part of the previous line. This causes the formatter to insert
|
|
|
|
// another unnecessary newline on each formatting. We handle this case by
|
|
|
|
// checking if the file already ends with a newline character.
|
|
|
|
hasExtraNewline := content[len(content)-1] == '\n'
|
2018-11-07 10:58:55 -07:00
|
|
|
result := make([]protocol.TextEdit, len(edits))
|
|
|
|
for i, edit := range edits {
|
2018-11-19 17:19:47 -07:00
|
|
|
rng := toProtocolRange(tok, edit.Range)
|
|
|
|
// If the edit ends at the end of the file, add the extra line.
|
|
|
|
if hasExtraNewline && tok.Offset(edit.Range.End) == len(content) {
|
|
|
|
rng.End.Line++
|
|
|
|
rng.End.Character = 0
|
|
|
|
}
|
2018-11-07 10:58:55 -07:00
|
|
|
result[i] = protocol.TextEdit{
|
2018-11-19 17:19:47 -07:00
|
|
|
Range: rng,
|
2018-11-07 10:58:55 -07:00
|
|
|
NewText: edit.NewText,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result
|
2018-09-24 16:05:51 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *server) OnTypeFormatting(context.Context, *protocol.DocumentOnTypeFormattingParams) ([]protocol.TextEdit, error) {
|
|
|
|
return nil, notImplemented("OnTypeFormatting")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *server) Rename(context.Context, *protocol.RenameParams) ([]protocol.WorkspaceEdit, error) {
|
|
|
|
return nil, notImplemented("Rename")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *server) FoldingRanges(context.Context, *protocol.FoldingRangeRequestParam) ([]protocol.FoldingRange, error) {
|
|
|
|
return nil, notImplemented("FoldingRanges")
|
|
|
|
}
|
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)
|
|
|
|
}
|