-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathcompat.go
69 lines (62 loc) · 1.58 KB
/
compat.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package protocol
import (
"encoding/json"
"strings"
)
// tsprotocol.go is not general enough to support other LSP servers besides
// gopls. Let's try to be compatible with all LSP servers.
func (m *MarkupContent) UnmarshalJSON(data []byte) error {
d := strings.TrimSpace(string(data))
if len(d) == 0 {
return nil
}
switch d[0] {
case '"': // string (deprecated in LSP)
var s string
if err := json.Unmarshal(data, &s); err != nil {
return err
}
m.Kind = PlainText
m.Value = s
return nil
case '[': // []MarkedString (deprecated in LSP)
var mslist []MarkupContent
err := json.Unmarshal(data, &mslist)
if err != nil {
return err
}
var b strings.Builder
for i, ms := range mslist {
if i > 0 {
b.WriteRune('\n')
}
b.WriteString(ms.Value)
}
m.Kind = PlainText
m.Value = b.String()
return nil
}
// MarkedString (deprecated in LSP) or MarkedContent
type noUnmarshal MarkupContent
m.Kind = PlainText // for MarkedString
return json.Unmarshal(data, (*noUnmarshal)(m))
}
// CodeActionLiteralSupport is a type alias that works around difficulty in initializing the pointer
// InitializeParams.Capabilities.TextDocument.CodeAction.CodeActionLiteralSupport.
type CodeActionLiteralSupport = struct {
CodeActionKind struct {
ValueSet []CodeActionKind `json:"valueSet"`
} `json:"codeActionKind"`
}
func ToCodeActionOptions(v map[string]interface{}) (*CodeActionOptions, error) {
b, err := json.Marshal(v)
if err != nil {
return nil, err
}
var opt CodeActionOptions
err = json.Unmarshal(b, &opt)
if err != nil {
return nil, err
}
return &opt, nil
}