From 928728476e957341be3fde959ad972be63fa5ab5 Mon Sep 17 00:00:00 2001 From: Koichi Shiraishi Date: Tue, 23 Feb 2021 02:59:11 +0900 Subject: [PATCH] Support lsp 3.16 (#17) * codes: move to codes.go and romeve not JSON-RPC 2 specific codes * all: change int64 type to int32 for correspond to lsp 3.16 --- codes.go | 86 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ conn.go | 4 +-- errors.go | 31 +------------------- wire.go | 49 ++----------------------------- 4 files changed, 91 insertions(+), 79 deletions(-) create mode 100644 codes.go diff --git a/codes.go b/codes.go new file mode 100644 index 0000000..680d468 --- /dev/null +++ b/codes.go @@ -0,0 +1,86 @@ +// SPDX-FileCopyrightText: Copyright 2021 The Go Language Server Authors +// SPDX-License-Identifier: BSD-3-Clause + +package jsonrpc2 + +// Code is an error code as defined in the JSON-RPC spec. +type Code int32 + +// list of JSON-RPC error codes. +const ( + // ParseError is the invalid JSON was received by the server. + // An error occurred on the server while parsing the JSON text. + ParseError Code = -32700 + + // InvalidRequest is the JSON sent is not a valid Request object. + InvalidRequest Code = -32600 + + // MethodNotFound is the method does not exist / is not available. + MethodNotFound Code = -32601 + + // InvalidParams is the invalid method parameter(s). + InvalidParams Code = -32602 + + // InternalError is the internal JSON-RPC error. + InternalError Code = -32603 + + // JSONRPCReservedErrorRangeStart is the start range of JSON RPC reserved error codes. + // + // It doesn't denote a real error code. No LSP error codes should + // be defined between the start and end range. For backwards + // compatibility the "ServerNotInitialized" and the "UnknownErrorCode" + // are left in the range. + // + // @since 3.16.0. + JSONRPCReservedErrorRangeStart Code = -32099 + + // CodeServerErrorStart reserved for implementation-defined server-errors. + // + // Deprecated: Use JSONRPCReservedErrorRangeStart instead. + CodeServerErrorStart = JSONRPCReservedErrorRangeStart + + // ServerNotInitialized is the error of server not initialized. + ServerNotInitialized Code = -32002 + + // UnknownError should be used for all non coded errors. + UnknownError Code = -32001 + + // JSONRPCReservedErrorRangeEnd is the start range of JSON RPC reserved error codes. + // + // It doesn't denote a real error code. + // + // @since 3.16.0. + JSONRPCReservedErrorRangeEnd Code = -32000 + + // CodeServerErrorEnd reserved for implementation-defined server-errors. + // + // Deprecated: Use JSONRPCReservedErrorRangeEnd instead. + CodeServerErrorEnd = JSONRPCReservedErrorRangeEnd +) + +// This file contains the Go forms of the wire specification. +// +// See http://www.jsonrpc.org/specification for details. +// +// list of JSON-RPC errors. +var ( + // ErrUnknown should be used for all non coded errors. + ErrUnknown = NewError(UnknownError, "JSON-RPC unknown error") + + // ErrParse is used when invalid JSON was received by the server. + ErrParse = NewError(ParseError, "JSON-RPC parse error") + + // ErrInvalidRequest is used when the JSON sent is not a valid Request object. + ErrInvalidRequest = NewError(InvalidRequest, "JSON-RPC invalid request") + + // ErrMethodNotFound should be returned by the handler when the method does + // not exist / is not available. + ErrMethodNotFound = NewError(MethodNotFound, "JSON-RPC method not found") + + // ErrInvalidParams should be returned by the handler when method + // parameter(s) were invalid. + ErrInvalidParams = NewError(InvalidParams, "JSON-RPC invalid params") + + // ErrInternal is not currently returned but defined for completeness. + ErrInternal = NewError(InternalError, "JSON-RPC internal error") +) diff --git a/conn.go b/conn.go index 65721b8..34de352 100644 --- a/conn.go +++ b/conn.go @@ -65,7 +65,7 @@ type Conn interface { } type conn struct { - seq int64 // access atomically + seq int32 // access atomically writeMu sync.Mutex // protects writes to the stream stream Stream // supplied stream pendingMu sync.Mutex // protects the pending map @@ -88,7 +88,7 @@ func NewConn(s Stream) Conn { // Call implements Conn. func (c *conn) Call(ctx context.Context, method string, params, result interface{}) (id ID, err error) { // generate a new request identifier - id = NewNumberID(atomic.AddInt64(&c.seq, 1)) + id = NewNumberID(atomic.AddInt32(&c.seq, 1)) call, err := NewCall(id, method, params) if err != nil { return id, fmt.Errorf("marshaling call parameters: %w", err) diff --git a/errors.go b/errors.go index a262b66..38e6d2f 100644 --- a/errors.go +++ b/errors.go @@ -64,36 +64,7 @@ var _ error = (*constErr)(nil) // Error implements error.Error. func (e constErr) Error() string { return string(e) } -// This file contains the Go forms of the wire specification. -// -// See http://www.jsonrpc.org/specification for details. -// -// list of JSON-RPC errors. -var ( - // ErrUnknown should be used for all non coded errors. - ErrUnknown = NewError(UnknownError, "JSON-RPC unknown error") - - // ErrParse is used when invalid JSON was received by the server. - ErrParse = NewError(ParseError, "JSON-RPC parse error") - - // ErrInvalidRequest is used when the JSON sent is not a valid Request object. - ErrInvalidRequest = NewError(InvalidRequest, "JSON-RPC invalid request") - - // ErrMethodNotFound should be returned by the handler when the method does - // not exist / is not available. - ErrMethodNotFound = NewError(MethodNotFound, "JSON-RPC method not found") - - // ErrInvalidParams should be returned by the handler when method - // parameter(s) were invalid. - ErrInvalidParams = NewError(InvalidParams, "JSON-RPC invalid params") - - // ErrInternal is not currently returned but defined for completeness. - ErrInternal = NewError(InternalError, "JSON-RPC internal error") - - // ErrServerOverloaded is returned when a message was refused due to a - // server being temporarily unable to accept any new messages. - ErrServerOverloaded = NewError(ServerOverloaded, "JSON-RPC overloaded") - +const ( // ErrIdleTimeout is returned when serving timed out waiting for new connections. ErrIdleTimeout = constErr("timed out waiting for new connections") ) diff --git a/wire.go b/wire.go index a793d69..1fbc7b7 100644 --- a/wire.go +++ b/wire.go @@ -9,51 +9,6 @@ import ( "github.com/segmentio/encoding/json" ) -// Code is an int64 error code as defined in the JSON-RPC spec. -type Code int64 - -// list of JSON-RPC error codes. -const ( - // ParseError is the invalid JSON was received by the server. - // An error occurred on the server while parsing the JSON text. - ParseError Code = -32700 - - // InvalidRequest is the JSON sent is not a valid Request object. - InvalidRequest Code = -32600 - - // MethodNotFound is the method does not exist / is not available. - MethodNotFound Code = -32601 - - // InvalidParams is the invalid method parameter(s). - InvalidParams Code = -32602 - - // InternalError is the internal JSON-RPC error. - InternalError Code = -32603 - - // ServerNotInitialized is the error of server not initialized. - ServerNotInitialized Code = -32002 - - // UnknownError should be used for all non coded errors. - UnknownError Code = -32001 - - // RequestCancelled is the cancellation error. - // - // Defined by the Language Server Protocol. - RequestCancelled Code = -32800 - - // ContentModified is the state change that invalidates the result of a request in execution. - // - // Defined by the Language Server Protocol. - ContentModified Code = -32801 - - // ServerOverloaded is returned when a message was refused due to a - // server being temporarily unable to accept any new messages. - ServerOverloaded Code = -32000 - - codeServerErrorStart Code = -32099 - codeServerErrorEnd Code = -32000 -) - // Version represents a JSON-RPC version. const Version = "2.0" @@ -91,7 +46,7 @@ func (version) UnmarshalJSON(data []byte) error { // number form if the Name is the empty string. type ID struct { name string - number int64 + number int32 } // compile time check whether the ID implements a fmt.Formatter, json.Marshaler and json.Unmarshaler interfaces. @@ -102,7 +57,7 @@ var ( ) // NewNumberID returns a new number request ID. -func NewNumberID(v int64) ID { return ID{number: v} } +func NewNumberID(v int32) ID { return ID{number: v} } // NewStringID returns a new string request ID. func NewStringID(v string) ID { return ID{name: v} }