Repository navigation
Expand file tree
/
Copy pathhandler.go
More file actions
73 lines (65 loc) · 1.91 KB
/
Copy pathhandler.go
File metadata and controls
73 lines (65 loc) · 1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package server
import (
contextpkg "context"
"fmt"
"github.com/sourcegraph/jsonrpc2"
"github.com/tliron/glsp"
)
// See: https://github.com/sourcegraph/go-langserver/blob/master/langserver/handler.go#L206
func (self *Server) newHandler() jsonrpc2.Handler {
return jsonrpc2.HandlerWithError(self.handle)
}
func (self *Server) handle(context contextpkg.Context, connection *jsonrpc2.Conn, request *jsonrpc2.Request) (any, error) {
glspContext := glsp.Context{
Method: request.Method,
Notify: func(method string, params any) {
if err := connection.Notify(context, method, params); err != nil {
self.Log.Error(err.Error())
}
},
Call: func(method string, params any, result any) {
if err := connection.Call(context, method, params, result); err != nil {
self.Log.Error(err.Error())
}
},
Context: context,
}
if request.Params != nil {
glspContext.Params = *request.Params
}
switch request.Method {
case "exit":
// We're giving the attached handler a chance to handle it first, but we'll ignore any result
self.Handler.Handle(&glspContext)
err := connection.Close()
return nil, err
default:
// Note: jsonrpc2 will not even call this function if reqest.Params is invalid JSON,
// so we don't need to handle jsonrpc2.CodeParseError here
result, validMethod, validParams, err := self.Handler.Handle(&glspContext)
if !validMethod {
return nil, &jsonrpc2.Error{
Code: jsonrpc2.CodeMethodNotFound,
Message: fmt.Sprintf("method not supported: %s", request.Method),
}
} else if !validParams {
if err == nil {
return nil, &jsonrpc2.Error{
Code: jsonrpc2.CodeInvalidParams,
}
} else {
return nil, &jsonrpc2.Error{
Code: jsonrpc2.CodeInvalidParams,
Message: err.Error(),
}
}
} else if err != nil {
return nil, &jsonrpc2.Error{
Code: jsonrpc2.CodeInvalidRequest,
Message: err.Error(),
}
} else {
return result, nil
}
}
}