forked from tliron/glsp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtext-document-synchronization.go
345 lines (282 loc) · 9.65 KB
/
text-document-synchronization.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
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
package protocol
import (
"encoding/json"
"github.com/tliron/glsp"
)
// https://microsoft.github.io/language-server-protocol/specification#textDocument_synchronization
type TextDocumentSyncKind Integer
/**
* Defines how the host (editor) should sync document changes to the language
* server.
*/
const (
/**
* Documents should not be synced at all.
*/
TextDocumentSyncKindNone = TextDocumentSyncKind(0)
/**
* Documents are synced by always sending the full content
* of the document.
*/
TextDocumentSyncKindFull = TextDocumentSyncKind(1)
/**
* Documents are synced by sending the full content on open.
* After that only incremental updates to the document are
* send.
*/
TextDocumentSyncKindIncremental = TextDocumentSyncKind(2)
)
// https://microsoft.github.io/language-server-protocol/specification#textDocument_didOpen
const MethodTextDocumentDidOpen = Method("textDocument/didOpen")
type TextDocumentDidOpenFunc func(context *glsp.Context, params *DidOpenTextDocumentParams) error
type DidOpenTextDocumentParams struct {
/**
* The document that was opened.
*/
TextDocument TextDocumentItem `json:"textDocument"`
}
// https://microsoft.github.io/language-server-protocol/specification#textDocument_didChange
/**
* Describe options to be used when registering for text document change events.
*/
type TextDocumentChangeRegistrationOptions struct {
TextDocumentRegistrationOptions
/**
* How documents are synced to the server. See TextDocumentSyncKind.Full
* and TextDocumentSyncKind.Incremental.
*/
SyncKind TextDocumentSyncKind `json:"syncKind"`
}
const MethodTextDocumentDidChange = Method("textDocument/didChange")
type TextDocumentDidChangeFunc func(context *glsp.Context, params *DidChangeTextDocumentParams) error
type DidChangeTextDocumentParams struct {
/**
* The document that did change. The version number points
* to the version after all provided content changes have
* been applied.
*/
TextDocument VersionedTextDocumentIdentifier `json:"textDocument"`
/**
* The actual content changes. The content changes describe single state
* changes to the document. So if there are two content changes c1 (at
* array index 0) and c2 (at array index 1) for a document in state S then
* c1 moves the document from S to S' and c2 from S' to S''. So c1 is
* computed on the state S and c2 is computed on the state S'.
*
* To mirror the content of a document using change events use the following
* approach:
* - start with the same initial content
* - apply the 'textDocument/didChange' notifications in the order you
* receive them.
* - apply the `TextDocumentContentChangeEvent`s in a single notification
* in the order you receive them.
*/
ContentChanges []interface{} `json:"contentChanges"` // TextDocumentContentChangeEvent or TextDocumentContentChangeEventWhole
}
// json.Unmarshaler interface
func (self *DidChangeTextDocumentParams) UnmarshalJSON(data []byte) error {
var value struct {
TextDocument VersionedTextDocumentIdentifier `json:"textDocument"`
ContentChanges []json.RawMessage `json:"contentChanges"` // TextDocumentContentChangeEvent or TextDocumentContentChangeEventWhole
}
if err := json.Unmarshal(data, &value); err == nil {
self.TextDocument = value.TextDocument
for _, contentChange := range value.ContentChanges {
var value_ TextDocumentContentChangeEvent
if err = json.Unmarshal(contentChange, &value_); err == nil {
self.ContentChanges = append(self.ContentChanges, value_)
} else {
var value_ TextDocumentContentChangeEventWhole
if err = json.Unmarshal(contentChange, &value_); err == nil {
self.ContentChanges = append(self.ContentChanges, value_)
} else {
return err
}
}
}
return nil
} else {
return err
}
}
/**
* An event describing a change to a text document. If range and rangeLength are
* omitted the new text is considered to be the full content of the document.
*/
type TextDocumentContentChangeEvent struct {
/**
* The range of the document that changed.
*/
Range Range `json:"range"`
/**
* The optional length of the range that got replaced.
*
* @deprecated use range instead.
*/
RangeLength *UInteger `json:"rangeLength,omitempty"`
/**
* The new text for the provided range.
*/
Text string `json:"text"`
}
type TextDocumentContentChangeEventWhole struct {
/**
* The new text of the whole document.
*/
Text string `json:"text"`
}
// https://microsoft.github.io/language-server-protocol/specification#textDocument_willSave
const MethodTextDocumentWillSave = Method("textDocument/willSave")
type TextDocumentWillSaveFunc func(context *glsp.Context, params *WillSaveTextDocumentParams) error
/**
* The parameters send in a will save text document notification.
*/
type WillSaveTextDocumentParams struct {
/**
* The document that will be saved.
*/
TextDocument TextDocumentIdentifier `json:"textDocument"`
/**
* The 'TextDocumentSaveReason'.
*/
Reason TextDocumentSaveReason `json:"reason"`
}
type TextDocumentSaveReason Integer
/**
* Represents reasons why a text document is saved.
*/
const (
/**
* Manually triggered, e.g. by the user pressing save, by starting
* debugging, or by an API call.
*/
TextDocumentSaveReasonManual = TextDocumentSaveReason(1)
/**
* Automatic after a delay.
*/
TextDocumentSaveReasonAfterDelay = TextDocumentSaveReason(2)
/**
* When the editor lost focus.
*/
TextDocumentSaveReasonFocusOut = TextDocumentSaveReason(3)
)
// https://microsoft.github.io/language-server-protocol/specification#textDocument_willSaveWaitUntil
const MethodTextDocumentWillSaveWaitUntil = Method("textDocument/willSaveWaitUntil")
type TextDocumentWillSaveWaitUntilFunc func(context *glsp.Context, params *WillSaveTextDocumentParams) ([]TextEdit, error)
// https://microsoft.github.io/language-server-protocol/specification#textDocument_didSave
type SaveOptions struct {
/**
* The client is supposed to include the content on save.
*/
IncludeText *bool `json:"includeText,omitempty"`
}
type TextDocumentSaveRegistrationOptions struct {
TextDocumentRegistrationOptions
/**
* The client is supposed to include the content on save.
*/
IncludeText *bool `json:"includeText"`
}
const MethodTextDocumentDidSave = Method("textDocument/didSave")
type TextDocumentDidSaveFunc func(context *glsp.Context, params *DidSaveTextDocumentParams) error
type DidSaveTextDocumentParams struct {
/**
* The document that was saved.
*/
TextDocument TextDocumentIdentifier `json:"textDocument"`
/**
* Optional the content when saved. Depends on the includeText value
* when the save notification was requested.
*/
Text *string `json:"text,omitempty"`
}
// https://microsoft.github.io/language-server-protocol/specification#textDocument_didClose
type TextDocumentSyncClientCapabilities struct {
/**
* Whether text document synchronization supports dynamic registration.
*/
DynamicRegistration *bool `json:"dynamicRegistration,omitempty"`
/**
* The client supports sending will save notifications.
*/
WillSave *bool `json:"willSave,omitempty"`
/**
* The client supports sending a will save request and
* waits for a response providing text edits which will
* be applied to the document before it is saved.
*/
WillSaveWaitUntil *bool `json:"willSaveWaitUntil,omitempty"`
/**
* The client supports did save notifications.
*/
DidSave *bool `json:"didSave,omitempty"`
}
type TextDocumentSyncOptions struct {
/**
* Open and close notifications are sent to the server. If omitted open
* close notification should not be sent.
*/
OpenClose *bool `json:"openClose,omitempty"`
/**
* Change notifications are sent to the server. See
* TextDocumentSyncKind.None, TextDocumentSyncKind.Full and
* TextDocumentSyncKind.Incremental. If omitted it defaults to
* TextDocumentSyncKind.None.
*/
Change *TextDocumentSyncKind `json:"change,omitempty"`
/**
* If present will save notifications are sent to the server. If omitted
* the notification should not be sent.
*/
WillSave *bool `json:"willSave,omitempty"`
/**
* If present will save wait until requests are sent to the server. If
* omitted the request should not be sent.
*/
WillSaveWaitUntil *bool `json:"willSaveWaitUntil,omitempty"`
/**
* If present save notifications are sent to the server. If omitted the
* notification should not be sent.
*/
Save interface{} `json:"save,omitempty"` // nil | bool | SaveOptions
}
// json.Unmarshaler interface
func (self *TextDocumentSyncOptions) UnmarshalJSON(data []byte) error {
var value struct {
OpenClose *bool `json:"openClose"`
Change *TextDocumentSyncKind `json:"change"`
WillSave *bool `json:"willSave"`
WillSaveWaitUntil *bool `json:"willSaveWaitUntil"`
Save json.RawMessage `json:"save"` // nil | bool | SaveOptions
}
if err := json.Unmarshal(data, &value); err == nil {
self.OpenClose = value.OpenClose
self.Change = value.Change
self.WillSave = value.WillSave
self.WillSaveWaitUntil = value.WillSaveWaitUntil
if value.Save != nil {
var value_ bool
if err = json.Unmarshal(value.Save, &value_); err == nil {
self.Save = value_
} else {
var value_ SaveOptions
if err = json.Unmarshal(value.Save, &value_); err == nil {
self.Save = value_
} else {
return err
}
}
}
return nil
} else {
return err
}
}
const MethodTextDocumentDidClose = Method("textDocument/didClose")
type TextDocumentDidCloseFunc func(context *glsp.Context, params *DidCloseTextDocumentParams) error
type DidCloseTextDocumentParams struct {
/**
* The document that was closed.
*/
TextDocument TextDocumentIdentifier `json:"textDocument"`
}