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"
|
|
|
|
"sync"
|
2019-02-17 23:00:10 -07:00
|
|
|
"unicode/utf8"
|
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"
|
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)
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
func RunServerOnPort(ctx context.Context, port int, opts ...interface{}) error {
|
2019-02-07 15:05:20 -07:00
|
|
|
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 {
|
2019-01-10 18:42:25 -07:00
|
|
|
s := &server{}
|
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)
|
|
|
|
go func() {
|
|
|
|
conn, client := protocol.RunServer(ctx, stream, s, opts...)
|
|
|
|
s.client = client
|
|
|
|
conn.Wait(ctx)
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-24 16:05:51 -06:00
|
|
|
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
|
|
|
|
|
2019-03-04 15:50:16 -07:00
|
|
|
textDocumentSyncKind protocol.TextDocumentSyncKind
|
|
|
|
|
2018-12-18 14:18:03 -07:00
|
|
|
viewMu sync.Mutex
|
2019-03-05 15:30:44 -07:00
|
|
|
view *cache.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-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-11 10:17:05 -06:00
|
|
|
capText := params.Capabilities.InnerClientCapabilities.TextDocument
|
|
|
|
if capText != nil && capText.Completion != nil && capText.Completion.CompletionItem != nil {
|
|
|
|
s.snippetsSupported = capText.Completion.CompletionItem.SnippetSupport
|
|
|
|
}
|
2018-11-20 14:05:10 -07:00
|
|
|
s.signatureHelpEnabled = true
|
|
|
|
|
2019-03-11 10:17:05 -06:00
|
|
|
var rootURI string
|
|
|
|
if params.RootURI != "" {
|
|
|
|
rootURI = params.RootURI
|
2019-02-04 15:44:35 -07:00
|
|
|
}
|
|
|
|
sourceURI, err := fromProtocolURI(rootURI)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
rootPath, err := sourceURI.Filename()
|
2018-12-12 11:57:52 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-02-04 15:44:35 -07:00
|
|
|
|
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
|
|
|
|
|
2018-12-18 14:18:03 -07:00
|
|
|
s.view = cache.NewView(&packages.Config{
|
2019-02-08 15:55:06 -07:00
|
|
|
Context: ctx,
|
2018-12-18 14:18:03 -07:00
|
|
|
Dir: rootPath,
|
2019-02-06 16:47:00 -07:00
|
|
|
Mode: packages.LoadImports,
|
2018-12-18 14:18:03 -07:00
|
|
|
Fset: token.NewFileSet(),
|
|
|
|
Overlay: make(map[string][]byte),
|
2019-02-08 15:55:06 -07:00
|
|
|
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-18 14:18:03 -07:00
|
|
|
})
|
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,
|
|
|
|
HoverProvider: true,
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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-12-18 14:18:03 -07:00
|
|
|
s.cacheAndDiagnose(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
|
|
|
}
|
|
|
|
|
2019-02-17 23:00:10 -07:00
|
|
|
func bytesOffset(content []byte, pos protocol.Position) int {
|
|
|
|
var line, char, offset int
|
|
|
|
|
2019-03-11 14:42:38 -06:00
|
|
|
for {
|
2019-02-17 23:00:10 -07:00
|
|
|
if line == int(pos.Line) && char == int(pos.Character) {
|
|
|
|
return offset
|
|
|
|
}
|
2019-03-11 14:42:38 -06:00
|
|
|
if len(content) == 0 {
|
|
|
|
return -1
|
|
|
|
}
|
2019-03-04 15:50:16 -07:00
|
|
|
|
2019-02-17 23:00:10 -07:00
|
|
|
r, size := utf8.DecodeRune(content)
|
|
|
|
char++
|
|
|
|
// The offsets are based on a UTF-16 string representation.
|
|
|
|
// So the rune should be checked twice for two code units in UTF-16.
|
|
|
|
if r >= 0x10000 {
|
|
|
|
if line == int(pos.Line) && char == int(pos.Character) {
|
|
|
|
return offset
|
|
|
|
}
|
|
|
|
char++
|
|
|
|
}
|
|
|
|
offset += size
|
|
|
|
content = content[size:]
|
|
|
|
if r == '\n' {
|
|
|
|
line++
|
|
|
|
char = 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
sourceURI, err := fromProtocolURI(params.TextDocument.URI)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
file, err := s.view.GetFile(ctx, sourceURI)
|
|
|
|
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 {
|
|
|
|
start := bytesOffset(content, change.Range.Start)
|
|
|
|
if start == -1 {
|
|
|
|
return "", jsonrpc2.NewErrorf(jsonrpc2.CodeInternalError, "invalid range for content change")
|
|
|
|
}
|
|
|
|
end := bytesOffset(content, change.Range.End)
|
|
|
|
if end == -1 {
|
|
|
|
return "", jsonrpc2.NewErrorf(jsonrpc2.CodeInternalError, "invalid range for content change")
|
|
|
|
}
|
|
|
|
var buf bytes.Buffer
|
|
|
|
buf.Write(content[:start])
|
|
|
|
buf.WriteString(change.Text)
|
|
|
|
buf.Write(content[end:])
|
|
|
|
content = buf.Bytes()
|
|
|
|
}
|
|
|
|
return string(content), nil
|
|
|
|
}
|
|
|
|
|
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")
|
|
|
|
}
|
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-02-17 23:00:10 -07:00
|
|
|
s.cacheAndDiagnose(ctx, params.TextDocument.URI, text)
|
2018-09-24 16:05:51 -06:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
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 {
|
2019-02-04 15:44:35 -07:00
|
|
|
sourceURI, err := fromProtocolURI(params.TextDocument.URI)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
s.setContent(ctx, sourceURI, 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) {
|
2019-02-04 15:44:35 -07:00
|
|
|
sourceURI, err := fromProtocolURI(params.TextDocument.URI)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
f, err := s.view.GetFile(ctx, sourceURI)
|
2018-12-18 14:18:03 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-03-05 15:30:44 -07:00
|
|
|
tok := f.GetToken(ctx)
|
2018-11-07 18:57:08 -07:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2018-12-04 16:16:34 -07:00
|
|
|
func (s *server) Hover(ctx context.Context, params *protocol.TextDocumentPositionParams) (*protocol.Hover, error) {
|
2019-02-04 15:44:35 -07:00
|
|
|
sourceURI, err := fromProtocolURI(params.TextDocument.URI)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
f, err := s.view.GetFile(ctx, sourceURI)
|
2018-12-18 14:18:03 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-03-05 15:30:44 -07:00
|
|
|
tok := f.GetToken(ctx)
|
2018-12-04 16:16:34 -07:00
|
|
|
pos := fromProtocolPosition(tok, params.Position)
|
2019-01-23 12:16:43 -07:00
|
|
|
ident, err := source.Identifier(ctx, s.view, f, pos)
|
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-11 10:17:05 -06:00
|
|
|
x := toProtocolRange(tok, ident.Range)
|
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-03-11 10:17:05 -06:00
|
|
|
Range: &x,
|
2018-12-04 16:16:34 -07:00
|
|
|
}, nil
|
2018-09-24 16:05:51 -06:00
|
|
|
}
|
|
|
|
|
2018-11-12 14:53:10 -07:00
|
|
|
func (s *server) SignatureHelp(ctx context.Context, params *protocol.TextDocumentPositionParams) (*protocol.SignatureHelp, error) {
|
2019-02-04 15:44:35 -07:00
|
|
|
sourceURI, err := fromProtocolURI(params.TextDocument.URI)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
f, err := s.view.GetFile(ctx, sourceURI)
|
2018-12-18 14:18:03 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-03-05 15:30:44 -07:00
|
|
|
tok := f.GetToken(ctx)
|
2018-11-12 14:53:10 -07:00
|
|
|
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) {
|
2019-02-04 15:44:35 -07:00
|
|
|
sourceURI, err := fromProtocolURI(params.TextDocument.URI)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
f, err := s.view.GetFile(ctx, sourceURI)
|
2018-12-18 14:18:03 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-03-05 15:30:44 -07:00
|
|
|
tok := f.GetToken(ctx)
|
2018-11-06 12:04:07 -07:00
|
|
|
pos := fromProtocolPosition(tok, params.Position)
|
2019-01-23 12:16:43 -07:00
|
|
|
ident, err := source.Identifier(ctx, s.view, f, pos)
|
2018-11-06 12:04:07 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-01-23 12:16:43 -07:00
|
|
|
return []protocol.Location{toProtocolLocation(s.view.FileSet(), ident.Declaration.Range)}, nil
|
2018-09-24 16:05:51 -06:00
|
|
|
}
|
|
|
|
|
2018-12-03 15:14:30 -07:00
|
|
|
func (s *server) TypeDefinition(ctx context.Context, params *protocol.TextDocumentPositionParams) ([]protocol.Location, error) {
|
2019-02-04 15:44:35 -07:00
|
|
|
sourceURI, err := fromProtocolURI(params.TextDocument.URI)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
f, err := s.view.GetFile(ctx, sourceURI)
|
2018-12-18 14:18:03 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-03-05 15:30:44 -07:00
|
|
|
tok := f.GetToken(ctx)
|
2018-12-03 15:14:30 -07:00
|
|
|
pos := fromProtocolPosition(tok, params.Position)
|
2019-01-23 12:16:43 -07:00
|
|
|
ident, err := source.Identifier(ctx, s.view, f, pos)
|
2018-12-03 15:14:30 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-01-23 12:16:43 -07:00
|
|
|
return []protocol.Location{toProtocolLocation(s.view.FileSet(), ident.Type.Range)}, nil
|
2018-09-24 16:05:51 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2018-12-14 15:00:24 -07:00
|
|
|
func (s *server) CodeAction(ctx context.Context, params *protocol.CodeActionParams) ([]protocol.CodeAction, error) {
|
|
|
|
edits, err := organizeImports(ctx, s.view, params.TextDocument.URI)
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2019-03-11 10:17:05 -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
|
|
|
|
|
|
|
func notImplemented(method string) *jsonrpc2.Error {
|
|
|
|
return jsonrpc2.NewErrorf(jsonrpc2.CodeMethodNotFound, "method %q not yet implemented", method)
|
|
|
|
}
|