Skip to content

Commit 79f6e43

Browse files
committed
WIP9
Signed-off-by: Koichi Shiraishi <zchee.io@gmail.com>
1 parent 123b56b commit 79f6e43

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+11260
-29607
lines changed

base.go

Lines changed: 36 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,96 +1,57 @@
1-
// SPDX-FileCopyrightText: 2021 The Go Language Server Authors
1+
// Copyright 2024 The Go Language Server Authors
22
// SPDX-License-Identifier: BSD-3-Clause
33

44
package protocol
55

6-
import (
7-
"fmt"
6+
// ErrorCodes predefined error codes.
7+
type ErrorCodes int32
88

9-
"github.com/segmentio/encoding/json"
10-
)
9+
const (
10+
ParseErrorErrorCodes ErrorCodes = -32700
1111

12-
// CancelParams params of cancelRequest.
13-
type CancelParams struct {
14-
// ID is the request id to cancel.
15-
ID interface{} `json:"id"` // int32 | string
16-
}
12+
InvalidRequestErrorCodes ErrorCodes = -32600
1713

18-
// ProgressParams params of Progress netification.
19-
//
20-
// @since 3.15.0.
21-
type ProgressParams struct {
22-
// Token is the progress token provided by the client or server.
23-
Token ProgressToken `json:"token"`
14+
MethodNotFoundErrorCodes ErrorCodes = -32601
2415

25-
// Value is the progress data.
26-
Value interface{} `json:"value"`
27-
}
16+
InvalidParamsErrorCodes ErrorCodes = -32602
2817

29-
// ProgressToken is the progress token provided by the client or server.
30-
//
31-
// @since 3.15.0.
32-
type ProgressToken struct {
33-
name string
34-
number int32
35-
}
18+
InternalErrorErrorCodes ErrorCodes = -32603
19+
20+
// ServerNotInitializedErrorCodes error code indicating that a server received a notification or request before the server has received the `initialize` request.
21+
ServerNotInitializedErrorCodes ErrorCodes = -32002
3622

37-
// compile time check whether the ProgressToken implements a fmt.Formatter, fmt.Stringer, json.Marshaler and json.Unmarshaler interfaces.
38-
var (
39-
_ fmt.Formatter = (*ProgressToken)(nil)
40-
_ fmt.Stringer = (*ProgressToken)(nil)
41-
_ json.Marshaler = (*ProgressToken)(nil)
42-
_ json.Unmarshaler = (*ProgressToken)(nil)
23+
UnknownErrorCodeErrorCodes ErrorCodes = -32001
4324
)
4425

45-
// NewProgressToken returns a new ProgressToken.
46-
func NewProgressToken(s string) *ProgressToken {
47-
return &ProgressToken{name: s}
48-
}
26+
type LSPErrorCodes int32
4927

50-
// NewNumberProgressToken returns a new number ProgressToken.
51-
func NewNumberProgressToken(n int32) *ProgressToken {
52-
return &ProgressToken{number: n}
53-
}
28+
const (
29+
// RequestFailedLSPErrorCodes a request failed but it was syntactically correct, e.g the method name was known and the parameters were valid. The error message should contain human readable information about why the request failed.
30+
//
31+
// @since 3.17.0
32+
RequestFailedLSPErrorCodes LSPErrorCodes = -32803
5433

55-
// Format writes the ProgressToken to the formatter.
56-
//
57-
// If the rune is q the representation is non ambiguous,
58-
// string forms are quoted.
59-
func (v ProgressToken) Format(f fmt.State, r rune) {
60-
const numF = `%d`
61-
strF := `%s`
62-
if r == 'q' {
63-
strF = `%q`
64-
}
65-
66-
switch {
67-
case v.name != "":
68-
fmt.Fprintf(f, strF, v.name)
69-
default:
70-
fmt.Fprintf(f, numF, v.number)
71-
}
72-
}
34+
// ServerCancelledLSPErrorCodes the server cancelled the request. This error code should only be used for requests that explicitly support being server cancellable.
35+
//
36+
// @since 3.17.0
37+
ServerCancelledLSPErrorCodes LSPErrorCodes = -32802
7338

74-
// String returns a string representation of the ProgressToken.
75-
func (v ProgressToken) String() string {
76-
return fmt.Sprint(v) //nolint:gocritic
77-
}
39+
// ContentModifiedLSPErrorCodes the server detected that the content of a document got modified outside normal conditions. A server should NOT send this error code if it detects a content change in it unprocessed messages. The result even computed on an older state might still be useful for the client. If a client decides that a result is not of any use anymore the client should cancel the request.
40+
ContentModifiedLSPErrorCodes LSPErrorCodes = -32801
7841

79-
// MarshalJSON implements json.Marshaler.
80-
func (v *ProgressToken) MarshalJSON() ([]byte, error) {
81-
if v.name != "" {
82-
return json.Marshal(v.name)
83-
}
42+
// RequestCancelledLSPErrorCodes the client has canceled a request and a server has detected the cancel.
43+
RequestCancelledLSPErrorCodes LSPErrorCodes = -32800
44+
)
8445

85-
return json.Marshal(v.number)
46+
type CancelParams struct {
47+
// ID the request id to cancel.
48+
ID CancelParamsID `json:"id"`
8649
}
8750

88-
// UnmarshalJSON implements json.Unmarshaler.
89-
func (v *ProgressToken) UnmarshalJSON(data []byte) error {
90-
*v = ProgressToken{}
91-
if err := json.Unmarshal(data, &v.number); err == nil {
92-
return nil
93-
}
51+
type ProgressParams struct {
52+
// Token the progress token provided by the client or server.
53+
Token ProgressToken `json:"token"`
9454

95-
return json.Unmarshal(data, &v.name)
55+
// Value the progress data.
56+
Value any `json:"value"`
9657
}

base_test.go

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,14 @@ import (
99

1010
"github.com/google/go-cmp/cmp"
1111
"github.com/google/go-cmp/cmp/cmpopts"
12-
"github.com/segmentio/encoding/json"
1312
)
1413

1514
func TestCancelParams(t *testing.T) {
1615
t.Parallel()
1716

1817
const want = `{"id":"testID"}`
1918
wantType := CancelParams{
20-
ID: "testID",
19+
ID: NewCancelParamsID("testID"),
2120
}
2221

2322
t.Run("Marshal", func(t *testing.T) {
@@ -40,11 +39,10 @@ func TestCancelParams(t *testing.T) {
4039
}
4140

4241
for _, tt := range tests {
43-
tt := tt
4442
t.Run(tt.name, func(t *testing.T) {
4543
t.Parallel()
4644

47-
got, err := json.Marshal(&tt.field)
45+
got, err := marshal(&tt.field)
4846
if (err != nil) != tt.wantMarshalErr {
4947
t.Fatal(err)
5048
}
@@ -76,16 +74,17 @@ func TestCancelParams(t *testing.T) {
7674
}
7775

7876
for _, tt := range tests {
79-
tt := tt
8077
t.Run(tt.name, func(t *testing.T) {
8178
t.Parallel()
8279

8380
var got CancelParams
84-
if err := json.Unmarshal([]byte(tt.field), &got); (err != nil) != tt.wantUnmarshalErr {
81+
if err := unmarshal([]byte(tt.field), &got); (err != nil) != tt.wantUnmarshalErr {
8582
t.Fatal(err)
8683
}
8784

88-
if diff := cmp.Diff(tt.want, got); (diff != "") != tt.wantErr {
85+
if diff := cmp.Diff(tt.want, got,
86+
cmpopts.EquateComparable(CancelParams{}),
87+
); (diff != "") != tt.wantErr {
8988
t.Errorf("%s: wantErr: %t\n(-want +got)\n%s", tt.name, tt.wantErr, diff)
9089
}
9190
})
@@ -101,7 +100,7 @@ func TestProgressParams(t *testing.T) {
101100

102101
token := NewProgressToken(wantWorkDoneToken)
103102
wantType := ProgressParams{
104-
Token: *token,
103+
Token: token,
105104
Value: "testValue",
106105
}
107106

@@ -125,11 +124,10 @@ func TestProgressParams(t *testing.T) {
125124
}
126125

127126
for _, tt := range tests {
128-
tt := tt
129127
t.Run(tt.name, func(t *testing.T) {
130128
t.Parallel()
131129

132-
got, err := json.Marshal(&tt.field)
130+
got, err := marshal(&tt.field)
133131
if (err != nil) != tt.wantMarshalErr {
134132
t.Fatal(err)
135133
}
@@ -161,12 +159,11 @@ func TestProgressParams(t *testing.T) {
161159
}
162160

163161
for _, tt := range tests {
164-
tt := tt
165162
t.Run(tt.name, func(t *testing.T) {
166163
t.Parallel()
167164

168165
var got ProgressParams
169-
if err := json.Unmarshal([]byte(tt.field), &got); (err != nil) != tt.wantUnmarshalErr {
166+
if err := unmarshal([]byte(tt.field), &got); (err != nil) != tt.wantUnmarshalErr {
170167
t.Fatal(err)
171168
}
172169

@@ -175,7 +172,7 @@ func TestProgressParams(t *testing.T) {
175172
}
176173

177174
if token := got.Token; !reflect.ValueOf(token).IsZero() {
178-
if diff := cmp.Diff(token.String(), wantWorkDoneToken); (diff != "") != tt.wantErr {
175+
if diff := cmp.Diff(token, wantWorkDoneToken); (diff != "") != tt.wantErr {
179176
t.Errorf("%s: wantErr: %t\n(-want +got)\n%s", tt.name, tt.wantErr, diff)
180177
}
181178
}

0 commit comments

Comments
 (0)