| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package lsprpc |
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
import ( |
| 10 |
"context" |
| 11 |
"encoding/json" |
| 12 |
"fmt" |
| 13 |
|
| 14 |
"golang.org/x/tools/gopls/internal/protocol" |
| 15 |
"golang.org/x/tools/internal/event" |
| 16 |
jsonrpc2_v2 "golang.org/x/tools/internal/jsonrpc2_v2" |
| 17 |
) |
| 18 |
|
| 19 |
const HandshakeMethod = handshakeMethod |
| 20 |
|
| 21 |
|
| 22 |
type ServerFunc func(context.Context, protocol.ClientCloser) protocol.Server |
| 23 |
|
| 24 |
type Canceler struct { |
| 25 |
Conn *jsonrpc2_v2.Connection |
| 26 |
} |
| 27 |
|
| 28 |
func (c *Canceler) Preempt(ctx context.Context, req *jsonrpc2_v2.Request) (any, error) { |
| 29 |
if req.Method != "$/cancelRequest" { |
| 30 |
return nil, jsonrpc2_v2.ErrNotHandled |
| 31 |
} |
| 32 |
var params protocol.CancelParams |
| 33 |
if err := json.Unmarshal(req.Params, ¶ms); err != nil { |
| 34 |
return nil, fmt.Errorf("%w: %v", jsonrpc2_v2.ErrParse, err) |
| 35 |
} |
| 36 |
id, err := jsonrpc2_v2.MakeID(params.ID) |
| 37 |
if err != nil { |
| 38 |
return nil, fmt.Errorf("%w: invalid ID type %T", jsonrpc2_v2.ErrParse, params.ID) |
| 39 |
} |
| 40 |
c.Conn.Cancel(id) |
| 41 |
return nil, nil |
| 42 |
} |
| 43 |
|
| 44 |
type ForwardBinder struct { |
| 45 |
dialer jsonrpc2_v2.Dialer |
| 46 |
onBind func(*jsonrpc2_v2.Connection) |
| 47 |
} |
| 48 |
|
| 49 |
func NewForwardBinder(dialer jsonrpc2_v2.Dialer) *ForwardBinder { |
| 50 |
return &ForwardBinder{ |
| 51 |
dialer: dialer, |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
func (b *ForwardBinder) Bind(ctx context.Context, conn *jsonrpc2_v2.Connection) (opts jsonrpc2_v2.ConnectionOptions) { |
| 56 |
client := protocol.ClientDispatcherV2(conn) |
| 57 |
clientBinder := NewClientBinder(func(context.Context, protocol.Server) protocol.Client { return client }) |
| 58 |
|
| 59 |
serverConn, err := jsonrpc2_v2.Dial(context.Background(), b.dialer, clientBinder, nil) |
| 60 |
if err != nil { |
| 61 |
return jsonrpc2_v2.ConnectionOptions{ |
| 62 |
Handler: jsonrpc2_v2.HandlerFunc(func(context.Context, *jsonrpc2_v2.Request) (any, error) { |
| 63 |
return nil, fmt.Errorf("%w: %v", jsonrpc2_v2.ErrInternal, err) |
| 64 |
}), |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
if b.onBind != nil { |
| 69 |
b.onBind(serverConn) |
| 70 |
} |
| 71 |
server := protocol.ServerDispatcherV2(serverConn) |
| 72 |
preempter := &Canceler{ |
| 73 |
Conn: conn, |
| 74 |
} |
| 75 |
detached := context.WithoutCancel(ctx) |
| 76 |
go func() { |
| 77 |
conn.Wait() |
| 78 |
if err := serverConn.Close(); err != nil { |
| 79 |
event.Log(detached, fmt.Sprintf("closing remote connection: %v", err)) |
| 80 |
} |
| 81 |
}() |
| 82 |
return jsonrpc2_v2.ConnectionOptions{ |
| 83 |
Handler: protocol.ServerHandlerV2(server), |
| 84 |
Preempter: preempter, |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
func NewClientBinder(newClient ClientFunc) *clientBinder { |
| 89 |
return &clientBinder{newClient} |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
type ClientFunc func(context.Context, protocol.Server) protocol.Client |
| 94 |
|
| 95 |
|
| 96 |
type clientBinder struct { |
| 97 |
newClient ClientFunc |
| 98 |
} |
| 99 |
|
| 100 |
func (b *clientBinder) Bind(ctx context.Context, conn *jsonrpc2_v2.Connection) jsonrpc2_v2.ConnectionOptions { |
| 101 |
server := protocol.ServerDispatcherV2(conn) |
| 102 |
client := b.newClient(ctx, server) |
| 103 |
return jsonrpc2_v2.ConnectionOptions{ |
| 104 |
Handler: protocol.ClientHandlerV2(client), |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
type HandlerMiddleware func(jsonrpc2_v2.Handler) jsonrpc2_v2.Handler |
| 110 |
|
| 111 |
|
| 112 |
func BindHandler(hmw HandlerMiddleware) Middleware { |
| 113 |
return Middleware(func(binder jsonrpc2_v2.Binder) jsonrpc2_v2.Binder { |
| 114 |
return BinderFunc(func(ctx context.Context, conn *jsonrpc2_v2.Connection) jsonrpc2_v2.ConnectionOptions { |
| 115 |
opts := binder.Bind(ctx, conn) |
| 116 |
opts.Handler = hmw(opts.Handler) |
| 117 |
return opts |
| 118 |
}) |
| 119 |
}) |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
type BinderFunc func(ctx context.Context, conn *jsonrpc2_v2.Connection) jsonrpc2_v2.ConnectionOptions |
| 125 |
|
| 126 |
func (f BinderFunc) Bind(ctx context.Context, conn *jsonrpc2_v2.Connection) jsonrpc2_v2.ConnectionOptions { |
| 127 |
return f(ctx, conn) |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
type Middleware func(jsonrpc2_v2.Binder) jsonrpc2_v2.Binder |
| 133 |
|
| 134 |
var GetGoEnv = getGoEnv |