LSP-server-example
44 строки · 1.1 Кб
1// //nolint
2
3// Copyright 2022 The Go Authors. All rights reserved.
4// Use of this source code is governed by a BSD-style
5// license that can be found in the LICENSE file.
6
7package protocol
8
9import (
10"encoding/json"
11"errors"
12)
13
14// DocumentChanges is a union of a file edit and directory rename operations
15// for package renaming feature. At most one field of this struct is non-nil.
16type DocumentChanges struct {
17TextDocumentEdit *TextDocumentEdit
18RenameFile *RenameFile
19}
20
21func (d *DocumentChanges) UnmarshalJSON(data []byte) error {
22var m map[string]interface{}
23
24if err := json.Unmarshal(data, &m); err != nil {
25return err
26}
27
28if _, ok := m["textDocument"]; ok {
29d.TextDocumentEdit = new(TextDocumentEdit)
30return json.Unmarshal(data, d.TextDocumentEdit)
31}
32
33d.RenameFile = new(RenameFile)
34return json.Unmarshal(data, d.RenameFile)
35}
36
37func (d *DocumentChanges) MarshalJSON() ([]byte, error) {
38if d.TextDocumentEdit != nil {
39return json.Marshal(d.TextDocumentEdit)
40} else if d.RenameFile != nil {
41return json.Marshal(d.RenameFile)
42}
43return nil, errors.New("empty DocumentChanges union value")
44}
45