@@ -86,14 +86,24 @@ type ClientMessagesHandler interface {
86
86
87
87
// Server is an LSP Server
88
88
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 )
92
94
}
93
95
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
+
94
102
func NewServer (in io.Reader , out io.Writer , handler ClientMessagesHandler ) * Server {
95
103
serv := & Server {
96
- errorHandler : func (e error ) {},
104
+ errorHandler : func (e error ) {},
105
+ customNotification : map [string ]CustomNotification {},
106
+ customRequest : map [string ]CustomRequest {},
97
107
}
98
108
serv .handler = handler
99
109
serv .conn = jsonrpc .NewConnection (
@@ -112,6 +122,14 @@ func (serv *Server) SetErrorHandler(handler func(e error)) {
112
122
serv .errorHandler = handler
113
123
}
114
124
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
+
115
133
func (serv * Server ) Run () {
116
134
serv .conn .Run ()
117
135
}
@@ -228,7 +246,11 @@ func (serv *Server) notificationDispatcher(logger jsonrpc.FunctionLogger, method
228
246
}
229
247
serv .handler .TextDocumentDidClose (logger , & param )
230
248
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
+ }
232
254
}
233
255
}
234
256
@@ -537,7 +559,11 @@ func (serv *Server) requestDispatcher(ctx context.Context, logger jsonrpc.Functi
537
559
}
538
560
resp (serv .handler .TextDocumentMoniker (ctx , logger , & param ))
539
561
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
+ }
541
567
}
542
568
}
543
569
0 commit comments