-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
text-document.rkt
529 lines (469 loc) · 21 KB
/
text-document.rkt
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
#lang racket/base
(require data/interval-map
json
racket/match
racket/list
racket/contract
racket/class
racket/format
racket/string
racket/set
racket/dict
"error-codes.rkt"
"interfaces.rkt"
"json-util.rkt"
"path-util.rkt"
"responses.rkt"
"symbol-kinds.rkt"
"docs-helpers.rkt"
"documentation-parser.rkt"
"doc.rkt"
"struct.rkt")
;;
;; Match Expanders
;;;;;;;;;;;;;;;;;;;;
(define-json-expander Location
[uri string?]
[range any/c])
(define-json-expander ContentChangeEvent
[range any/c]
[rangeLength exact-nonnegative-integer?]
[text string?])
;; VersionedTextDocumentIdentifier
(define-json-expander DocIdentifier
[version exact-nonnegative-integer?]
[uri string?])
;; TextDocumentItem
(define-json-expander DocItem
[uri string?]
[languageId string?]
[version exact-nonnegative-integer?]
[text string?])
(define-json-expander DocHighlight
[range any/c])
(define-json-expander SymbolInfo
[name string?]
[kind exact-positive-integer?]
[location any/c])
(define-json-expander InlayHint
; must a position `Pos`, defined in interfaces.rkt
[position any/c]
; should be `string | InlayHintLabelPart[]`
; but let's stay in simple case for now
[label string?])
(define (pos->abs-pos doc pos)
(match-define (Pos #:line line #:char char) pos)
(doc-pos doc line char))
(define (abs-pos->pos doc pos)
(define-values (line char) (doc-line/ch doc pos))
(Pos #:line line #:char char))
(define (start/end->range doc start end)
(Range #:start (abs-pos->pos doc start) #:end (abs-pos->pos doc end)))
;;
;; Methods
;;;;;;;;;;;;
(define open-docs (make-hasheq))
(define (did-open! params)
(match-define (hash-table ['textDocument (DocItem #:uri uri #:text text)]) params)
(hash-set! open-docs (string->symbol uri)
(new-doc uri text)))
(define (did-close! params)
(match-define (hash-table ['textDocument (DocItem #:uri uri)]) params)
(hash-remove! open-docs (string->symbol uri)))
(define (did-change! params)
(match-define (hash-table ['textDocument (DocIdentifier #:uri uri)]
['contentChanges content-changes]) params)
(define this-doc (hash-ref open-docs (string->symbol uri)))
(define content-changes*
(cond [(eq? (json-null) content-changes) empty]
[(list? content-changes) content-changes]
[else (list content-changes)]))
(doc-batch-change this-doc
(for ([change (in-list content-changes*)])
(match change
[(ContentChangeEvent #:range (Range #:start (Pos #:line st-ln #:char st-ch)
#:end (Pos #:line ed-ln #:char ed-ch))
#:text text)
(doc-update! this-doc st-ln st-ch ed-ln ed-ch text)]
[(ContentChangeEvent #:text text)
(doc-reset! this-doc text)])))
(void))
;; Hover request
;; Returns an object conforming to the Hover interface, to
;; be used as the result of the response message.
(define (hover id params)
(match params
[(hash-table ['textDocument (DocIdentifier #:uri uri)]
['position (Pos #:line line #:char ch)])
(define this-doc (hash-ref open-docs (string->symbol uri)))
(define doc-trace (Doc-trace this-doc))
(define hovers (send doc-trace get-hovers))
(define pos (doc-pos this-doc line ch))
(define-values (start end text)
(interval-map-ref/bounds hovers pos #f))
(match-define (list link tag)
(interval-map-ref (send doc-trace get-docs) pos (list #f #f)))
(define result
(cond [text
;; We want signatures from `scribble/blueboxes` as they have better indentation,
;; but in some super rare cases blueboxes aren't accessible, thus we try to use the
;; parsed signature instead
(match-define (list sigs args-descr)
(if tag
(get-docs-for-tag tag)
(list #f #f)))
(define maybe-signature
(if sigs
(~a "```\n"
(string-join sigs "\n")
(if args-descr (~a "\n" args-descr) "")
"\n```\n---\n")
#f))
(define documentation-text
(if link
(~a (or maybe-signature "")
(or (extract-documentation-for-selected-element
link #:include-signature? (not maybe-signature))
""))
""))
(define contents (if link
(~a text
" - [online docs]("
(make-proper-url-for-online-documentation link)
")\n"
(if (non-empty-string? documentation-text)
(~a "\n---\n" documentation-text)
""))
text))
(hasheq 'contents contents
'range (start/end->range this-doc start end))]
[else (hasheq 'contents empty)]))
(success-response id result)]
[_
(error-response id INVALID-PARAMS "textDocument/hover failed")]))
;; Code Action request
(define (code-action id params)
(match params
[(hash-table ['textDocument (DocIdentifier #:uri uri)]
['range (Range #:start start #:end _end)]
; TODO: _ctx has three fields
; 1. `diagnostics`
; 2. `only: CodeActionKind[]` server should use this to filter out client-unwanted code action
; 3. `triggerKind?: CodeActionTriggerKind` the reason why code action were requested
['context _ctx])
(define this-doc (hash-ref open-docs (string->symbol uri)))
(define doc-trace (Doc-trace this-doc))
(define act (interval-map-ref (send doc-trace get-quickfixs)
(pos->abs-pos this-doc start)
#f))
(if act
(success-response id (list act))
(success-response id (list)))]
[(hash-table ['textDocument (DocIdentifier #:uri uri)])
(error-response id INVALID-PARAMS
(format "textDocument/codeAction failed uri is not a path ~a" uri))]
[_ (error-response id INVALID-PARAMS "textDocument/codeAction failed")]))
;; Signature Help request
(define (signatureHelp id params)
(match params
[(hash-table ['textDocument (DocIdentifier #:uri uri)]
['position (Pos #:line line #:char ch)])
(define this-doc (hash-ref open-docs (string->symbol uri)))
(define doc-trace (Doc-trace this-doc))
(define pos (doc-pos this-doc line ch))
(define new-pos (doc-find-containing-paren this-doc (- pos 1)))
(define result
(cond [new-pos
(define maybe-tag (interval-map-ref (send doc-trace get-docs) (+ new-pos 1) #f))
(define tag
(cond [maybe-tag (last maybe-tag)]
[else
(define symbols (doc-get-symbols this-doc))
(define-values (start end symbol)
(interval-map-ref/bounds symbols (+ new-pos 2) #f))
(cond [symbol
(id-to-tag (first symbol) doc-trace)]
[else #f])]))
(cond [tag
(match-define (list sigs docs) (get-docs-for-tag tag))
(if sigs
(hasheq 'signatures (map (lambda (sig)
(hasheq 'label sig
'documentation (or docs (json-null))))
sigs))
(json-null))]
[else (json-null)])]
[else (json-null)]))
(success-response id result)]
[_
(error-response id INVALID-PARAMS "textDocument/signatureHelp failed")]))
;; Completion Request
(define (completion id params)
(match params
[(hash-table ['textDocument (DocIdentifier #:uri uri)]
['position (Pos #:line line #:char ch)])
(define this-doc (hash-ref open-docs (string->symbol uri)))
(define doc-trace (Doc-trace this-doc))
(define completions (send doc-trace get-completions))
(define result
(for/list ([completion (in-list completions)])
(hasheq 'label (symbol->string completion))))
(success-response id result)]
[_
(error-response id INVALID-PARAMS "textDocument/completion failed")]))
;; Definition request
(define (definition id params)
(match params
[(hash-table ['textDocument (DocIdentifier #:uri uri)]
['position (Pos #:line line #:char char)])
(define-values (start end decl) (get-decl uri line char))
(define this-doc (hash-ref open-docs (string->symbol uri)))
(define result
(match decl
[#f (json-null)]
[(Decl #f id start end)
(Location #:uri uri
#:range (start/end->range this-doc start end))]
[(Decl path id 0 0)
(Location #:uri (path->uri path)
#:range (Range->hash (get-definition-by-id path id)))]))
(success-response id result)]
[_
(error-response id INVALID-PARAMS "textDocument/definition failed")]))
;; Reference request
(define (references id params)
(match params
[(hash-table ['textDocument (DocIdentifier #:uri uri)]
['position (Pos #:line line #:char char)]
['context (hash-table ['includeDeclaration include-decl?])])
(define-values (start end decl) (get-decl uri line char))
(define this-doc (hash-ref open-docs (string->symbol uri)))
(define result
(match decl
[(Decl req? id left right)
(define ranges
(if req?
(list (start/end->range this-doc start end)
(start/end->range this-doc left right))
(or (get-bindings uri decl))))
(for/list ([range (in-list ranges)])
(hasheq 'uri uri 'range range))]
[#f (json-null)]))
(success-response id result)]
[_
(error-response id INVALID-PARAMS "textDocument/references failed")]))
;; Document Highlight request
(define (document-highlight id params)
(match params
[(hash-table ['textDocument (DocIdentifier #:uri uri)]
['position (Pos #:line line #:char char)])
(define this-doc (hash-ref open-docs (string->symbol uri)))
(define-values (start end decl) (get-decl uri line char))
(define result
(match decl
[(Decl filename id left right)
(define ranges
(if filename
(list (start/end->range this-doc start end)
(start/end->range this-doc left right))
(or (append (get-bindings uri decl)
(list (start/end->range this-doc left right))))))
(for/list ([range (in-list ranges)])
(hasheq 'range range))]
[#f (json-null)]))
(success-response id result)]
[_
(error-response id INVALID-PARAMS "textDocument/documentHighlight failed")]))
;; Rename request
(define (_rename id params)
(match params
[(hash-table ['textDocument (DocIdentifier #:uri uri)]
['position (Pos #:line line #:char char)]
['newName new-name])
(define-values (start end decl) (get-decl uri line char))
(define this-doc (hash-ref open-docs (string->symbol uri)))
(define result
(match decl
[(Decl req? id left right)
(cond [req? (json-null)]
[else
(define ranges (cons (start/end->range this-doc left right)
(get-bindings uri decl)))
(WorkspaceEdit
#:changes
(hasheq (string->symbol uri)
(for/list ([range (in-list ranges)])
(TextEdit #:range range #:newText new-name))))])]
[#f (json-null)]))
(success-response id result)]
[_
(error-response id INVALID-PARAMS "textDocument/documentHighlight failed")]))
;; Prepare rename
(define (prepareRename id params)
(match params
[(hash-table ['textDocument (DocIdentifier #:uri uri)]
['position (Pos #:line line #:char char)])
(define-values (start end decl) (get-decl uri line char))
(define this-doc (hash-ref open-docs (string->symbol uri)))
(if (and decl (not (Decl-filename decl)))
(success-response id (start/end->range this-doc start end))
(success-response id (json-null)))]
[_
(error-response id INVALID-PARAMS "textDocument/documentHighlight failed")]))
;; Gets a list of Range objects indicating bindings related to the
;; symbol at the given position. If #:include-decl is #t, the list includes
;; the declaration. If #:include-decl is 'all, the list includes the declaration
;; and all bound occurrences.
(define (get-bindings uri decl)
(define this-doc (hash-ref open-docs (string->symbol uri)))
(define doc-trace (Doc-trace this-doc))
(define doc-decls (send doc-trace get-sym-decls))
(match-define (Decl req? id left right) decl)
(define-values (bind-start bind-end bindings)
(interval-map-ref/bounds doc-decls left #f))
(if bindings
(for/list ([range (in-set bindings)])
(start/end->range this-doc (car range) (cdr range)))
empty))
(define (get-decl uri line char)
(define this-doc (hash-ref open-docs (string->symbol uri)))
(define doc-trace (Doc-trace this-doc))
(define pos (doc-pos this-doc line char))
(define doc-decls (send doc-trace get-sym-decls))
(define doc-bindings (send doc-trace get-sym-bindings))
(define-values (start end maybe-decl)
(interval-map-ref/bounds doc-bindings pos #f))
(define-values (bind-start bind-end maybe-bindings)
(interval-map-ref/bounds doc-decls pos #f))
(if maybe-decl
(values start end maybe-decl)
(if maybe-bindings
(values bind-start
bind-end
(interval-map-ref doc-bindings (car (set-first maybe-bindings)) #f))
(values #f #f #f))))
;; Document Symbol request
(define (document-symbol id params)
(match params
[(hash-table ['textDocument (DocIdentifier #:uri uri)])
(define this-doc (hash-ref open-docs (string->symbol uri)))
(define results
(dict-map (doc-get-symbols this-doc)
(λ (key value)
(match-define (cons start end) key)
(match-define (list text type) value)
(define kind (match type
['constant SymbolKind-Constant]
['string SymbolKind-String]
['symbol SymbolKind-Variable]))
(define range
(Range #:start (abs-pos->pos this-doc start)
#:end (abs-pos->pos this-doc end)))
(SymbolInfo #:name text
#:kind kind
#:location (Location #:uri uri
#:range range)))))
(success-response id results)]
[_
(error-response id INVALID-PARAMS "textDocument/documentSymbol failed")]))
;; Inlay Hint
(define (inlay-hint id params)
(match params
[(hash-table ['textDocument (DocIdentifier #:uri uri)]
['range (Range #:start start #:end end)])
(success-response id '())]
[_ (error-response id INVALID-PARAMS "textDocument/inlayHint failed")]))
;; Full document formatting request
(define (formatting! id params)
(match params
[(hash-table ['textDocument (DocIdentifier #:uri uri)]
['options (as-FormattingOptions opts)])
(define this-doc (hash-ref open-docs (string->symbol uri)))
(define-values (st-ln st-ch) (doc-line/ch this-doc 0))
(define-values (ed-ln ed-ch) (doc-line/ch this-doc (doc-endpos this-doc)))
(success-response id (format! this-doc st-ln st-ch ed-ln ed-ch #:formatting-options opts))]
[_
(error-response id INVALID-PARAMS "textDocument/formatting failed")]))
;; Range Formatting request
(define (range-formatting! id params)
(match params
[(hash-table ['textDocument (DocIdentifier #:uri uri)]
['range (Range #:start (Pos #:line st-ln #:char st-ch)
#:end (Pos #:line ed-ln #:char ed-ch))]
['options (as-FormattingOptions opts)])
(define this-doc (hash-ref open-docs (string->symbol uri)))
(success-response id (format! this-doc st-ln st-ch ed-ln ed-ch #:formatting-options opts))]
[_
(error-response id INVALID-PARAMS "textDocument/rangeFormatting failed")]))
;; On-type formatting request
(define (on-type-formatting! id params)
(match params
[(hash-table ['textDocument (DocIdentifier #:uri uri)]
;; `position` is assumed to be the cursor position that after the edit.
;; Therefore, `position - 1` is the position of `ch`.
;; Also see issue https://github.com/jeapostrophe/racket-langserver/issues/111
['position (Pos #:line line #:char char)]
['ch ch]
['options (as-FormattingOptions opts)])
(define this-doc (hash-ref open-docs (string->symbol uri)))
(define ch-pos (- (doc-pos this-doc line char) 1))
(define-values (st-ln st-ch ed-ln ed-ch)
(match ch
["\n"
(define-values (st-ln st-ch) (doc-line/ch this-doc (doc-line-start-pos this-doc line)))
(define-values (ed-ln ed-ch) (doc-line/ch this-doc (doc-line-end-pos this-doc line)))
(values st-ln st-ch ed-ln ed-ch)]
[_
(define-values (st-ln st-ch)
(doc-line/ch this-doc (or (doc-find-containing-paren this-doc (max 0 (sub1 ch-pos))) 0)))
(define-values (ed-ln ed-ch) (doc-line/ch this-doc ch-pos))
(values st-ln st-ch ed-ln ed-ch)]))
(success-response id (format! this-doc st-ln st-ch ed-ln ed-ch
#:on-type? #t
#:formatting-options opts))]
[_
(error-response id INVALID-PARAMS "textDocument/onTypeFormatting failed")]))
(define (full-semantic-tokens id params)
(match params
[(hash-table ['textDocument (DocIdentifier #:uri uri)])
(define this-doc (hash-ref open-docs (string->symbol uri)))
(semantic-tokens uri id 0 (doc-endpos this-doc))]
[_ (error-response id INVALID-PARAMS "textDocument/semanticTokens/full failed")]))
(define (range-semantic-tokens id params)
(match params
[(hash-table ['textDocument (DocIdentifier #:uri uri)]
['range (Range #:start (Pos #:line st-ln #:char st-ch)
#:end (Pos #:line ed-ln #:char ed-ch))])
(define this-doc (hash-ref open-docs (string->symbol uri)))
(define start-pos (doc-pos this-doc st-ln st-ch))
(define end-pos (doc-pos this-doc ed-ln ed-ch))
(semantic-tokens uri id start-pos end-pos)]
[_ (error-response id INVALID-PARAMS "textDocument/semanticTokens/range failed")]))
(define (semantic-tokens uri id start-pos end-pos)
(define this-doc (hash-ref open-docs (string->symbol uri)))
(define new-editor (send (Doc-text this-doc) copy))
(λ ()
(define tokens (doc-range-tokens new-editor uri start-pos end-pos))
(success-response id (hash 'data tokens))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(provide
(contract-out
[did-open! (jsexpr? . -> . void?)]
[did-close! (jsexpr? . -> . void?)]
[did-change! (jsexpr? . -> . void?)]
[hover (exact-nonnegative-integer? jsexpr? . -> . jsexpr?)]
[code-action (exact-nonnegative-integer? jsexpr? . -> . jsexpr?)]
[completion (exact-nonnegative-integer? jsexpr? . -> . jsexpr?)]
[signatureHelp (exact-nonnegative-integer? jsexpr? . -> . jsexpr?)]
[definition (exact-nonnegative-integer? jsexpr? . -> . jsexpr?)]
[document-highlight (exact-nonnegative-integer? jsexpr? . -> . jsexpr?)]
[references (exact-nonnegative-integer? jsexpr? . -> . jsexpr?)]
[document-symbol (exact-nonnegative-integer? jsexpr? . -> . jsexpr?)]
[inlay-hint (exact-nonnegative-integer? jsexpr? . -> . jsexpr?)]
[rename _rename rename (exact-nonnegative-integer? jsexpr? . -> . jsexpr?)]
[prepareRename (exact-nonnegative-integer? jsexpr? . -> . jsexpr?)]
[formatting! (exact-nonnegative-integer? jsexpr? . -> . jsexpr?)]
[range-formatting! (exact-nonnegative-integer? jsexpr? . -> . jsexpr?)]
[on-type-formatting! (exact-nonnegative-integer? jsexpr? . -> . jsexpr?)]
[full-semantic-tokens (exact-nonnegative-integer? jsexpr? . -> . (or/c jsexpr? (-> jsexpr?)))]
[range-semantic-tokens (exact-nonnegative-integer? jsexpr? . -> . (or/c jsexpr? (-> jsexpr?)))]))