2019-04-17 13:37:20 -06:00
|
|
|
// Copyright 2019 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-05-03 12:31:03 -06:00
|
|
|
"bytes"
|
2019-04-17 13:37:20 -06:00
|
|
|
"context"
|
2019-09-11 11:13:44 -06:00
|
|
|
"fmt"
|
2019-04-17 13:37:20 -06:00
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
|
|
|
|
"golang.org/x/tools/internal/jsonrpc2"
|
2019-05-29 17:58:27 -06:00
|
|
|
"golang.org/x/tools/internal/lsp/debug"
|
2019-04-17 13:37:20 -06:00
|
|
|
"golang.org/x/tools/internal/lsp/protocol"
|
2019-05-14 21:04:23 -06:00
|
|
|
"golang.org/x/tools/internal/lsp/source"
|
2019-04-17 13:37:20 -06:00
|
|
|
"golang.org/x/tools/internal/span"
|
2019-08-13 13:07:39 -06:00
|
|
|
"golang.org/x/tools/internal/telemetry/log"
|
2019-08-06 13:13:11 -06:00
|
|
|
errors "golang.org/x/xerrors"
|
2019-04-17 13:37:20 -06:00
|
|
|
)
|
|
|
|
|
2019-11-17 12:29:15 -07:00
|
|
|
func (s *Server) initialize(ctx context.Context, params *protocol.ParamInitialize) (*protocol.InitializeResult, error) {
|
2019-07-29 21:48:11 -06:00
|
|
|
s.stateMu.Lock()
|
|
|
|
state := s.state
|
|
|
|
s.stateMu.Unlock()
|
|
|
|
if state >= serverInitializing {
|
2019-04-17 13:37:20 -06:00
|
|
|
return nil, jsonrpc2.NewErrorf(jsonrpc2.CodeInvalidRequest, "server already initialized")
|
|
|
|
}
|
2019-07-29 21:48:11 -06:00
|
|
|
s.stateMu.Lock()
|
2019-07-27 12:59:14 -06:00
|
|
|
s.state = serverInitializing
|
2019-07-29 21:48:11 -06:00
|
|
|
s.stateMu.Unlock()
|
2019-04-17 13:37:20 -06:00
|
|
|
|
2019-09-05 22:17:36 -06:00
|
|
|
options := s.session.Options()
|
|
|
|
defer func() { s.session.SetOptions(options) }()
|
|
|
|
|
2019-09-11 11:13:44 -06:00
|
|
|
// TODO: Handle results here.
|
2019-09-09 07:28:25 -06:00
|
|
|
source.SetOptions(&options, params.InitializationOptions)
|
|
|
|
options.ForClientCapabilities(params.Capabilities)
|
2019-04-17 13:37:20 -06:00
|
|
|
|
2019-09-09 11:04:12 -06:00
|
|
|
s.pendingFolders = params.WorkspaceFolders
|
|
|
|
if len(s.pendingFolders) == 0 {
|
2019-04-17 13:37:20 -06:00
|
|
|
if params.RootURI != "" {
|
2019-09-09 11:04:12 -06:00
|
|
|
s.pendingFolders = []protocol.WorkspaceFolder{{
|
2019-04-17 13:37:20 -06:00
|
|
|
URI: params.RootURI,
|
|
|
|
Name: path.Base(params.RootURI),
|
|
|
|
}}
|
|
|
|
} else {
|
2019-09-06 16:25:36 -06:00
|
|
|
// No folders and no root--we are in single file mode.
|
|
|
|
// TODO: https://golang.org/issue/34160.
|
|
|
|
return nil, errors.Errorf("gopls does not yet support editing a single file. Please open a directory.")
|
2019-04-17 13:37:20 -06:00
|
|
|
}
|
|
|
|
}
|
2019-05-06 14:44:43 -06:00
|
|
|
|
2019-11-21 21:42:27 -07:00
|
|
|
var codeActionProvider interface{} = true
|
2019-11-17 12:29:15 -07:00
|
|
|
if ca := params.Capabilities.TextDocument.CodeAction; len(ca.CodeActionLiteralSupport.CodeActionKind.ValueSet) > 0 {
|
2019-08-14 13:24:21 -06:00
|
|
|
// If the client has specified CodeActionLiteralSupport,
|
|
|
|
// send the code actions we support.
|
|
|
|
//
|
|
|
|
// Using CodeActionOptions is only valid if codeActionLiteralSupport is set.
|
|
|
|
codeActionProvider = &protocol.CodeActionOptions{
|
|
|
|
CodeActionKinds: s.getSupportedCodeActions(),
|
|
|
|
}
|
|
|
|
}
|
2019-11-21 21:42:27 -07:00
|
|
|
var renameOpts interface{} = true
|
|
|
|
if r := params.Capabilities.TextDocument.Rename; r.PrepareSupport {
|
|
|
|
renameOpts = protocol.RenameOptions{
|
|
|
|
PrepareProvider: r.PrepareSupport,
|
|
|
|
}
|
2019-08-22 11:31:03 -06:00
|
|
|
}
|
2019-04-17 13:37:20 -06:00
|
|
|
return &protocol.InitializeResult{
|
|
|
|
Capabilities: protocol.ServerCapabilities{
|
2019-08-14 13:24:21 -06:00
|
|
|
CodeActionProvider: codeActionProvider,
|
2019-11-17 12:29:15 -07:00
|
|
|
CompletionProvider: protocol.CompletionOptions{
|
2019-04-17 13:37:20 -06:00
|
|
|
TriggerCharacters: []string{"."},
|
|
|
|
},
|
2019-05-08 03:44:01 -06:00
|
|
|
DefinitionProvider: true,
|
2019-10-31 16:19:51 -06:00
|
|
|
TypeDefinitionProvider: true,
|
|
|
|
ImplementationProvider: true,
|
2019-05-08 03:44:01 -06:00
|
|
|
DocumentFormattingProvider: true,
|
|
|
|
DocumentSymbolProvider: true,
|
2019-11-17 12:29:15 -07:00
|
|
|
ExecuteCommandProvider: protocol.ExecuteCommandOptions{
|
2019-09-18 23:21:54 -06:00
|
|
|
Commands: options.SupportedCommands,
|
|
|
|
},
|
|
|
|
FoldingRangeProvider: true,
|
|
|
|
HoverProvider: true,
|
|
|
|
DocumentHighlightProvider: true,
|
2019-11-17 12:29:15 -07:00
|
|
|
DocumentLinkProvider: protocol.DocumentLinkOptions{},
|
2019-09-18 23:21:54 -06:00
|
|
|
ReferencesProvider: true,
|
|
|
|
RenameProvider: renameOpts,
|
2019-11-17 12:29:15 -07:00
|
|
|
SignatureHelpProvider: protocol.SignatureHelpOptions{
|
2019-04-17 13:37:20 -06:00
|
|
|
TriggerCharacters: []string{"(", ","},
|
|
|
|
},
|
|
|
|
TextDocumentSync: &protocol.TextDocumentSyncOptions{
|
2019-09-05 22:17:36 -06:00
|
|
|
Change: options.TextDocumentSyncKind,
|
2019-04-17 13:37:20 -06:00
|
|
|
OpenClose: true,
|
2019-11-17 12:29:15 -07:00
|
|
|
Save: protocol.SaveOptions{
|
2019-06-18 10:41:22 -06:00
|
|
|
IncludeText: false,
|
|
|
|
},
|
2019-04-17 13:37:20 -06:00
|
|
|
},
|
2019-11-17 12:29:15 -07:00
|
|
|
Workspace: protocol.WorkspaceGn{
|
2019-11-20 14:38:43 -07:00
|
|
|
WorkspaceFolders: protocol.WorkspaceFoldersGn{
|
2019-05-06 14:44:43 -06:00
|
|
|
Supported: true,
|
|
|
|
ChangeNotifications: "workspace/didChangeWorkspaceFolders",
|
|
|
|
},
|
|
|
|
},
|
2019-04-17 13:37:20 -06:00
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) initialized(ctx context.Context, params *protocol.InitializedParams) error {
|
2019-07-29 21:48:11 -06:00
|
|
|
s.stateMu.Lock()
|
|
|
|
s.state = serverInitialized
|
|
|
|
s.stateMu.Unlock()
|
|
|
|
|
2019-09-05 22:17:36 -06:00
|
|
|
options := s.session.Options()
|
|
|
|
defer func() { s.session.SetOptions(options) }()
|
|
|
|
|
2019-08-19 10:53:51 -06:00
|
|
|
var registrations []protocol.Registration
|
2019-09-05 22:17:36 -06:00
|
|
|
if options.ConfigurationSupported && options.DynamicConfigurationSupported {
|
2019-08-19 10:53:51 -06:00
|
|
|
registrations = append(registrations,
|
|
|
|
protocol.Registration{
|
|
|
|
ID: "workspace/didChangeConfiguration",
|
|
|
|
Method: "workspace/didChangeConfiguration",
|
|
|
|
},
|
|
|
|
protocol.Registration{
|
|
|
|
ID: "workspace/didChangeWorkspaceFolders",
|
|
|
|
Method: "workspace/didChangeWorkspaceFolders",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-09-05 22:17:36 -06:00
|
|
|
if options.WatchFileChanges && options.DynamicWatchedFilesSupported {
|
2019-08-19 10:53:51 -06:00
|
|
|
registrations = append(registrations, protocol.Registration{
|
|
|
|
ID: "workspace/didChangeWatchedFiles",
|
|
|
|
Method: "workspace/didChangeWatchedFiles",
|
|
|
|
RegisterOptions: protocol.DidChangeWatchedFilesRegistrationOptions{
|
|
|
|
Watchers: []protocol.FileSystemWatcher{{
|
|
|
|
GlobPattern: "**/*.go",
|
2019-10-15 16:07:52 -06:00
|
|
|
Kind: float64(protocol.WatchChange + protocol.WatchDelete + protocol.WatchCreate),
|
2019-04-17 13:37:20 -06:00
|
|
|
}},
|
2019-08-19 10:53:51 -06:00
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(registrations) > 0 {
|
|
|
|
s.client.RegisterCapability(ctx, &protocol.RegistrationParams{
|
|
|
|
Registrations: registrations,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-05-03 12:31:03 -06:00
|
|
|
buf := &bytes.Buffer{}
|
2019-05-29 17:58:27 -06:00
|
|
|
debug.PrintVersionInfo(buf, true, debug.PlainText)
|
2019-07-14 21:08:10 -06:00
|
|
|
log.Print(ctx, buf.String())
|
2019-09-09 11:04:12 -06:00
|
|
|
|
2019-11-21 16:55:49 -07:00
|
|
|
s.addFolders(ctx, s.pendingFolders)
|
|
|
|
s.pendingFolders = nil
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) addFolders(ctx context.Context, folders []protocol.WorkspaceFolder) {
|
|
|
|
originalViews := len(s.session.Views())
|
2019-11-15 10:43:45 -07:00
|
|
|
viewErrors := make(map[span.URI]error)
|
2019-11-21 16:55:49 -07:00
|
|
|
|
|
|
|
for _, folder := range folders {
|
2019-11-15 10:43:45 -07:00
|
|
|
uri := span.NewURI(folder.URI)
|
2019-11-15 12:47:29 -07:00
|
|
|
view, workspacePackages, err := s.addView(ctx, folder.Name, span.NewURI(folder.URI))
|
|
|
|
if err != nil {
|
2019-11-15 10:43:45 -07:00
|
|
|
viewErrors[uri] = err
|
2019-11-15 12:47:29 -07:00
|
|
|
continue
|
2019-09-09 11:04:12 -06:00
|
|
|
}
|
2019-11-20 12:26:02 -07:00
|
|
|
go s.diagnoseSnapshot(view.Snapshot(), workspacePackages)
|
2019-09-09 11:04:12 -06:00
|
|
|
}
|
2019-11-15 10:43:45 -07:00
|
|
|
if len(viewErrors) > 0 {
|
2019-11-21 16:55:49 -07:00
|
|
|
errMsg := fmt.Sprintf("Error loading workspace folders (expected %v, got %v)\n", len(folders), len(s.session.Views())-originalViews)
|
2019-11-15 10:43:45 -07:00
|
|
|
for uri, err := range viewErrors {
|
|
|
|
errMsg += fmt.Sprintf("failed to load view for %s: %v\n", uri, err)
|
|
|
|
}
|
|
|
|
s.client.ShowMessage(ctx, &protocol.ShowMessageParams{
|
|
|
|
Type: protocol.Error,
|
|
|
|
Message: errMsg,
|
|
|
|
})
|
|
|
|
}
|
2019-04-17 13:37:20 -06:00
|
|
|
}
|
|
|
|
|
2019-09-11 11:13:44 -06:00
|
|
|
func (s *Server) fetchConfig(ctx context.Context, name string, folder span.URI, o *source.Options) error {
|
2019-09-09 07:28:25 -06:00
|
|
|
if !s.session.Options().ConfigurationSupported {
|
2019-09-09 11:04:12 -06:00
|
|
|
return nil
|
|
|
|
}
|
2019-11-17 12:29:15 -07:00
|
|
|
v := protocol.ParamConfiguration{
|
2019-09-16 22:38:16 -06:00
|
|
|
ConfigurationParams: protocol.ConfigurationParams{
|
2019-09-07 15:01:26 -06:00
|
|
|
Items: []protocol.ConfigurationItem{{
|
2019-09-09 11:04:12 -06:00
|
|
|
ScopeURI: protocol.NewURI(folder),
|
2019-09-07 15:01:26 -06:00
|
|
|
Section: "gopls",
|
|
|
|
}, {
|
2019-09-09 11:04:12 -06:00
|
|
|
ScopeURI: protocol.NewURI(folder),
|
2019-09-18 10:32:40 -06:00
|
|
|
Section: fmt.Sprintf("gopls-%s", name),
|
2019-09-16 22:38:16 -06:00
|
|
|
}},
|
|
|
|
},
|
2019-09-07 15:01:26 -06:00
|
|
|
}
|
|
|
|
configs, err := s.client.Configuration(ctx, &v)
|
2019-07-27 12:59:14 -06:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, config := range configs {
|
2019-09-11 11:13:44 -06:00
|
|
|
results := source.SetOptions(o, config)
|
|
|
|
for _, result := range results {
|
|
|
|
if result.Error != nil {
|
|
|
|
s.client.ShowMessage(ctx, &protocol.ShowMessageParams{
|
|
|
|
Type: protocol.Error,
|
|
|
|
Message: result.Error.Error(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
switch result.State {
|
|
|
|
case source.OptionUnexpected:
|
|
|
|
s.client.ShowMessage(ctx, &protocol.ShowMessageParams{
|
|
|
|
Type: protocol.Error,
|
|
|
|
Message: fmt.Sprintf("unexpected config %s", result.Name),
|
|
|
|
})
|
|
|
|
case source.OptionDeprecated:
|
|
|
|
msg := fmt.Sprintf("config %s is deprecated", result.Name)
|
|
|
|
if result.Replacement != "" {
|
|
|
|
msg = fmt.Sprintf("%s, use %s instead", msg, result.Replacement)
|
|
|
|
}
|
|
|
|
s.client.ShowMessage(ctx, &protocol.ShowMessageParams{
|
|
|
|
Type: protocol.Warning,
|
|
|
|
Message: msg,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2019-07-27 12:59:14 -06:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-04-17 13:37:20 -06:00
|
|
|
func (s *Server) shutdown(ctx context.Context) error {
|
2019-07-29 21:48:11 -06:00
|
|
|
s.stateMu.Lock()
|
|
|
|
defer s.stateMu.Unlock()
|
2019-07-27 12:59:14 -06:00
|
|
|
if s.state < serverInitialized {
|
2019-04-17 13:37:20 -06:00
|
|
|
return jsonrpc2.NewErrorf(jsonrpc2.CodeInvalidRequest, "server not initialized")
|
|
|
|
}
|
2019-05-02 08:55:04 -06:00
|
|
|
// drop all the active views
|
2019-05-15 10:24:49 -06:00
|
|
|
s.session.Shutdown(ctx)
|
2019-07-27 12:59:14 -06:00
|
|
|
s.state = serverShutDown
|
2019-04-17 13:37:20 -06:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) exit(ctx context.Context) error {
|
2019-07-29 21:48:11 -06:00
|
|
|
s.stateMu.Lock()
|
|
|
|
defer s.stateMu.Unlock()
|
2019-07-27 12:59:14 -06:00
|
|
|
if s.state != serverShutDown {
|
2019-04-17 13:37:20 -06:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
os.Exit(0)
|
|
|
|
return nil
|
|
|
|
}
|
2019-09-05 22:17:36 -06:00
|
|
|
|
|
|
|
func setBool(b *bool, m map[string]interface{}, name string) {
|
|
|
|
if v, ok := m[name].(bool); ok {
|
|
|
|
*b = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func setNotBool(b *bool, m map[string]interface{}, name string) {
|
|
|
|
if v, ok := m[name].(bool); ok {
|
|
|
|
*b = !v
|
|
|
|
}
|
|
|
|
}
|