Skip to content

Commit 92b0697

Browse files
committed
Correctly interpret empty DocumentSymbol arrays
1 parent 8eb20fe commit 92b0697

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

lsp/protocol.go

+13
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,9 @@ func (entry *DocumentSymbolArrayOrSymbolInformationArray) UnmarshalJSON(raw []by
264264
if err := json.Unmarshal(raw, &intermediate); err != nil {
265265
return err
266266
}
267+
if len(intermediate) == 0 {
268+
return nil
269+
}
267270
discriminator := struct {
268271
Range *Range `json:"range,omitempty"`
269272
Location *Location `json:"location,omitempty"`
@@ -292,6 +295,16 @@ func (entry *DocumentSymbolArrayOrSymbolInformationArray) UnmarshalJSON(raw []by
292295
return nil
293296
}
294297

298+
func (entry DocumentSymbolArrayOrSymbolInformationArray) MarshalJSON() ([]byte, error) {
299+
if entry.DocumentSymbolArray != nil {
300+
return json.Marshal(entry.DocumentSymbolArray)
301+
}
302+
if entry.SymbolInformationArray != nil {
303+
return json.Marshal(entry.SymbolInformationArray)
304+
}
305+
return []byte("[]"), nil
306+
}
307+
295308
// ApplyWorkspaceEditParams structure according to LSP
296309
type ApplyWorkspaceEditParams struct {
297310
Label string `json:"label,omitempty"`

lsp/protocol_test.go

+10
Original file line numberDiff line numberDiff line change
@@ -159,4 +159,14 @@ func TestVariousMessages(t *testing.T) {
159159
err := json.Unmarshal([]byte(msg), &symbol)
160160
require.NoError(t, err)
161161
})
162+
163+
t.Run("EmptyDocumentSymbolMarshalUnmarshal", func(t *testing.T) {
164+
var symbol DocumentSymbolArrayOrSymbolInformationArray
165+
err := json.Unmarshal([]byte(`[]`), &symbol)
166+
require.NoError(t, err)
167+
data, err := json.Marshal(symbol)
168+
require.Equal(t, "[]", string(data))
169+
data, err = json.Marshal(&symbol)
170+
require.Equal(t, "[]", string(data))
171+
})
162172
}

0 commit comments

Comments
 (0)