2020-02-02 10:53:30 -07:00
|
|
|
// Copyright 2020 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 fake
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-05-28 19:21:29 -06:00
|
|
|
"fmt"
|
2020-02-02 10:53:30 -07:00
|
|
|
|
|
|
|
"golang.org/x/tools/internal/lsp/protocol"
|
|
|
|
)
|
|
|
|
|
2020-05-07 10:25:33 -06:00
|
|
|
// ClientHooks are called to handle the corresponding client LSP method.
|
|
|
|
type ClientHooks struct {
|
|
|
|
OnLogMessage func(context.Context, *protocol.LogMessageParams) error
|
|
|
|
OnDiagnostics func(context.Context, *protocol.PublishDiagnosticsParams) error
|
|
|
|
OnWorkDoneProgressCreate func(context.Context, *protocol.WorkDoneProgressCreateParams) error
|
|
|
|
OnProgress func(context.Context, *protocol.ProgressParams) error
|
|
|
|
OnShowMessage func(context.Context, *protocol.ShowMessageParams) error
|
2020-05-28 19:21:29 -06:00
|
|
|
OnShowMessageRequest func(context.Context, *protocol.ShowMessageRequestParams) error
|
2020-05-07 10:25:33 -06:00
|
|
|
}
|
|
|
|
|
2020-04-21 21:44:31 -06:00
|
|
|
// Client is an adapter that converts an *Editor into an LSP Client. It mosly
|
|
|
|
// delegates functionality to hooks that can be configured by tests.
|
2020-02-02 10:53:30 -07:00
|
|
|
type Client struct {
|
2020-05-07 10:25:33 -06:00
|
|
|
editor *Editor
|
|
|
|
hooks ClientHooks
|
2020-04-21 21:44:31 -06:00
|
|
|
}
|
|
|
|
|
2020-02-02 10:53:30 -07:00
|
|
|
func (c *Client) ShowMessage(ctx context.Context, params *protocol.ShowMessageParams) error {
|
2020-05-07 10:25:33 -06:00
|
|
|
if c.hooks.OnShowMessage != nil {
|
|
|
|
return c.hooks.OnShowMessage(ctx, params)
|
2020-04-29 11:33:43 -06:00
|
|
|
}
|
2020-02-02 10:53:30 -07:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) ShowMessageRequest(ctx context.Context, params *protocol.ShowMessageRequestParams) (*protocol.MessageActionItem, error) {
|
2020-05-28 19:21:29 -06:00
|
|
|
if c.hooks.OnShowMessageRequest != nil {
|
|
|
|
if err := c.hooks.OnShowMessageRequest(ctx, params); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(params.Actions) == 0 || len(params.Actions) > 1 {
|
|
|
|
return nil, fmt.Errorf("fake editor cannot handle multiple action items")
|
|
|
|
}
|
|
|
|
return ¶ms.Actions[0], nil
|
2020-02-02 10:53:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) LogMessage(ctx context.Context, params *protocol.LogMessageParams) error {
|
2020-05-07 10:25:33 -06:00
|
|
|
if c.hooks.OnLogMessage != nil {
|
|
|
|
return c.hooks.OnLogMessage(ctx, params)
|
2020-02-02 10:53:30 -07:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) Event(ctx context.Context, event *interface{}) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) PublishDiagnostics(ctx context.Context, params *protocol.PublishDiagnosticsParams) error {
|
2020-05-07 10:25:33 -06:00
|
|
|
if c.hooks.OnDiagnostics != nil {
|
|
|
|
return c.hooks.OnDiagnostics(ctx, params)
|
2020-02-02 10:53:30 -07:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) WorkspaceFolders(context.Context) ([]protocol.WorkspaceFolder, error) {
|
|
|
|
return []protocol.WorkspaceFolder{}, nil
|
|
|
|
}
|
|
|
|
|
2020-04-14 13:35:57 -06:00
|
|
|
func (c *Client) Configuration(_ context.Context, p *protocol.ParamConfiguration) ([]interface{}, error) {
|
|
|
|
results := make([]interface{}, len(p.Items))
|
|
|
|
for i, item := range p.Items {
|
|
|
|
if item.Section != "gopls" {
|
|
|
|
continue
|
|
|
|
}
|
2020-05-07 10:25:33 -06:00
|
|
|
results[i] = c.editor.configuration()
|
2020-04-14 13:35:57 -06:00
|
|
|
}
|
|
|
|
return results, nil
|
2020-02-02 10:53:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) RegisterCapability(context.Context, *protocol.RegistrationParams) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) UnregisterCapability(context.Context, *protocol.UnregistrationParams) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-04-21 21:44:31 -06:00
|
|
|
func (c *Client) Progress(ctx context.Context, params *protocol.ProgressParams) error {
|
2020-05-07 10:25:33 -06:00
|
|
|
if c.hooks.OnProgress != nil {
|
|
|
|
return c.hooks.OnProgress(ctx, params)
|
2020-04-21 21:44:31 -06:00
|
|
|
}
|
2020-03-12 14:31:00 -06:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-04-21 21:44:31 -06:00
|
|
|
func (c *Client) WorkDoneProgressCreate(ctx context.Context, params *protocol.WorkDoneProgressCreateParams) error {
|
2020-05-07 10:25:33 -06:00
|
|
|
if c.hooks.OnWorkDoneProgressCreate != nil {
|
|
|
|
return c.hooks.OnWorkDoneProgressCreate(ctx, params)
|
2020-04-21 21:44:31 -06:00
|
|
|
}
|
2020-03-12 14:31:00 -06:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-02-02 10:53:30 -07:00
|
|
|
// ApplyEdit applies edits sent from the server. Note that as of writing gopls
|
|
|
|
// doesn't use this feature, so it is untested.
|
|
|
|
func (c *Client) ApplyEdit(ctx context.Context, params *protocol.ApplyWorkspaceEditParams) (*protocol.ApplyWorkspaceEditResponse, error) {
|
|
|
|
if len(params.Edit.Changes) != 0 {
|
|
|
|
return &protocol.ApplyWorkspaceEditResponse{FailureReason: "Edit.Changes is unsupported"}, nil
|
|
|
|
}
|
|
|
|
for _, change := range params.Edit.DocumentChanges {
|
2020-05-07 10:25:33 -06:00
|
|
|
path := c.editor.sandbox.Workdir.URIToPath(change.TextDocument.URI)
|
2020-02-27 15:20:53 -07:00
|
|
|
edits := convertEdits(change.Edits)
|
2020-05-07 10:25:33 -06:00
|
|
|
c.editor.EditBuffer(ctx, path, edits)
|
2020-02-02 10:53:30 -07:00
|
|
|
}
|
|
|
|
return &protocol.ApplyWorkspaceEditResponse{Applied: true}, nil
|
|
|
|
}
|