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{{
|
2020-02-12 14:36:46 -07:00
|
|
|
URI: string(params.RootURI),
|
|
|
|
Name: path.Base(params.RootURI.SpanURI().Filename()),
|
2019-04-17 13:37:20 -06:00
|
|
|
}}
|
|
|
|
} 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,
|
2020-01-05 03:46:20 -07:00
|
|
|
WorkspaceSymbolProvider: 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-12-24 14:16:06 -07:00
|
|
|
Change: protocol.Incremental,
|
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-12-24 14:16:06 -07:00
|
|
|
if 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 {
|
2020-02-12 14:36:46 -07:00
|
|
|
uri := span.URIFromURI(folder.URI)
|
|
|
|
_, snapshot, err := s.addView(ctx, folder.Name, uri)
|
2019-11-15 12:47:29 -07:00
|
|
|
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
|
|
|
}
|
2020-01-11 21:59:57 -07:00
|
|
|
go s.diagnoseDetached(snapshot)
|
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{{
|
2020-02-12 14:36:46 -07:00
|
|
|
ScopeURI: string(folder),
|
2019-09-07 15:01:26 -06:00
|
|
|
Section: "gopls",
|
|
|
|
}, {
|
2020-02-12 14:36:46 -07:00
|
|
|
ScopeURI: string(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
|
|
|
|
}
|
|
|
|
|
2020-02-13 11:46:49 -07:00
|
|
|
// beginFileRequest checks preconditions for a file-oriented request and routes
|
|
|
|
// it to a snapshot.
|
|
|
|
// We don't want to return errors for benign conditions like wrong file type,
|
|
|
|
// so callers should do if !ok { return err } rather than if err != nil.
|
|
|
|
func (s *Server) beginFileRequest(pURI protocol.DocumentURI, expectKind source.FileKind) (source.Snapshot, source.FileHandle, bool, error) {
|
|
|
|
uri := pURI.SpanURI()
|
|
|
|
if !uri.IsFile() {
|
|
|
|
// Not a file URI. Stop processing the request, but don't return an error.
|
|
|
|
return nil, nil, false, nil
|
|
|
|
}
|
|
|
|
view, err := s.session.ViewOf(uri)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, false, err
|
|
|
|
}
|
|
|
|
snapshot := view.Snapshot()
|
|
|
|
fh, err := snapshot.GetFile(uri)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, false, err
|
|
|
|
}
|
|
|
|
if expectKind != source.UnknownKind && fh.Identity().Kind != expectKind {
|
|
|
|
// Wrong kind of file. Nothing to do.
|
|
|
|
return nil, nil, false, nil
|
|
|
|
}
|
|
|
|
return snapshot, fh, true, 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")
|
|
|
|
}
|
2020-02-21 15:31:45 -07:00
|
|
|
if s.state != serverShutDown {
|
|
|
|
// drop all the active views
|
|
|
|
s.session.Shutdown(ctx)
|
|
|
|
s.state = serverShutDown
|
|
|
|
}
|
2019-04-17 13:37:20 -06:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-02-09 12:44:03 -07:00
|
|
|
// ServerExitFunc is used to exit when requested by the client. It is mutable
|
|
|
|
// for testing purposes.
|
|
|
|
var ServerExitFunc = os.Exit
|
|
|
|
|
2019-04-17 13:37:20 -06:00
|
|
|
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 {
|
2020-02-09 12:44:03 -07:00
|
|
|
ServerExitFunc(1)
|
2019-04-17 13:37:20 -06:00
|
|
|
}
|
2020-02-09 12:44:03 -07:00
|
|
|
ServerExitFunc(0)
|
2019-04-17 13:37:20 -06:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|