-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathlsp.go
More file actions
357 lines (303 loc) · 9.85 KB
/
lsp.go
File metadata and controls
357 lines (303 loc) · 9.85 KB
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
// Copyright 2025 CloudWeGo Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package lsp
import (
"context"
"encoding/json"
"fmt"
"path/filepath"
"strings"
sitter "github.com/smacker/go-tree-sitter"
"github.com/sourcegraph/go-lsp"
)
// The SymbolKind values are defined at https://microsoft.github.io/language-server-protocol/specification.
const (
SKUnknown SymbolKind = 1
SKFile SymbolKind = 1
SKModule SymbolKind = 2
SKNamespace SymbolKind = 3
SKPackage SymbolKind = 4
SKClass SymbolKind = 5
SKMethod SymbolKind = 6
SKProperty SymbolKind = 7
SKField SymbolKind = 8
SKConstructor SymbolKind = 9
SKEnum SymbolKind = 10
SKInterface SymbolKind = 11
SKFunction SymbolKind = 12
SKVariable SymbolKind = 13
SKConstant SymbolKind = 14
SKString SymbolKind = 15
SKNumber SymbolKind = 16
SKBoolean SymbolKind = 17
SKArray SymbolKind = 18
SKObject SymbolKind = 19
SKKey SymbolKind = 20
SKNull SymbolKind = 21
SKEnumMember SymbolKind = 22
SKStruct SymbolKind = 23
SKEvent SymbolKind = 24
SKOperator SymbolKind = 25
SKTypeParameter SymbolKind = 26
)
type SymbolKind = lsp.SymbolKind
type SymbolRole int
const (
DEFINITION SymbolRole = 1
REFERENCE SymbolRole = 2
)
type Position lsp.Position
func (r Position) Less(s Position) bool {
if r.Line != s.Line {
return r.Line < s.Line
}
return r.Character < s.Character
}
func (r Position) String() string {
return fmt.Sprintf("%d:%d", r.Line, r.Character)
}
type Range struct {
Start Position `json:"start"`
End Position `json:"end"`
}
func (r Range) String() string {
return fmt.Sprintf("%s-%s", r.Start, r.End)
}
func (r Range) MarshalText() ([]byte, error) {
return []byte(r.String()), nil
}
type _Range Range
func (r Range) MarshalJSON() ([]byte, error) {
return json.Marshal(_Range(r))
}
func isPositionInRange(pos Position, r Range, close bool) bool {
if pos.Line < r.Start.Line || pos.Line > r.End.Line {
return false
}
if pos.Line == r.Start.Line && pos.Character < r.Start.Character {
return false
}
if pos.Line == r.End.Line {
if close {
return pos.Character <= r.End.Character
} else {
return pos.Character < r.End.Character
}
}
return true
}
func (a Range) Include(b Range) bool {
return isPositionInRange(b.Start, a, false) && isPositionInRange(b.End, a, true)
}
type Location struct {
URI DocumentURI `json:"uri"`
Range Range `json:"range"`
}
func (l Location) String() string {
return fmt.Sprintf("%s:%d:%d-%d:%d", l.URI, l.Range.Start.Line, l.Range.Start.Character, l.Range.End.Line, l.Range.End.Character)
}
var locationMarshalJSONInline = true
func SetLocationMarshalJSONInline(inline bool) {
locationMarshalJSONInline = inline
}
type _Location Location
func (l Location) MarshalJSON() ([]byte, error) {
if locationMarshalJSONInline {
return []byte(fmt.Sprintf("%q", l.String())), nil
}
return json.Marshal(_Location(l))
}
func (l Location) MarshalText() ([]byte, error) {
return []byte(l.String()), nil
}
func (a Location) Include(b Location) bool {
if a == b {
return true
}
if a.URI != b.URI {
return false
}
return isPositionInRange(b.Range.Start, a.Range, false) && isPositionInRange(b.Range.End, a.Range, true)
}
type DocumentURI lsp.DocumentURI
func (l DocumentURI) File() string {
return strings.TrimPrefix(string(l), "file://")
}
func NewURI(file string) DocumentURI {
if !filepath.IsAbs(file) {
file, _ = filepath.Abs(file)
}
return DocumentURI("file://" + file)
}
type TextDocumentItem struct {
URI DocumentURI `json:"uri"`
LanguageID string `json:"languageId"`
Version int `json:"version"`
Text string `json:"text"`
LineCounts []int `json:"-"`
Symbols map[Range]*DocumentSymbol `json:"-"`
Definitions map[Position][]Location `json:"-"`
SemanticTokens *SemanticTokens `json:"-"`
}
type DocumentSymbol struct {
Name string `json:"name"`
Kind SymbolKind `json:"kind"`
Tags []json.RawMessage `json:"tags"`
Children []*DocumentSymbol `json:"children"`
Text string `json:"text"`
Tokens []Token `json:"tokens"`
Node *sitter.Node `json:"-"`
Role SymbolRole `json:"-"`
// Older LSPs might return SymbolInformation[] which have `Location`.
// Newer LSPs return DocumentSymbol[] which have `Range` and `SelectionRange`.
// ABCoder uses `Location`, and converts `Range` to `Location` when needed.
Location Location `json:"location"`
Range *Range `json:"range"`
SelectionRange *Range `json:"selectionRange"`
}
type TextDocumentPositionParams struct {
/**
* The text document.
*/
TextDocument TextDocumentIdentifier `json:"textDocument"`
/**
* The position inside the text document.
*/
Position Position `json:"position"`
}
type TextDocumentIdentifier struct {
/**
* The text document's URI.
*/
URI DocumentURI `json:"uri"`
}
type Hover struct {
Contents []MarkedString `json:"contents"`
Range *Range `json:"range,omitempty"`
}
type MarkedString markedString
type markedString struct {
Language string `json:"language"`
Value string `json:"value"`
isRawString bool
}
type WorkspaceSymbolParams struct {
Query string `json:"query"`
Limit int `json:"limit"`
}
type SymbolInformation struct {
Name string `json:"name"`
Kind SymbolKind `json:"kind"`
Location Location `json:"location"`
ContainerName string `json:"containerName,omitempty"`
}
// TypeHierarchyItem represents a node in the type hierarchy tree.
//
// @since 3.17.0
type TypeHierarchyItem struct {
Name string `json:"name"`
Kind SymbolKind `json:"kind"`
Detail string `json:"detail,omitempty"`
URI DocumentURI `json:"uri"`
Range Range `json:"range"`
SelectionRange Range `json:"selectionRange"`
Data interface{} `json:"data,omitempty"`
}
func (cli *LSPClient) WorkspaceSymbols(ctx context.Context, query string) ([]DocumentSymbol, error) {
req := WorkspaceSymbolParams{
Query: query,
}
var resp []DocumentSymbol
if err := cli.Call(ctx, "workspace/symbol", req, &resp); err != nil {
return nil, err
}
return resp, nil
}
func (s *DocumentSymbol) MarshalJSON() ([]byte, error) {
if s == nil {
return []byte("null"), nil
}
r := *s
if js, err := json.Marshal(r); err != nil {
return nil, err
} else {
return js, nil
}
}
func (s *DocumentSymbol) MarshalText() ([]byte, error) {
return []byte(s.String()), nil
}
func (s *DocumentSymbol) String() string {
if s == nil {
return "null"
}
return fmt.Sprintf("%s %s %s", s.Name, s.Kind, s.Location)
}
type SemanticTokens struct {
ResultID string `json:"resultId"`
Data []uint32 `json:"data"`
}
type Token struct {
Location Location `json:"location"`
Type string `json:"type"`
Modifiers []string `json:"modifiers"`
Text string `json:"text"`
}
func (t *Token) String() string {
return fmt.Sprintf("%s %s %v %s", t.Text, t.Type, t.Modifiers, t.Location)
}
func (cli *LSPClient) Hover(ctx context.Context, uri DocumentURI, line, character int) (*Hover, error) {
if cli.provider != nil {
// The type assertion is safe because the provider is for the specific language.
return cli.provider.Hover(ctx, cli, uri, line, character)
}
// Default hover implementation (or return an error if not supported)
// Default implementation (or return an error if not supported)
return nil, fmt.Errorf("Hover not supported for this language")
}
func (cli *LSPClient) Implementation(ctx context.Context, uri DocumentURI, pos Position) ([]Location, error) {
if cli.provider != nil {
return cli.provider.Implementation(ctx, cli, uri, pos)
}
// Default implementation (or return an error if not supported)
return nil, fmt.Errorf("implementation not supported for this language")
}
func (cli *LSPClient) WorkspaceSearchSymbols(ctx context.Context, query string) ([]SymbolInformation, error) {
if cli.provider != nil {
return cli.provider.WorkspaceSearchSymbols(ctx, cli, query)
}
// Default implementation (or return an error if not supported)
return nil, fmt.Errorf("WorkspaceSearchSymbols not supported for this language")
}
func (cli *LSPClient) PrepareTypeHierarchy(ctx context.Context, uri DocumentURI, pos Position) ([]TypeHierarchyItem, error) {
if cli.provider != nil {
return cli.provider.PrepareTypeHierarchy(ctx, cli, uri, pos)
}
// Default implementation (or return an error if not supported)
return nil, fmt.Errorf("PrepareTypeHierarchy not supported for this language")
}
func (cli *LSPClient) TypeHierarchySupertypes(ctx context.Context, item TypeHierarchyItem) ([]TypeHierarchyItem, error) {
if cli.provider != nil {
return cli.provider.TypeHierarchySupertypes(ctx, cli, item)
}
// Default implementation (or return an error if not supported)
return nil, fmt.Errorf("TypeHierarchySupertypes not supported for this language")
}
func (cli *LSPClient) TypeHierarchySubtypes(ctx context.Context, item TypeHierarchyItem) ([]TypeHierarchyItem, error) {
if cli.provider != nil {
return cli.provider.TypeHierarchySubtypes(ctx, cli, item)
}
// Default implementation (or return an error if not supported)
return nil, fmt.Errorf("TypeHierarchySubtypes not supported for this language")
}