mirror of
https://github.com/golang/go
synced 2024-11-18 20:14:43 -07:00
dbad8e90c9
This rationalises all the position handling and conversion code out. Fixes golang/go#29149 Change-Id: I2814f3e8ba769924bc70f35df9e5bf4d97d064de Reviewed-on: https://go-review.googlesource.com/c/tools/+/166884 Reviewed-by: Rebecca Stambler <rstambler@golang.org>
439 lines
14 KiB
Go
439 lines
14 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
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"fmt"
|
|
"go/ast"
|
|
"go/parser"
|
|
"go/token"
|
|
"net"
|
|
"os"
|
|
"sync"
|
|
|
|
"golang.org/x/tools/go/packages"
|
|
"golang.org/x/tools/internal/jsonrpc2"
|
|
"golang.org/x/tools/internal/lsp/cache"
|
|
"golang.org/x/tools/internal/lsp/protocol"
|
|
"golang.org/x/tools/internal/lsp/source"
|
|
"golang.org/x/tools/internal/span"
|
|
)
|
|
|
|
// 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 {
|
|
s := &server{}
|
|
conn, client := protocol.RunServer(ctx, stream, s, opts...)
|
|
s.client = client
|
|
return conn.Wait(ctx)
|
|
}
|
|
|
|
// RunServerOnPort starts an LSP server on the given port and does not exit.
|
|
// This function exists for debugging purposes.
|
|
func RunServerOnPort(ctx context.Context, port int, opts ...interface{}) error {
|
|
return RunServerOnAddress(ctx, fmt.Sprintf(":%v", port))
|
|
}
|
|
|
|
// RunServerOnPort starts an LSP server on the given port and does not exit.
|
|
// This function exists for debugging purposes.
|
|
func RunServerOnAddress(ctx context.Context, addr string, opts ...interface{}) error {
|
|
s := &server{}
|
|
ln, err := net.Listen("tcp", addr)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
for {
|
|
conn, err := ln.Accept()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
stream := jsonrpc2.NewHeaderStream(conn, conn)
|
|
go func() {
|
|
conn, client := protocol.RunServer(ctx, stream, s, opts...)
|
|
s.client = client
|
|
conn.Wait(ctx)
|
|
}()
|
|
}
|
|
}
|
|
|
|
type server struct {
|
|
client protocol.Client
|
|
|
|
initializedMu sync.Mutex
|
|
initialized bool // set once the server has received "initialize" request
|
|
|
|
signatureHelpEnabled bool
|
|
snippetsSupported bool
|
|
|
|
textDocumentSyncKind protocol.TextDocumentSyncKind
|
|
|
|
viewMu sync.Mutex
|
|
view *cache.View
|
|
}
|
|
|
|
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")
|
|
}
|
|
s.initialized = true // mark server as initialized now
|
|
|
|
// Check if the client supports snippets in completion items.
|
|
capText := params.Capabilities.InnerClientCapabilities.TextDocument
|
|
if capText != nil && capText.Completion != nil && capText.Completion.CompletionItem != nil {
|
|
s.snippetsSupported = capText.Completion.CompletionItem.SnippetSupport
|
|
}
|
|
s.signatureHelpEnabled = true
|
|
|
|
var rootURI span.URI
|
|
if params.RootURI != "" {
|
|
rootURI = span.URI(params.RootURI)
|
|
}
|
|
rootPath, err := rootURI.Filename()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// TODO(rstambler): Change this default to protocol.Incremental (or add a
|
|
// flag). Disabled for now to simplify debugging.
|
|
s.textDocumentSyncKind = protocol.Full
|
|
|
|
s.view = cache.NewView(&packages.Config{
|
|
Context: ctx,
|
|
Dir: rootPath,
|
|
Mode: packages.LoadImports,
|
|
Fset: token.NewFileSet(),
|
|
Overlay: make(map[string][]byte),
|
|
ParseFile: func(fset *token.FileSet, filename string, src []byte) (*ast.File, error) {
|
|
return parser.ParseFile(fset, filename, src, parser.AllErrors|parser.ParseComments)
|
|
},
|
|
Tests: true,
|
|
})
|
|
|
|
return &protocol.InitializeResult{
|
|
Capabilities: protocol.ServerCapabilities{
|
|
InnerServerCapabilities: protocol.InnerServerCapabilities{
|
|
CodeActionProvider: true,
|
|
CompletionProvider: &protocol.CompletionOptions{
|
|
TriggerCharacters: []string{"."},
|
|
},
|
|
DefinitionProvider: true,
|
|
DocumentFormattingProvider: true,
|
|
DocumentRangeFormattingProvider: true,
|
|
HoverProvider: true,
|
|
SignatureHelpProvider: &protocol.SignatureHelpOptions{
|
|
TriggerCharacters: []string{"(", ","},
|
|
},
|
|
TextDocumentSync: &protocol.TextDocumentSyncOptions{
|
|
Change: s.textDocumentSyncKind,
|
|
OpenClose: true,
|
|
},
|
|
},
|
|
TypeDefinitionServerCapabilities: protocol.TypeDefinitionServerCapabilities{
|
|
TypeDefinitionProvider: true,
|
|
},
|
|
},
|
|
}, nil
|
|
}
|
|
|
|
func (s *server) Initialized(context.Context, *protocol.InitializedParams) error {
|
|
return nil // ignore
|
|
}
|
|
|
|
func (s *server) Shutdown(context.Context) error {
|
|
s.initializedMu.Lock()
|
|
defer s.initializedMu.Unlock()
|
|
if !s.initialized {
|
|
return jsonrpc2.NewErrorf(jsonrpc2.CodeInvalidRequest, "server not initialized")
|
|
}
|
|
s.initialized = false
|
|
return nil
|
|
}
|
|
|
|
func (s *server) Exit(ctx context.Context) error {
|
|
if s.initialized {
|
|
os.Exit(1)
|
|
}
|
|
os.Exit(0)
|
|
return nil
|
|
}
|
|
|
|
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")
|
|
}
|
|
|
|
func (s *server) DidOpen(ctx context.Context, params *protocol.DidOpenTextDocumentParams) error {
|
|
return s.cacheAndDiagnose(ctx, span.URI(params.TextDocument.URI), params.TextDocument.Text)
|
|
}
|
|
|
|
func (s *server) applyChanges(ctx context.Context, params *protocol.DidChangeTextDocumentParams) (string, error) {
|
|
if len(params.ContentChanges) == 1 && params.ContentChanges[0].Range == nil {
|
|
// If range is empty, we expect the full content of file, i.e. a single change with no range.
|
|
change := params.ContentChanges[0]
|
|
if change.RangeLength != 0 {
|
|
return "", jsonrpc2.NewErrorf(jsonrpc2.CodeInternalError, "unexpected change range provided")
|
|
}
|
|
return change.Text, nil
|
|
}
|
|
|
|
file, m, err := newColumnMap(ctx, s.view, span.URI(params.TextDocument.URI))
|
|
if err != nil {
|
|
return "", jsonrpc2.NewErrorf(jsonrpc2.CodeInternalError, "file not found")
|
|
}
|
|
content := file.GetContent(ctx)
|
|
for _, change := range params.ContentChanges {
|
|
spn := m.RangeSpan(*change.Range).Clean(nil)
|
|
if spn.Start.Offset <= 0 {
|
|
return "", jsonrpc2.NewErrorf(jsonrpc2.CodeInternalError, "invalid range for content change")
|
|
}
|
|
if spn.End.Offset <= spn.Start.Offset {
|
|
return "", jsonrpc2.NewErrorf(jsonrpc2.CodeInternalError, "invalid range for content change")
|
|
}
|
|
var buf bytes.Buffer
|
|
buf.Write(content[:spn.Start.Offset])
|
|
buf.WriteString(change.Text)
|
|
buf.Write(content[spn.End.Offset:])
|
|
content = buf.Bytes()
|
|
}
|
|
return string(content), nil
|
|
}
|
|
|
|
func (s *server) DidChange(ctx context.Context, params *protocol.DidChangeTextDocumentParams) error {
|
|
if len(params.ContentChanges) < 1 {
|
|
return jsonrpc2.NewErrorf(jsonrpc2.CodeInternalError, "no content changes provided")
|
|
}
|
|
|
|
var text string
|
|
switch s.textDocumentSyncKind {
|
|
case protocol.Incremental:
|
|
var err error
|
|
text, err = s.applyChanges(ctx, params)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
case protocol.Full:
|
|
// We expect the full content of file, i.e. a single change with no range.
|
|
change := params.ContentChanges[0]
|
|
if change.RangeLength != 0 {
|
|
return jsonrpc2.NewErrorf(jsonrpc2.CodeInternalError, "unexpected change range provided")
|
|
}
|
|
text = change.Text
|
|
}
|
|
return s.cacheAndDiagnose(ctx, span.URI(params.TextDocument.URI), text)
|
|
}
|
|
|
|
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 {
|
|
return nil // ignore
|
|
}
|
|
|
|
func (s *server) DidClose(ctx context.Context, params *protocol.DidCloseTextDocumentParams) error {
|
|
s.setContent(ctx, span.URI(params.TextDocument.URI), nil)
|
|
return nil
|
|
}
|
|
|
|
func (s *server) Completion(ctx context.Context, params *protocol.CompletionParams) (*protocol.CompletionList, error) {
|
|
f, m, err := newColumnMap(ctx, s.view, span.URI(params.TextDocument.URI))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
spn := m.PointSpan(params.Position)
|
|
items, prefix, err := source.Completion(ctx, f, spn.Range(m.Converter).Start)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &protocol.CompletionList{
|
|
IsIncomplete: false,
|
|
Items: toProtocolCompletionItems(items, prefix, params.Position, s.snippetsSupported, s.signatureHelpEnabled),
|
|
}, nil
|
|
}
|
|
|
|
func (s *server) CompletionResolve(context.Context, *protocol.CompletionItem) (*protocol.CompletionItem, error) {
|
|
return nil, notImplemented("CompletionResolve")
|
|
}
|
|
|
|
func (s *server) Hover(ctx context.Context, params *protocol.TextDocumentPositionParams) (*protocol.Hover, error) {
|
|
f, m, err := newColumnMap(ctx, s.view, span.URI(params.TextDocument.URI))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
spn := m.PointSpan(params.Position)
|
|
ident, err := source.Identifier(ctx, s.view, f, spn.Range(m.Converter).Start)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
content, err := ident.Hover(ctx, nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
markdown := "```go\n" + content + "\n```"
|
|
rng := m.Range(ident.Range.Span())
|
|
return &protocol.Hover{
|
|
Contents: protocol.MarkupContent{
|
|
Kind: protocol.Markdown,
|
|
Value: markdown,
|
|
},
|
|
Range: &rng,
|
|
}, nil
|
|
}
|
|
|
|
func (s *server) SignatureHelp(ctx context.Context, params *protocol.TextDocumentPositionParams) (*protocol.SignatureHelp, error) {
|
|
f, m, err := newColumnMap(ctx, s.view, span.URI(params.TextDocument.URI))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
spn := m.PointSpan(params.Position)
|
|
info, err := source.SignatureHelp(ctx, f, spn.Range(m.Converter).Start)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return toProtocolSignatureHelp(info), nil
|
|
}
|
|
|
|
func (s *server) Definition(ctx context.Context, params *protocol.TextDocumentPositionParams) ([]protocol.Location, error) {
|
|
f, m, err := newColumnMap(ctx, s.view, span.URI(params.TextDocument.URI))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
spn := m.PointSpan(params.Position)
|
|
ident, err := source.Identifier(ctx, s.view, f, spn.Range(m.Converter).Start)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return []protocol.Location{m.Location(ident.Declaration.Range.Span())}, nil
|
|
}
|
|
|
|
func (s *server) TypeDefinition(ctx context.Context, params *protocol.TextDocumentPositionParams) ([]protocol.Location, error) {
|
|
f, m, err := newColumnMap(ctx, s.view, span.URI(params.TextDocument.URI))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
spn := m.PointSpan(params.Position)
|
|
ident, err := source.Identifier(ctx, s.view, f, spn.Range(m.Converter).Start)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return []protocol.Location{m.Location(ident.Type.Range.Span())}, nil
|
|
}
|
|
|
|
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(ctx context.Context, params *protocol.CodeActionParams) ([]protocol.CodeAction, error) {
|
|
_, m, err := newColumnMap(ctx, s.view, span.URI(params.TextDocument.URI))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
spn := m.RangeSpan(params.Range)
|
|
edits, err := organizeImports(ctx, s.view, spn)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return []protocol.CodeAction{
|
|
{
|
|
Title: "Organize Imports",
|
|
Kind: protocol.SourceOrganizeImports,
|
|
Edit: &protocol.WorkspaceEdit{
|
|
Changes: &map[string][]protocol.TextEdit{
|
|
params.TextDocument.URI: edits,
|
|
},
|
|
},
|
|
},
|
|
}, nil
|
|
}
|
|
|
|
func (s *server) CodeLens(context.Context, *protocol.CodeLensParams) ([]protocol.CodeLens, error) {
|
|
return nil, nil // ignore
|
|
}
|
|
|
|
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) {
|
|
return nil, nil // ignore
|
|
}
|
|
|
|
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")
|
|
}
|
|
|
|
func (s *server) Formatting(ctx context.Context, params *protocol.DocumentFormattingParams) ([]protocol.TextEdit, error) {
|
|
spn := span.Span{URI: span.URI(params.TextDocument.URI)}
|
|
return formatRange(ctx, s.view, spn)
|
|
}
|
|
|
|
func (s *server) RangeFormatting(ctx context.Context, params *protocol.DocumentRangeFormattingParams) ([]protocol.TextEdit, error) {
|
|
_, m, err := newColumnMap(ctx, s.view, span.URI(params.TextDocument.URI))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
spn := m.RangeSpan(params.Range)
|
|
return formatRange(ctx, s.view, spn)
|
|
}
|
|
|
|
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.FoldingRangeParams) ([]protocol.FoldingRange, error) {
|
|
return nil, notImplemented("FoldingRanges")
|
|
}
|
|
|
|
func notImplemented(method string) *jsonrpc2.Error {
|
|
return jsonrpc2.NewErrorf(jsonrpc2.CodeMethodNotFound, "method %q not yet implemented", method)
|
|
}
|