Skip to content

Commit 18652be

Browse files
committed
all: support go-language-server/[email protected] interfaces
1 parent acb4cc5 commit 18652be

13 files changed

+1383
-1137
lines changed

.errcheckignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
(*github.com/go-language-server/jsonrpc2.Conn).Reply
1+
(*github.com/go-language-server/jsonrpc2.Request).Reply
22
(*github.com/go-language-server/jsonrpc2.Conn).Notify

canceller.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// Copyright 2020 The go-language-server Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package protocol
6+
7+
import (
8+
"bytes"
9+
"context"
10+
"encoding/json"
11+
12+
"github.com/go-language-server/jsonrpc2"
13+
"go.uber.org/zap"
14+
)
15+
16+
// RequestCancelledError should be used when a request is canceled early.
17+
const RequestCancelledError jsonrpc2.Code = -32800
18+
19+
type canceller struct {
20+
logger *zap.Logger
21+
}
22+
23+
// compile time check whether the canceller implements jsonrpc2.Handler interface.
24+
var _ jsonrpc2.Handler = (*canceller)(nil)
25+
26+
// Deliver implements Handler interface.
27+
func (canceller) Deliver(ctx context.Context, r *jsonrpc2.Request, delivered bool) bool {
28+
// Hide cancellations from downstream handlers.
29+
return r.Method == MethodCancelRequest
30+
}
31+
32+
// Cancel implements Handler interface.
33+
func (canceller) Cancel(ctx context.Context, conn *jsonrpc2.Conn, id jsonrpc2.ID, canceled bool) bool {
34+
if canceled {
35+
return false
36+
}
37+
38+
conn.Notify(ctx, MethodCancelRequest, &CancelParams{ID: id})
39+
return true
40+
}
41+
42+
// Request implements Handler interface.
43+
func (c *canceller) Request(ctx context.Context, conn *jsonrpc2.Conn, direction jsonrpc2.Direction, r *jsonrpc2.WireRequest) context.Context {
44+
if direction == jsonrpc2.Receive && r.Method == MethodCancelRequest {
45+
dec := json.NewDecoder(bytes.NewReader(*r.Params))
46+
var params CancelParams
47+
if err := dec.Decode(&params); err != nil {
48+
c.logger.Error("Request", zap.Error(err))
49+
return ctx
50+
}
51+
52+
conn.Cancel(params.ID)
53+
}
54+
55+
return ctx
56+
}
57+
58+
// Response implements Handler interface.
59+
func (canceller) Response(ctx context.Context, conn *jsonrpc2.Conn, direction jsonrpc2.Direction, r *jsonrpc2.WireResponse) context.Context {
60+
return ctx
61+
}
62+
63+
// Done implements Handler interface.
64+
func (canceller) Done(ctx context.Context, err error) {}
65+
66+
// Read implements Handler interface.
67+
func (canceller) Read(ctx context.Context, n int64) context.Context { return ctx }
68+
69+
// Write implements Handler interface.
70+
func (canceller) Write(ctx context.Context, n int64) context.Context { return ctx }
71+
72+
// Error implements Handler interface.
73+
func (canceller) Error(ctx context.Context, err error) {}

client.go

Lines changed: 49 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,41 @@ import (
1111
"go.uber.org/zap"
1212
)
1313

14+
// clientHandler represents a client handler.
15+
type clientHandler struct {
16+
client ClientInterface
17+
}
18+
19+
// compile time check whether the clientHandler implements jsonrpc2.Handler interface.
20+
var _ jsonrpc2.Handler = &clientHandler{}
21+
22+
// Cancel implements Handler interface.
23+
func (clientHandler) Cancel(ctx context.Context, conn *jsonrpc2.Conn, id jsonrpc2.ID, canceled bool) bool {
24+
return false
25+
}
26+
27+
// Request implements Handler interface.
28+
func (clientHandler) Request(ctx context.Context, conn *jsonrpc2.Conn, direction jsonrpc2.Direction, r *jsonrpc2.WireRequest) context.Context {
29+
return ctx
30+
}
31+
32+
// Response implements Handler interface.
33+
func (clientHandler) Response(ctx context.Context, conn *jsonrpc2.Conn, direction jsonrpc2.Direction, r *jsonrpc2.WireResponse) context.Context {
34+
return ctx
35+
}
36+
37+
// Done implements Handler interface.
38+
func (clientHandler) Done(ctx context.Context, err error) {}
39+
40+
// Read implements Handler interface.
41+
func (clientHandler) Read(ctx context.Context, bytes int64) context.Context { return ctx }
42+
43+
// Write implements Handler interface.
44+
func (clientHandler) Write(ctx context.Context, bytes int64) context.Context { return ctx }
45+
46+
// Error implements Handler interface.
47+
func (clientHandler) Error(ctx context.Context, err error) {}
48+
1449
// ClientInterface represents a Language Server Protocol client.
1550
type ClientInterface interface {
1651
Run(ctx context.Context) (err error)
@@ -58,23 +93,23 @@ const (
5893
MethodWorkspaceWorkspaceFolders = "workspace/workspaceFolders"
5994
)
6095

61-
// Client implements a Language Server Protocol client.
62-
type Client struct {
96+
// client implements a Language Server Protocol client.
97+
type client struct {
6398
*jsonrpc2.Conn
6499
logger *zap.Logger
65100
}
66101

67102
// compiler time check whether the Client implements ClientInterface interface.
68-
var _ ClientInterface = (*Client)(nil)
103+
var _ ClientInterface = (*client)(nil)
69104

70105
// Run runs the Language Server Protocol client.
71-
func (c *Client) Run(ctx context.Context) (err error) {
106+
func (c *client) Run(ctx context.Context) (err error) {
72107
err = c.Conn.Run(ctx)
73108
return
74109
}
75110

76111
// LogMessage sends the notification from the server to the client to ask the client to log a particular message.
77-
func (c *Client) LogMessage(ctx context.Context, params *LogMessageParams) (err error) {
112+
func (c *client) LogMessage(ctx context.Context, params *LogMessageParams) (err error) {
78113
err = c.Conn.Notify(ctx, MethodWindowLogMessage, params)
79114
return
80115
}
@@ -89,30 +124,30 @@ func (c *Client) LogMessage(ctx context.Context, params *LogMessageParams) (err
89124
// When a file changes it is the server’s responsibility to re-compute diagnostics and push them to the client.
90125
// If the computed set is empty it has to push the empty array to clear former diagnostics.
91126
// Newly pushed diagnostics always replace previously pushed diagnostics. There is no merging that happens on the client side.
92-
func (c *Client) PublishDiagnostics(ctx context.Context, params *PublishDiagnosticsParams) (err error) {
127+
func (c *client) PublishDiagnostics(ctx context.Context, params *PublishDiagnosticsParams) (err error) {
93128
err = c.Conn.Notify(ctx, MethodTextDocumentPublishDiagnostics, params)
94129
return
95130
}
96131

97132
// ShowMessage sends the notification from a server to a client to ask the
98133
// client to display a particular message in the user interface.
99-
func (c *Client) ShowMessage(ctx context.Context, params *ShowMessageParams) (err error) {
134+
func (c *client) ShowMessage(ctx context.Context, params *ShowMessageParams) (err error) {
100135
err = c.Conn.Notify(ctx, MethodWindowShowMessage, params)
101136
return
102137
}
103138

104139
// ShowMessageRequest sends the request from a server to a client to ask the client to display a particular message in the user interface.
105140
//
106141
// In addition to the show message notification the request allows to pass actions and to wait for an answer from the client.
107-
func (c *Client) ShowMessageRequest(ctx context.Context, params *ShowMessageRequestParams) (result *MessageActionItem, err error) {
142+
func (c *client) ShowMessageRequest(ctx context.Context, params *ShowMessageRequestParams) (result *MessageActionItem, err error) {
108143
result = new(MessageActionItem)
109144
err = c.Conn.Call(ctx, MethodWindowShowMessageRequest, params, result)
110145

111146
return result, err
112147
}
113148

114149
// Telemetry sends the notification from the server to the client to ask the client to log a telemetry event.
115-
func (c *Client) Telemetry(ctx context.Context, params interface{}) (err error) {
150+
func (c *client) Telemetry(ctx context.Context, params interface{}) (err error) {
116151
err = c.Conn.Notify(ctx, MethodTelemetryEvent, params)
117152
return
118153
}
@@ -123,19 +158,19 @@ func (c *Client) Telemetry(ctx context.Context, params interface{}) (err error)
123158
//
124159
// A client opts in via the dynamicRegistration property on the specific client capabilities.
125160
// A client can even provide dynamic registration for capability A but not for capability B (see TextDocumentClientCapabilities as an example).
126-
func (c *Client) RegisterCapability(ctx context.Context, params *RegistrationParams) (err error) {
161+
func (c *client) RegisterCapability(ctx context.Context, params *RegistrationParams) (err error) {
127162
err = c.Conn.Call(ctx, MethodClientRegisterCapability, params, nil)
128163
return
129164
}
130165

131166
// UnregisterCapability sends the request from the server to the client to unregister a previously registered capability.
132-
func (c *Client) UnregisterCapability(ctx context.Context, params *UnregistrationParams) (err error) {
167+
func (c *client) UnregisterCapability(ctx context.Context, params *UnregistrationParams) (err error) {
133168
err = c.Conn.Call(ctx, MethodClientUnregisterCapability, params, nil)
134169
return
135170
}
136171

137172
// WorkspaceApplyEdit sends the request from the server to the client to modify resource on the client side.
138-
func (c *Client) WorkspaceApplyEdit(ctx context.Context, params *ApplyWorkspaceEditParams) (result bool, err error) {
173+
func (c *client) WorkspaceApplyEdit(ctx context.Context, params *ApplyWorkspaceEditParams) (result bool, err error) {
139174
err = c.Conn.Call(ctx, MethodWorkspaceApplyEdit, params, &result)
140175

141176
return result, err
@@ -146,7 +181,7 @@ func (c *Client) WorkspaceApplyEdit(ctx context.Context, params *ApplyWorkspaceE
146181
// The request can fetch several configuration settings in one roundtrip.
147182
// The order of the returned configuration settings correspond to the order of the
148183
// passed ConfigurationItems (e.g. the first item in the response is the result for the first configuration item in the params).
149-
func (c *Client) WorkspaceConfiguration(ctx context.Context, params *ConfigurationParams) ([]interface{}, error) {
184+
func (c *client) WorkspaceConfiguration(ctx context.Context, params *ConfigurationParams) ([]interface{}, error) {
150185
var result []interface{}
151186
err := c.Conn.Call(ctx, MethodWorkspaceConfiguration, params, &result)
152187

@@ -158,7 +193,7 @@ func (c *Client) WorkspaceConfiguration(ctx context.Context, params *Configurati
158193
// Returns null in the response if only a single file is open in the tool. Returns an empty array if a workspace is open but no folders are configured.
159194
//
160195
// Since version 3.6.0.
161-
func (c *Client) WorkspaceFolders(ctx context.Context) (result []WorkspaceFolder, err error) {
196+
func (c *client) WorkspaceFolders(ctx context.Context) (result []WorkspaceFolder, err error) {
162197
err = c.Conn.Call(ctx, MethodWorkspaceWorkspaceFolders, nil, &result)
163198

164199
return result, err

0 commit comments

Comments
 (0)