Skip to content

Commit

Permalink
Support lsp 3.16 (#17)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
zchee authored Feb 22, 2021
1 parent 4ffaa77 commit 9287284
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 79 deletions.
86 changes: 86 additions & 0 deletions codes.go
Original file line number Diff line number Diff line change
@@ -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")
)
4 changes: 2 additions & 2 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
31 changes: 1 addition & 30 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
)
49 changes: 2 additions & 47 deletions wire.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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.
Expand All @@ -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} }
Expand Down

0 comments on commit 9287284

Please sign in to comment.