Skip to content

Commit bee2b51

Browse files
committed
Add support for custom messages
1 parent 8a2f8eb commit bee2b51

File tree

2 files changed

+58
-12
lines changed

2 files changed

+58
-12
lines changed

client.go

+26-6
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,18 @@ type ServerMessagesHandler interface {
4141

4242
// Client is an LSP Client
4343
type Client struct {
44-
conn *jsonrpc.Connection
45-
handler ServerMessagesHandler
46-
errorHandler func(e error)
44+
conn *jsonrpc.Connection
45+
handler ServerMessagesHandler
46+
customNotification map[string]CustomNotification
47+
customRequest map[string]CustomRequest
48+
errorHandler func(e error)
4749
}
4850

4951
func NewClient(in io.Reader, out io.Writer, handler ServerMessagesHandler) *Client {
5052
client := &Client{
51-
errorHandler: func(e error) {},
53+
errorHandler: func(e error) {},
54+
customNotification: map[string]CustomNotification{},
55+
customRequest: map[string]CustomRequest{},
5256
}
5357
client.handler = handler
5458
client.conn = jsonrpc.NewConnection(
@@ -67,6 +71,14 @@ func (client *Client) SetErrorHandler(handler func(e error)) {
6771
client.errorHandler = handler
6872
}
6973

74+
func (client *Client) RegisterCustomNotification(method string, callback CustomNotification) {
75+
client.customNotification[method] = callback
76+
}
77+
78+
func (client *Client) RegisterCustomRequest(method string, callback CustomRequest) {
79+
client.customRequest[method] = callback
80+
}
81+
7082
func (client *Client) Run() {
7183
client.conn.Run()
7284
}
@@ -114,7 +126,11 @@ func (client *Client) notificationDispatcher(logger jsonrpc.FunctionLogger, meth
114126
}
115127
client.handler.TextDocumentPublishDiagnostics(logger, &param)
116128
default:
117-
panic("unimplemented message")
129+
if handler, ok := client.customNotification[method]; ok {
130+
handler(logger, req)
131+
} else {
132+
panic("unimplemented notification: " + method)
133+
}
118134
}
119135
}
120136

@@ -177,7 +193,11 @@ func (client *Client) requestDispatcher(ctx context.Context, logger jsonrpc.Func
177193
case "workspace/codeLens/refresh":
178194
resp(nil, client.handler.WorkspaceCodeLensRefresh(ctx, logger))
179195
default:
180-
panic("unimplemented message")
196+
if handler, ok := client.customRequest[method]; ok {
197+
resp(handler(ctx, logger, req))
198+
} else {
199+
panic("unimplemented request: " + method)
200+
}
181201
}
182202
}
183203

server.go

+32-6
Original file line numberDiff line numberDiff line change
@@ -86,14 +86,24 @@ type ClientMessagesHandler interface {
8686

8787
// Server is an LSP Server
8888
type Server struct {
89-
conn *jsonrpc.Connection
90-
handler ClientMessagesHandler
91-
errorHandler func(e error)
89+
conn *jsonrpc.Connection
90+
handler ClientMessagesHandler
91+
customNotification map[string]CustomNotification
92+
customRequest map[string]CustomRequest
93+
errorHandler func(e error)
9294
}
9395

96+
// CustomNotification is a function type for incoming custom notifications callbacks
97+
type CustomNotification func(logger jsonrpc.FunctionLogger, req json.RawMessage)
98+
99+
// CustomNotification is a function type for incoming custom requests callbacks
100+
type CustomRequest func(ctx context.Context, logger jsonrpc.FunctionLogger, req json.RawMessage) (res interface{}, err *jsonrpc.ResponseError)
101+
94102
func NewServer(in io.Reader, out io.Writer, handler ClientMessagesHandler) *Server {
95103
serv := &Server{
96-
errorHandler: func(e error) {},
104+
errorHandler: func(e error) {},
105+
customNotification: map[string]CustomNotification{},
106+
customRequest: map[string]CustomRequest{},
97107
}
98108
serv.handler = handler
99109
serv.conn = jsonrpc.NewConnection(
@@ -112,6 +122,14 @@ func (serv *Server) SetErrorHandler(handler func(e error)) {
112122
serv.errorHandler = handler
113123
}
114124

125+
func (serv *Server) RegisterCustomNotification(method string, callback CustomNotification) {
126+
serv.customNotification[method] = callback
127+
}
128+
129+
func (serv *Server) RegisterCustomRequest(method string, callback CustomRequest) {
130+
serv.customRequest[method] = callback
131+
}
132+
115133
func (serv *Server) Run() {
116134
serv.conn.Run()
117135
}
@@ -228,7 +246,11 @@ func (serv *Server) notificationDispatcher(logger jsonrpc.FunctionLogger, method
228246
}
229247
serv.handler.TextDocumentDidClose(logger, &param)
230248
default:
231-
panic("unimplemented message")
249+
if handler, ok := serv.customNotification[method]; ok {
250+
handler(logger, req)
251+
} else {
252+
panic("unimplemented notification: " + method)
253+
}
232254
}
233255
}
234256

@@ -537,7 +559,11 @@ func (serv *Server) requestDispatcher(ctx context.Context, logger jsonrpc.Functi
537559
}
538560
resp(serv.handler.TextDocumentMoniker(ctx, logger, &param))
539561
default:
540-
panic("unimplemented message")
562+
if handler, ok := serv.customRequest[method]; ok {
563+
resp(handler(ctx, logger, req))
564+
} else {
565+
panic("unimplemented request: " + method)
566+
}
541567
}
542568
}
543569

0 commit comments

Comments
 (0)