-
Notifications
You must be signed in to change notification settings - Fork 25
/
utils.go
150 lines (139 loc) · 3.61 KB
/
utils.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// Package lsp contains Language Server Protocol utility functions.
package lsp
import (
"context"
"fmt"
"log"
"path/filepath"
"github.com/fhs/acme-lsp/internal/lsp/protocol"
"github.com/fhs/acme-lsp/internal/lsp/text"
)
func ServerProvidesCodeAction(cap *protocol.ServerCapabilities, kind protocol.CodeActionKind) bool {
switch ap := cap.CodeActionProvider.(type) {
case bool:
return ap
case map[string]interface{}:
opt, err := protocol.ToCodeActionOptions(ap)
if err != nil {
log.Printf("failed to decode CodeActionOptions: %v", err)
return false
}
for _, k := range opt.CodeActionKinds {
if k == kind {
return true
}
}
}
return false
}
func CompatibleCodeActions(cap *protocol.ServerCapabilities, kinds []protocol.CodeActionKind) []protocol.CodeActionKind {
switch ap := cap.CodeActionProvider.(type) {
case bool:
if ap {
return kinds
}
return nil
case map[string]interface{}:
opt, err := protocol.ToCodeActionOptions(ap)
if err != nil {
log.Printf("failed to decode CodeActionOptions: %v", err)
return nil
}
var compat []protocol.CodeActionKind
for _, k := range kinds {
found := false
for _, kk := range opt.CodeActionKinds {
if k == kk {
found = true
break
}
}
if found {
compat = append(compat, k)
} else {
log.Printf("code action %v is not compatible with server", k)
}
}
return compat
}
return nil
}
func LocationLink(l *protocol.Location) string {
p := text.ToPath(l.URI)
return fmt.Sprintf("%s:%v:%v-%v:%v", p,
l.Range.Start.Line+1, l.Range.Start.Character+1,
l.Range.End.Line+1, l.Range.End.Character+1)
}
func DidOpen(ctx context.Context, server protocol.Server, filename string, body []byte) error {
return server.DidOpen(ctx, &protocol.DidOpenTextDocumentParams{
TextDocument: protocol.TextDocumentItem{
URI: text.ToURI(filename),
LanguageID: DetectLanguage(filename),
Version: 0,
Text: string(body),
},
})
}
func DidClose(ctx context.Context, server protocol.Server, filename string) error {
return server.DidClose(ctx, &protocol.DidCloseTextDocumentParams{
TextDocument: protocol.TextDocumentIdentifier{
URI: text.ToURI(filename),
},
})
}
func DidSave(ctx context.Context, server protocol.Server, filename string) error {
return server.DidSave(ctx, &protocol.DidSaveTextDocumentParams{
TextDocument: protocol.VersionedTextDocumentIdentifier{
TextDocumentIdentifier: protocol.TextDocumentIdentifier{
URI: text.ToURI(filename),
},
// TODO(fhs): add text field for includeText option
},
})
}
func DidChange(ctx context.Context, server protocol.Server, filename string, body []byte) error {
return server.DidChange(ctx, &protocol.DidChangeTextDocumentParams{
TextDocument: protocol.VersionedTextDocumentIdentifier{
TextDocumentIdentifier: protocol.TextDocumentIdentifier{
URI: text.ToURI(filename),
},
},
ContentChanges: []protocol.TextDocumentContentChangeEvent{
{
Text: string(body),
},
},
})
}
func DetectLanguage(filename string) string {
switch base := filepath.Base(filename); base {
case "go.mod", "go.sum":
return base
}
lang := filepath.Ext(filename)
if len(lang) == 0 {
return lang
}
if lang[0] == '.' {
lang = lang[1:]
}
switch lang {
case "py":
lang = "python"
}
return lang
}
func DirsToWorkspaceFolders(dirs []string) ([]protocol.WorkspaceFolder, error) {
var workspaces []protocol.WorkspaceFolder
for _, d := range dirs {
d, err := filepath.Abs(d)
if err != nil {
return nil, err
}
workspaces = append(workspaces, protocol.WorkspaceFolder{
URI: text.ToURI(d),
Name: d,
})
}
return workspaces, nil
}