mirror of
https://github.com/golang/go
synced 2024-11-18 16:14:46 -07:00
5091d647ee
Code generation has been unified, so that tsprotocol.go and tsserver.go are produced by the same program. tsprotocol.go is about 900 lines shorter, partly from removing boilerplate comments that golint no longer requires. (And partly by generating fewer unneeded types.) The choice made for a union type is commented with the set of types. There is no Go equivalent for union types, but making themn all interface{} would replace type checking at unmarshalling with checking runtime conversions. Intersection types (A&B) are sometimes embedded (struct{A;B;}, and sometimes expanded, as they have to be if A and B have fields with the same names. There are fewer embedded structs, which had been verbose and confusing to initialize. They have been replaced by types whose names end in Gn. Essentially all the generated *structs have been removed. This makes no difference in what the client sends, and the server may send a {} where it previously might have sent nothing. The benefit is that some nil tests can be removed. Thus 'omitempty' in json tags is just documentation that the element is optional in the protocol. The files that generate this code will be submitted later, but soon. Change-Id: I52b997d9c58de3d733fc8c6ce061e47ce2bdb100 Reviewed-on: https://go-review.googlesource.com/c/tools/+/207598 Run-TryBot: Peter Weinberger <pjw@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Ian Cottrell <iancottrell@google.com>
92 lines
2.4 KiB
Go
92 lines
2.4 KiB
Go
// Copyright 2018 The Go Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package protocol
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
|
|
"golang.org/x/tools/internal/jsonrpc2"
|
|
"golang.org/x/tools/internal/telemetry/log"
|
|
"golang.org/x/tools/internal/telemetry/trace"
|
|
"golang.org/x/tools/internal/xcontext"
|
|
)
|
|
|
|
const (
|
|
// RequestCancelledError should be used when a request is cancelled early.
|
|
RequestCancelledError = -32800
|
|
)
|
|
|
|
type DocumentUri = string
|
|
|
|
type canceller struct{ jsonrpc2.EmptyHandler }
|
|
|
|
type clientHandler struct {
|
|
canceller
|
|
client Client
|
|
}
|
|
|
|
type serverHandler struct {
|
|
canceller
|
|
server Server
|
|
}
|
|
|
|
func (canceller) Request(ctx context.Context, conn *jsonrpc2.Conn, direction jsonrpc2.Direction, r *jsonrpc2.WireRequest) context.Context {
|
|
if direction == jsonrpc2.Receive && r.Method == "$/cancelRequest" {
|
|
var params CancelParams
|
|
if err := json.Unmarshal(*r.Params, ¶ms); err != nil {
|
|
log.Error(ctx, "", err)
|
|
} else {
|
|
v := jsonrpc2.ID{}
|
|
if n, ok := params.ID.(float64); ok {
|
|
v.Number = int64(n)
|
|
} else if s, ok := params.ID.(string); ok {
|
|
v.Name = s
|
|
} else {
|
|
log.Error(ctx, fmt.Sprintf("Request ID %v malformed", params.ID), nil)
|
|
return ctx
|
|
}
|
|
conn.Cancel(v)
|
|
}
|
|
}
|
|
return ctx
|
|
}
|
|
|
|
func (canceller) Cancel(ctx context.Context, conn *jsonrpc2.Conn, id jsonrpc2.ID, cancelled bool) bool {
|
|
if cancelled {
|
|
return false
|
|
}
|
|
ctx = xcontext.Detach(ctx)
|
|
ctx, done := trace.StartSpan(ctx, "protocol.canceller")
|
|
defer done()
|
|
conn.Notify(ctx, "$/cancelRequest", &CancelParams{ID: id})
|
|
return true
|
|
}
|
|
|
|
func NewClient(ctx context.Context, stream jsonrpc2.Stream, client Client) (context.Context, *jsonrpc2.Conn, Server) {
|
|
ctx = WithClient(ctx, client)
|
|
conn := jsonrpc2.NewConn(stream)
|
|
conn.AddHandler(&clientHandler{client: client})
|
|
return ctx, conn, &serverDispatcher{Conn: conn}
|
|
}
|
|
|
|
func NewServer(ctx context.Context, stream jsonrpc2.Stream, server Server) (context.Context, *jsonrpc2.Conn, Client) {
|
|
conn := jsonrpc2.NewConn(stream)
|
|
client := &clientDispatcher{Conn: conn}
|
|
ctx = WithClient(ctx, client)
|
|
conn.AddHandler(&serverHandler{server: server})
|
|
return ctx, conn, client
|
|
}
|
|
|
|
func sendParseError(ctx context.Context, req *jsonrpc2.Request, err error) {
|
|
if _, ok := err.(*jsonrpc2.Error); !ok {
|
|
err = jsonrpc2.NewErrorf(jsonrpc2.CodeParseError, "%v", err)
|
|
}
|
|
if err := req.Reply(ctx, nil, err); err != nil {
|
|
log.Error(ctx, "", err)
|
|
}
|
|
}
|