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 (
|
2019-02-17 23:00:10 -07:00
|
|
|
"bytes"
|
2018-09-24 16:05:51 -06:00
|
|
|
"context"
|
2019-01-10 18:42:25 -07:00
|
|
|
"fmt"
|
2019-02-08 15:55:06 -07:00
|
|
|
"go/ast"
|
|
|
|
"go/parser"
|
2018-12-18 14:18:03 -07:00
|
|
|
"go/token"
|
2019-01-10 18:42:25 -07:00
|
|
|
"net"
|
2018-09-27 09:28:20 -06:00
|
|
|
"os"
|
2019-03-28 06:49:42 -06:00
|
|
|
"path"
|
|
|
|
"strings"
|
2018-09-27 09:28:20 -06:00
|
|
|
"sync"
|
2018-09-24 16:05:51 -06:00
|
|
|
|
2018-12-18 14:18:03 -07:00
|
|
|
"golang.org/x/tools/go/packages"
|
2018-09-24 16:05:51 -06:00
|
|
|
"golang.org/x/tools/internal/jsonrpc2"
|
2018-12-05 15:00:36 -07:00
|
|
|
"golang.org/x/tools/internal/lsp/cache"
|
2018-09-24 16:05:51 -06:00
|
|
|
"golang.org/x/tools/internal/lsp/protocol"
|
2018-11-02 14:15:31 -06:00
|
|
|
"golang.org/x/tools/internal/lsp/source"
|
2019-03-29 17:04:29 -06:00
|
|
|
"golang.org/x/tools/internal/lsp/xlog"
|
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
|
|
|
|
func NewClientServer(client protocol.Client) *Server {
|
|
|
|
return &Server{
|
2019-03-29 12:16:59 -06:00
|
|
|
client: client,
|
2019-03-29 17:04:29 -06:00
|
|
|
log: xlog.New(protocol.NewLogger(client)),
|
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-03-28 19:06:01 -06:00
|
|
|
func NewServer(stream jsonrpc2.Stream) *Server {
|
2019-03-29 12:16:59 -06:00
|
|
|
s := &Server{}
|
2019-03-29 17:04:29 -06:00
|
|
|
s.Conn, s.client, s.log = protocol.NewServer(stream, s)
|
2019-03-28 19:06:01 -06:00
|
|
|
return 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-03-28 19:06:01 -06:00
|
|
|
func RunServerOnPort(ctx context.Context, port int, h func(s *Server)) error {
|
|
|
|
return RunServerOnAddress(ctx, 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-03-28 19:06:01 -06:00
|
|
|
func RunServerOnAddress(ctx context.Context, addr string, h func(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
|
|
|
|
}
|
|
|
|
stream := jsonrpc2.NewHeaderStream(conn, conn)
|
2019-03-28 19:06:01 -06:00
|
|
|
s := NewServer(stream)
|
|
|
|
h(s)
|
|
|
|
go s.Run(ctx)
|
2019-01-10 18:42:25 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
2019-03-29 17:04:29 -06:00
|
|
|
log xlog.Logger
|
2018-09-27 09:28:20 -06:00
|
|
|
|
|
|
|
initializedMu sync.Mutex
|
|
|
|
initialized bool // set once the server has received "initialize" request
|
|
|
|
|
2019-04-01 12:56:49 -06:00
|
|
|
signatureHelpEnabled bool
|
|
|
|
snippetsSupported bool
|
|
|
|
configurationSupported bool
|
|
|
|
dynamicConfigurationSupported bool
|
2018-11-20 14:05:10 -07:00
|
|
|
|
2019-03-04 15:50:16 -07:00
|
|
|
textDocumentSyncKind protocol.TextDocumentSyncKind
|
|
|
|
|
2019-03-29 14:05:59 -06:00
|
|
|
views []*cache.View
|
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-03-28 19:06:01 -06:00
|
|
|
func (s *Server) Run(ctx context.Context) error {
|
|
|
|
return s.Conn.Run(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) Initialize(ctx context.Context, params *protocol.InitializeParams) (*protocol.InitializeResult, error) {
|
2018-09-27 09:28:20 -06:00
|
|
|
s.initializedMu.Lock()
|
|
|
|
defer s.initializedMu.Unlock()
|
|
|
|
if s.initialized {
|
|
|
|
return nil, jsonrpc2.NewErrorf(jsonrpc2.CodeInvalidRequest, "server already initialized")
|
|
|
|
}
|
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.
|
2019-03-29 16:16:43 -06:00
|
|
|
if x, ok := params.Capabilities["textDocument"].(map[string]interface{}); ok {
|
|
|
|
if x, ok := x["completion"].(map[string]interface{}); ok {
|
|
|
|
if x, ok := x["completionItem"].(map[string]interface{}); ok {
|
|
|
|
if x, ok := x["snippetSupport"].(bool); ok {
|
|
|
|
s.snippetsSupported = x
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-03-11 10:17:05 -06:00
|
|
|
}
|
2019-04-01 12:56:49 -06:00
|
|
|
// Check if the client supports configuration messages.
|
|
|
|
if x, ok := params.Capabilities["workspace"].(map[string]interface{}); ok {
|
|
|
|
if x, ok := x["configuration"].(bool); ok {
|
|
|
|
s.configurationSupported = x
|
|
|
|
}
|
|
|
|
if x, ok := x["didChangeConfiguration"].(map[string]interface{}); ok {
|
|
|
|
if x, ok := x["dynamicRegistration"].(bool); ok {
|
|
|
|
s.dynamicConfigurationSupported = x
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-20 14:05:10 -07:00
|
|
|
s.signatureHelpEnabled = true
|
|
|
|
|
2019-03-04 15:50:16 -07:00
|
|
|
// TODO(rstambler): Change this default to protocol.Incremental (or add a
|
|
|
|
// flag). Disabled for now to simplify debugging.
|
|
|
|
s.textDocumentSyncKind = protocol.Full
|
|
|
|
|
2019-03-29 17:04:29 -06:00
|
|
|
//We need a "detached" context so it does not get timeout cancelled.
|
|
|
|
//TODO(iancottrell): Do we need to copy any values across?
|
|
|
|
viewContext := context.Background()
|
2019-03-29 14:05:59 -06:00
|
|
|
folders := params.WorkspaceFolders
|
|
|
|
if len(folders) == 0 {
|
|
|
|
if params.RootURI != "" {
|
|
|
|
folders = []protocol.WorkspaceFolder{{
|
|
|
|
URI: params.RootURI,
|
|
|
|
Name: path.Base(params.RootURI),
|
|
|
|
}}
|
|
|
|
} else {
|
|
|
|
// no folders and no root, single file mode
|
|
|
|
//TODO(iancottrell): not sure how to do single file mode yet
|
|
|
|
//issue: golang.org/issue/31168
|
|
|
|
return nil, fmt.Errorf("single file mode not supported yet")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, folder := range folders {
|
|
|
|
uri := span.NewURI(folder.URI)
|
|
|
|
folderPath, err := uri.Filename()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
s.views = append(s.views, cache.NewView(viewContext, s.log, folder.Name, uri, &packages.Config{
|
|
|
|
Context: ctx,
|
|
|
|
Dir: folderPath,
|
|
|
|
Env: os.Environ(),
|
|
|
|
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,
|
|
|
|
}))
|
|
|
|
}
|
2018-12-12 11:57:52 -07:00
|
|
|
|
2018-09-27 09:28:20 -06:00
|
|
|
return &protocol.InitializeResult{
|
|
|
|
Capabilities: protocol.ServerCapabilities{
|
2019-03-11 10:17:05 -06:00
|
|
|
InnerServerCapabilities: protocol.InnerServerCapabilities{
|
|
|
|
CodeActionProvider: true,
|
|
|
|
CompletionProvider: &protocol.CompletionOptions{
|
|
|
|
TriggerCharacters: []string{"."},
|
|
|
|
},
|
|
|
|
DefinitionProvider: true,
|
|
|
|
DocumentFormattingProvider: true,
|
|
|
|
DocumentRangeFormattingProvider: true,
|
2019-03-18 16:43:08 -06:00
|
|
|
DocumentSymbolProvider: true,
|
2019-03-11 10:17:05 -06:00
|
|
|
HoverProvider: true,
|
2019-03-25 18:56:05 -06:00
|
|
|
DocumentHighlightProvider: true,
|
2019-03-11 10:17:05 -06:00
|
|
|
SignatureHelpProvider: &protocol.SignatureHelpOptions{
|
|
|
|
TriggerCharacters: []string{"(", ","},
|
|
|
|
},
|
|
|
|
TextDocumentSync: &protocol.TextDocumentSyncOptions{
|
|
|
|
Change: s.textDocumentSyncKind,
|
|
|
|
OpenClose: true,
|
|
|
|
},
|
2018-11-14 18:42:30 -07:00
|
|
|
},
|
2019-03-11 10:17:05 -06:00
|
|
|
TypeDefinitionServerCapabilities: protocol.TypeDefinitionServerCapabilities{
|
|
|
|
TypeDefinitionProvider: true,
|
2018-09-27 09:28:20 -06:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}, nil
|
2018-09-24 16:05:51 -06:00
|
|
|
}
|
|
|
|
|
2019-03-28 19:06:01 -06:00
|
|
|
func (s *Server) Initialized(ctx context.Context, params *protocol.InitializedParams) error {
|
2019-04-01 12:56:49 -06:00
|
|
|
if s.configurationSupported {
|
|
|
|
if s.dynamicConfigurationSupported {
|
|
|
|
s.client.RegisterCapability(ctx, &protocol.RegistrationParams{
|
|
|
|
Registrations: []protocol.Registration{{
|
|
|
|
ID: "workspace/didChangeConfiguration",
|
|
|
|
Method: "workspace/didChangeConfiguration",
|
|
|
|
}},
|
|
|
|
})
|
2019-03-29 14:05:59 -06:00
|
|
|
}
|
2019-04-01 12:56:49 -06:00
|
|
|
for _, view := range s.views {
|
|
|
|
config, err := s.client.Configuration(ctx, &protocol.ConfigurationParams{
|
|
|
|
Items: []protocol.ConfigurationItem{{
|
|
|
|
ScopeURI: protocol.NewURI(view.Folder),
|
|
|
|
Section: "gopls",
|
|
|
|
}},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := s.processConfig(view, config[0]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-03-29 14:05:59 -06:00
|
|
|
}
|
2019-03-29 12:16:59 -06:00
|
|
|
}
|
2019-03-28 06:49:42 -06:00
|
|
|
return nil
|
2018-09-24 16:05:51 -06:00
|
|
|
}
|
|
|
|
|
2019-03-28 19:06:01 -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
|
|
|
}
|
|
|
|
|
2019-03-28 19:06:01 -06:00
|
|
|
func (s *Server) Exit(ctx context.Context) error {
|
2018-09-27 09:28:20 -06:00
|
|
|
if s.initialized {
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
os.Exit(0)
|
|
|
|
return nil
|
2018-09-24 16:05:51 -06:00
|
|
|
}
|
|
|
|
|
2019-03-28 19:06:01 -06:00
|
|
|
func (s *Server) DidChangeWorkspaceFolders(context.Context, *protocol.DidChangeWorkspaceFoldersParams) error {
|
2018-09-24 16:05:51 -06:00
|
|
|
return notImplemented("DidChangeWorkspaceFolders")
|
|
|
|
}
|
|
|
|
|
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-03-28 19:06:01 -06:00
|
|
|
func (s *Server) Symbols(context.Context, *protocol.WorkspaceSymbolParams) ([]protocol.SymbolInformation, error) {
|
2018-09-24 16:05:51 -06:00
|
|
|
return nil, notImplemented("Symbols")
|
|
|
|
}
|
|
|
|
|
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-03-28 19:06:01 -06:00
|
|
|
func (s *Server) DidOpen(ctx context.Context, params *protocol.DidOpenTextDocumentParams) error {
|
2019-03-26 19:11:20 -06:00
|
|
|
return s.cacheAndDiagnose(ctx, span.NewURI(params.TextDocument.URI), params.TextDocument.Text)
|
2019-02-17 23:00:10 -07:00
|
|
|
}
|
|
|
|
|
2019-03-28 19:06:01 -06:00
|
|
|
func (s *Server) applyChanges(ctx context.Context, params *protocol.DidChangeTextDocumentParams) (string, error) {
|
2019-02-17 23:00:10 -07:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-03-29 14:05:59 -06:00
|
|
|
uri := span.NewURI(params.TextDocument.URI)
|
|
|
|
view := s.findView(ctx, uri)
|
|
|
|
file, m, err := newColumnMap(ctx, view, uri)
|
2019-02-17 23:00:10 -07:00
|
|
|
if err != nil {
|
|
|
|
return "", jsonrpc2.NewErrorf(jsonrpc2.CodeInternalError, "file not found")
|
|
|
|
}
|
2019-03-05 15:30:44 -07:00
|
|
|
content := file.GetContent(ctx)
|
2019-02-17 23:00:10 -07:00
|
|
|
for _, change := range params.ContentChanges {
|
2019-03-15 11:19:43 -06:00
|
|
|
spn, err := m.RangeSpan(*change.Range)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if !spn.HasOffset() {
|
2019-02-17 23:00:10 -07:00
|
|
|
return "", jsonrpc2.NewErrorf(jsonrpc2.CodeInternalError, "invalid range for content change")
|
|
|
|
}
|
2019-03-15 11:19:43 -06:00
|
|
|
start, end := spn.Start().Offset(), spn.End().Offset()
|
|
|
|
if end <= start {
|
2019-02-17 23:00:10 -07:00
|
|
|
return "", jsonrpc2.NewErrorf(jsonrpc2.CodeInternalError, "invalid range for content change")
|
|
|
|
}
|
|
|
|
var buf bytes.Buffer
|
2019-03-15 11:19:43 -06:00
|
|
|
buf.Write(content[:start])
|
2019-02-17 23:00:10 -07:00
|
|
|
buf.WriteString(change.Text)
|
2019-03-15 11:19:43 -06:00
|
|
|
buf.Write(content[end:])
|
2019-02-17 23:00:10 -07:00
|
|
|
content = buf.Bytes()
|
|
|
|
}
|
|
|
|
return string(content), nil
|
|
|
|
}
|
|
|
|
|
2019-03-28 19:06:01 -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")
|
|
|
|
}
|
2019-02-17 23:00:10 -07:00
|
|
|
|
2019-03-04 15:50:16 -07:00
|
|
|
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
|
2018-09-27 16:15:45 -06:00
|
|
|
}
|
2019-03-26 19:11:20 -06:00
|
|
|
return s.cacheAndDiagnose(ctx, span.NewURI(params.TextDocument.URI), text)
|
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-03-28 19:06:01 -06:00
|
|
|
func (s *Server) DidSave(context.Context, *protocol.DidSaveTextDocumentParams) error {
|
2018-10-19 14:03:29 -06:00
|
|
|
return nil // ignore
|
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-03-29 14:05:59 -06:00
|
|
|
uri := span.NewURI(params.TextDocument.URI)
|
|
|
|
view := s.findView(ctx, uri)
|
|
|
|
return view.SetContent(ctx, uri, nil)
|
2018-09-24 16:05:51 -06:00
|
|
|
}
|
|
|
|
|
2019-03-28 19:06:01 -06:00
|
|
|
func (s *Server) Completion(ctx context.Context, params *protocol.CompletionParams) (*protocol.CompletionList, error) {
|
2019-03-29 14:05:59 -06:00
|
|
|
uri := span.NewURI(params.TextDocument.URI)
|
|
|
|
view := s.findView(ctx, uri)
|
|
|
|
f, m, err := newColumnMap(ctx, view, uri)
|
2019-02-04 15:44:35 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-03-15 11:19:43 -06:00
|
|
|
spn, err := m.PointSpan(params.Position)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
rng, err := spn.Range(m.Converter)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
items, prefix, err := source.Completion(ctx, f, rng.Start)
|
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
|
|
|
}
|
|
|
|
|
2019-03-28 19:06:01 -06:00
|
|
|
func (s *Server) CompletionResolve(context.Context, *protocol.CompletionItem) (*protocol.CompletionItem, error) {
|
2018-09-24 16:05:51 -06:00
|
|
|
return nil, notImplemented("CompletionResolve")
|
|
|
|
}
|
|
|
|
|
2019-03-28 19:06:01 -06:00
|
|
|
func (s *Server) Hover(ctx context.Context, params *protocol.TextDocumentPositionParams) (*protocol.Hover, error) {
|
2019-03-29 14:05:59 -06:00
|
|
|
uri := span.NewURI(params.TextDocument.URI)
|
|
|
|
view := s.findView(ctx, uri)
|
|
|
|
f, m, err := newColumnMap(ctx, view, uri)
|
2018-12-18 14:18:03 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-03-15 11:19:43 -06:00
|
|
|
spn, err := m.PointSpan(params.Position)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
identRange, err := spn.Range(m.Converter)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-03-29 14:05:59 -06:00
|
|
|
ident, err := source.Identifier(ctx, view, f, identRange.Start)
|
2018-12-04 16:16:34 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-03-05 15:30:44 -07:00
|
|
|
content, err := ident.Hover(ctx, nil)
|
2019-01-23 12:16:43 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
markdown := "```go\n" + content + "\n```"
|
2019-03-15 11:19:43 -06:00
|
|
|
identSpan, err := ident.Range.Span()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
rng, err := m.Range(identSpan)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-12-04 16:16:34 -07:00
|
|
|
return &protocol.Hover{
|
|
|
|
Contents: protocol.MarkupContent{
|
|
|
|
Kind: protocol.Markdown,
|
2019-01-23 12:16:43 -07:00
|
|
|
Value: markdown,
|
2018-12-04 16:16:34 -07:00
|
|
|
},
|
2019-02-19 19:11:15 -07:00
|
|
|
Range: &rng,
|
2018-12-04 16:16:34 -07:00
|
|
|
}, nil
|
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-03-29 14:05:59 -06:00
|
|
|
uri := span.NewURI(params.TextDocument.URI)
|
|
|
|
view := s.findView(ctx, uri)
|
|
|
|
f, m, err := newColumnMap(ctx, view, uri)
|
2019-02-04 15:44:35 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-03-15 11:19:43 -06:00
|
|
|
spn, err := m.PointSpan(params.Position)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
rng, err := spn.Range(m.Converter)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
info, err := source.SignatureHelp(ctx, f, rng.Start)
|
2018-11-12 14:53:10 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return toProtocolSignatureHelp(info), nil
|
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-03-29 14:05:59 -06:00
|
|
|
uri := span.NewURI(params.TextDocument.URI)
|
|
|
|
view := s.findView(ctx, uri)
|
|
|
|
f, m, err := newColumnMap(ctx, view, uri)
|
2019-02-04 15:44:35 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-03-15 11:19:43 -06:00
|
|
|
spn, err := m.PointSpan(params.Position)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
rng, err := spn.Range(m.Converter)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-03-29 14:05:59 -06:00
|
|
|
ident, err := source.Identifier(ctx, view, f, rng.Start)
|
2019-03-15 11:19:43 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
decSpan, err := ident.Declaration.Range.Span()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-03-29 14:05:59 -06:00
|
|
|
_, decM, err := newColumnMap(ctx, view, decSpan.URI())
|
2019-03-15 11:19:43 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
loc, err := decM.Location(decSpan)
|
2018-12-18 14:18:03 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-03-15 11:19:43 -06:00
|
|
|
return []protocol.Location{loc}, nil
|
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-03-29 14:05:59 -06:00
|
|
|
uri := span.NewURI(params.TextDocument.URI)
|
|
|
|
view := s.findView(ctx, uri)
|
|
|
|
f, m, err := newColumnMap(ctx, view, uri)
|
2019-02-04 15:44:35 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-03-15 11:19:43 -06:00
|
|
|
spn, err := m.PointSpan(params.Position)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
rng, err := spn.Range(m.Converter)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-03-29 14:05:59 -06:00
|
|
|
ident, err := source.Identifier(ctx, view, f, rng.Start)
|
2018-12-18 14:18:03 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-03-15 11:19:43 -06:00
|
|
|
identSpan, err := ident.Type.Range.Span()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-03-29 14:05:59 -06:00
|
|
|
_, identM, err := newColumnMap(ctx, view, identSpan.URI())
|
2019-03-15 11:19:43 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
loc, err := identM.Location(identSpan)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return []protocol.Location{loc}, nil
|
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-03-28 19:06:01 -06:00
|
|
|
func (s *Server) References(context.Context, *protocol.ReferenceParams) ([]protocol.Location, error) {
|
2018-09-24 16:05:51 -06:00
|
|
|
return nil, notImplemented("References")
|
|
|
|
}
|
|
|
|
|
2019-03-28 19:06:01 -06:00
|
|
|
func (s *Server) DocumentHighlight(ctx context.Context, params *protocol.TextDocumentPositionParams) ([]protocol.DocumentHighlight, error) {
|
2019-03-29 14:05:59 -06:00
|
|
|
uri := span.NewURI(params.TextDocument.URI)
|
|
|
|
view := s.findView(ctx, uri)
|
|
|
|
f, m, err := newColumnMap(ctx, view, uri)
|
2019-03-25 18:56:05 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
spn, err := m.PointSpan(params.Position)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
rng, err := spn.Range(m.Converter)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
spans := source.Highlight(ctx, f, rng.Start)
|
|
|
|
return toProtocolHighlight(m, spans), nil
|
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-03-29 14:05:59 -06:00
|
|
|
uri := span.NewURI(params.TextDocument.URI)
|
|
|
|
view := s.findView(ctx, uri)
|
|
|
|
f, m, err := newColumnMap(ctx, view, uri)
|
2019-03-18 16:43:08 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
symbols := source.DocumentSymbols(ctx, f)
|
|
|
|
return toProtocolDocumentSymbols(m, symbols), nil
|
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-03-29 14:05:59 -06:00
|
|
|
uri := span.NewURI(params.TextDocument.URI)
|
|
|
|
view := s.findView(ctx, uri)
|
|
|
|
_, m, err := newColumnMap(ctx, view, uri)
|
2019-02-19 19:11:15 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-03-15 11:19:43 -06:00
|
|
|
spn, err := m.RangeSpan(params.Range)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-03-29 14:05:59 -06:00
|
|
|
edits, err := organizeImports(ctx, view, spn)
|
2018-12-14 15:00:24 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return []protocol.CodeAction{
|
|
|
|
{
|
|
|
|
Title: "Organize Imports",
|
|
|
|
Kind: protocol.SourceOrganizeImports,
|
2019-03-11 10:17:05 -06:00
|
|
|
Edit: &protocol.WorkspaceEdit{
|
|
|
|
Changes: &map[string][]protocol.TextEdit{
|
2018-12-14 15:00:24 -07:00
|
|
|
params.TextDocument.URI: edits,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, nil
|
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-03-28 19:06:01 -06:00
|
|
|
func (s *Server) CodeLensResolve(context.Context, *protocol.CodeLens) (*protocol.CodeLens, error) {
|
2018-09-24 16:05:51 -06:00
|
|
|
return nil, notImplemented("CodeLensResolve")
|
|
|
|
}
|
|
|
|
|
2019-03-28 19:06:01 -06:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-03-28 19:06:01 -06:00
|
|
|
func (s *Server) DocumentLinkResolve(context.Context, *protocol.DocumentLink) (*protocol.DocumentLink, error) {
|
2018-09-24 16:05:51 -06:00
|
|
|
return nil, notImplemented("DocumentLinkResolve")
|
|
|
|
}
|
|
|
|
|
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-03-29 14:05:59 -06:00
|
|
|
uri := span.NewURI(params.TextDocument.URI)
|
|
|
|
view := s.findView(ctx, uri)
|
|
|
|
spn := span.New(uri, span.Point{}, span.Point{})
|
|
|
|
return formatRange(ctx, view, spn)
|
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-03-29 14:05:59 -06:00
|
|
|
uri := span.NewURI(params.TextDocument.URI)
|
|
|
|
view := s.findView(ctx, uri)
|
|
|
|
_, m, err := newColumnMap(ctx, view, uri)
|
2019-02-19 19:11:15 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-03-15 11:19:43 -06:00
|
|
|
spn, err := m.RangeSpan(params.Range)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-03-29 14:05:59 -06:00
|
|
|
return formatRange(ctx, view, spn)
|
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-03-28 19:06:01 -06:00
|
|
|
func (s *Server) Rename(context.Context, *protocol.RenameParams) ([]protocol.WorkspaceEdit, error) {
|
2018-09-24 16:05:51 -06:00
|
|
|
return nil, notImplemented("Rename")
|
|
|
|
}
|
|
|
|
|
2019-03-28 19:06:01 -06:00
|
|
|
func (s *Server) FoldingRanges(context.Context, *protocol.FoldingRangeParams) ([]protocol.FoldingRange, error) {
|
2018-09-24 16:05:51 -06:00
|
|
|
return nil, notImplemented("FoldingRanges")
|
|
|
|
}
|
2018-09-27 09:28:20 -06:00
|
|
|
|
2019-03-29 14:05:59 -06:00
|
|
|
func (s *Server) processConfig(view *cache.View, config interface{}) error {
|
2019-04-01 14:15:58 -06:00
|
|
|
// TODO: We should probably store and process more of the config.
|
|
|
|
if config == nil {
|
|
|
|
return nil // ignore error if you don't have a config
|
|
|
|
}
|
2019-03-28 06:49:42 -06:00
|
|
|
c, ok := config.(map[string]interface{})
|
|
|
|
if !ok {
|
2019-04-01 14:15:58 -06:00
|
|
|
return fmt.Errorf("invalid config gopls type %T", config)
|
2019-03-28 06:49:42 -06:00
|
|
|
}
|
|
|
|
env := c["env"]
|
|
|
|
if env == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
menv, ok := env.(map[string]interface{})
|
|
|
|
if !ok {
|
2019-04-01 14:15:58 -06:00
|
|
|
return fmt.Errorf("invalid config gopls.env type %T", env)
|
2019-03-28 06:49:42 -06:00
|
|
|
}
|
|
|
|
for k, v := range menv {
|
2019-03-29 14:05:59 -06:00
|
|
|
view.Config.Env = applyEnv(view.Config.Env, k, v)
|
2019-03-28 06:49:42 -06:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func applyEnv(env []string, k string, v interface{}) []string {
|
|
|
|
prefix := k + "="
|
|
|
|
value := prefix + fmt.Sprint(v)
|
|
|
|
for i, s := range env {
|
|
|
|
if strings.HasPrefix(s, prefix) {
|
|
|
|
env[i] = value
|
|
|
|
return env
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return append(env, value)
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|
2019-03-29 14:05:59 -06:00
|
|
|
|
|
|
|
func (s *Server) findView(ctx context.Context, uri span.URI) *cache.View {
|
|
|
|
// first see if a view already has this file
|
|
|
|
for _, view := range s.views {
|
|
|
|
if view.FindFile(ctx, uri) != nil {
|
|
|
|
return view
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var longest *cache.View
|
|
|
|
for _, view := range s.views {
|
|
|
|
if longest != nil && len(longest.Folder) > len(view.Folder) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if strings.HasPrefix(string(uri), string(view.Folder)) {
|
|
|
|
longest = view
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if longest != nil {
|
|
|
|
return longest
|
|
|
|
}
|
|
|
|
//TODO: are there any more heuristics we can use?
|
|
|
|
return s.views[0]
|
|
|
|
}
|