forked from jeapostrophe/racket-langserver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtext-document.rkt
More file actions
539 lines (503 loc) · 20.9 KB
/
text-document.rkt
File metadata and controls
539 lines (503 loc) · 20.9 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
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
529
530
531
532
533
534
535
536
537
538
539
#lang racket/base
(require (for-syntax racket/base)
data/interval-map
framework
json
racket/class
racket/contract/base
racket/list
racket/match
racket/string
racket/set
racket/exn
racket/dict
racket/format
syntax-color/module-lexer
syntax-color/racket-lexer
"check-syntax.rkt"
"error-codes.rkt"
"interfaces.rkt"
"json-util.rkt"
"responses.rkt"
"msg-io.rkt"
"symbol-kinds.rkt"
"docs-helpers.rkt"
"doc-trace.rkt"
"queue-only-latest.rkt")
(define (uri-is-path? str)
(string-prefix? str "file://"))
(define (uri->path uri)
(cond
[(eq? (system-type 'os) 'windows)
;; If a file URI begins with file:// or file:////, Windows translates it
;; as a UNC path. If it begins with file:///, it's translated to an MS-DOS
;; path. (https://en.wikipedia.org/wiki/File_URI_scheme#Windows_2)
(cond
[(string-prefix? uri "file:////") (substring uri 7)]
[(string-prefix? uri "file:///") (substring uri 8)]
[else (string-append "//" (substring uri 7))])]
[else (substring uri 7)]))
;;
;; 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 TextEdit
[range any/c]
[newText string?])
;;
;; Methods
;;;;;;;;;;;;
(define open-docs (make-hasheq))
(define (did-open! params)
(match-define (hash-table ['textDocument (DocItem #:uri uri #:text text)]) params)
(unless (uri-is-path? uri)
;; TODO: send user diagnostic or something
(error 'did-open "uri is not a path."))
(define path (uri->path uri))
(define doc-text (new racket:text%))
(send doc-text insert text 0)
(define trace (check-syntax path doc-text #f))
(hash-set! open-docs (string->symbol uri) (doc doc-text trace)))
(define (did-close! params)
(match-define (hash-table ['textDocument (DocItem #:uri uri)]) params)
(when (uri-is-path? uri)
(hash-remove! open-docs (string->symbol uri))))
(define (did-change! params)
(match-define (hash-table ['textDocument (DocIdentifier #:uri uri)]
['contentChanges content-changes]) params)
(when (uri-is-path? uri)
(define this-doc (hash-ref open-docs (string->symbol uri)))
(match-define (doc doc-text doc-trace) this-doc)
(define content-changes*
(cond [(eq? (json-null) content-changes) empty]
[(list? content-changes) content-changes]
[else (list content-changes)]))
(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)
(define st-pos (line/char->pos doc-text st-ln st-ch))
(define end-pos (line/char->pos doc-text ed-ln ed-ch))
(define old-len (- end-pos st-pos))
(define new-len (string-length text))
(cond [(> new-len old-len) (send doc-trace expand end-pos (+ st-pos new-len))]
[(< new-len old-len) (send doc-trace contract (+ st-pos new-len) end-pos)]
[else #f])
(send doc-text insert text st-pos end-pos)]
[(ContentChangeEvent #:text text)
(send doc-trace reset)
(send doc-text erase)
(send doc-text insert text 0)]))
(try-queue-check (uri->path uri) this-doc))
(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)])
(unless (uri-is-path? uri)
(error 'hover "uri is not a path"))
(match-define (doc doc-text doc-trace)
(hash-ref open-docs (string->symbol uri)))
(define hovers (send doc-trace get-hovers))
(define pos (line/char->pos doc-text 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
(hasheq 'contents (if link (~a text " - [docs](" (~a "https://docs.racket-lang.org/" (last (string-split link "/doc/"))) ")") text)
'range (start/end->Range doc-text start end))]
[else (hasheq 'contents empty)]))
(success-response id result)]
[_
(error-response id INVALID-PARAMS "textDocument/hover failed")]))
;; Signature Help request
(define (signatureHelp id params)
(match params
[(hash-table ['textDocument (DocIdentifier #:uri uri)]
['position (Pos #:line line #:char ch)])
(unless (uri-is-path? uri)
(error 'signatureHelp "uri is not a path"))
(match-define (doc doc-text doc-trace)
(hash-ref open-docs (string->symbol uri)))
(define pos (line/char->pos doc-text line ch))
(define text (send doc-text get-text))
(define new-pos (find-containing-paren (- pos 1) text))
(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 (get-symbols doc-text))
(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")]))
(define (get-symbols doc-text)
(define text (send doc-text get-text))
(define in (open-input-string text))
(port-count-lines! in)
(define lexer (get-lexer in))
(define symbols (make-interval-map))
(for ([lst (in-port (lexer-wrap lexer) in)] #:when (set-member? '(constant string symbol) (first (rest lst))))
(match-define (list text type paren? start end) lst)
(interval-map-set! symbols start end (list text type)))
symbols)
;; Wrapper for in-port, returns a list or EOF.
(define ((lexer-wrap lexer) in)
(define (eof-or-list txt type paren? start end)
(if (eof-object? txt)
eof
(list txt type paren? start end)))
(cond
[(procedure? lexer)
(define-values (txt type paren? start end)
(lexer in))
(eof-or-list txt type paren? start end)]
[(cons? lexer)
(define-values (txt type paren? start end backup mode)
((car lexer) in 0 (cdr lexer)))
(set! lexer (cons (car lexer) mode))
(eof-or-list txt type paren? start end)]))
;; Call module-lexer on an input port, then discard all
;; values except the lexer.
(define (get-lexer in)
(match-define-values
(_ _ _ _ _ _ lexer)
(module-lexer in 0 #f))
(cond
[(procedure? lexer) lexer]
[(cons? lexer) lexer]
[(eq? lexer 'no-lang-line) racket-lexer]
[(eq? lexer 'before-lang-line) racket-lexer]))
;; Completion Request
(define (completion id params)
(match params
[(hash-table ['textDocument (DocIdentifier #:uri uri)]
['position (Pos #:line line #:char ch)])
(unless (uri-is-path? uri)
(error 'completion "uri is not a path"))
(match-define (doc doc-text doc-trace)
(hash-ref open-docs (string->symbol uri)))
(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))
(match-define (doc doc-text doc-trace)
(hash-ref open-docs (string->symbol uri)))
(define result
(match decl
[#f (json-null)]
[(Decl _ start end)
(Location #:uri uri
#:range (start/end->Range doc-text start end))]))
(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))
(match-define (doc doc-text doc-trace)
(hash-ref open-docs (string->symbol uri)))
(define result
(match decl
[(Decl req? left right)
(define ranges
(if req?
(list (start/end->Range doc-text start end) (start/end->Range doc-text 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-values (start end decl) (get-decl uri line char))
(match-define (doc doc-text doc-trace)
(hash-ref open-docs (string->symbol uri)))
(define result
(match decl
[(Decl req? left right)
(define ranges
(if req?
(list (start/end->Range doc-text start end) (start/end->Range doc-text left right))
(or (append (get-bindings uri decl) (list (start/end->Range doc-text 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))
(match-define (doc doc-text doc-trace)
(hash-ref open-docs (string->symbol uri)))
(define result
(match decl
[(Decl req? left right)
(cond [req? (json-null)]
[else
(define ranges (cons (start/end->Range doc-text left right) (get-bindings uri decl)))
(hasheq '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))
(match-define (doc doc-text doc-trace)
(hash-ref open-docs (string->symbol uri)))
(if (and decl (not (Decl-require? decl)))
(success-response id (start/end->Range doc-text 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)
(unless (uri-is-path? uri)
(error 'get-bindings "uri is not a path"))
(match-define (doc doc-text doc-trace)
(hash-ref open-docs (string->symbol uri)))
(define doc-decls (send doc-trace get-sym-decls))
(match-define (Decl req? 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 doc-text (car range) (cdr range)))
empty))
(define (get-decl uri line char)
(unless (uri-is-path? uri)
(error 'get-decl "uri is not a path"))
(match-define (doc doc-text doc-trace)
(hash-ref open-docs (string->symbol uri)))
(define pos (line/char->pos doc-text 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)])
(unless (uri-is-path? uri)
(error 'document-symbol "uri is not a path"))
(match-define (doc doc-text doc-trace)
(hash-ref open-docs (string->symbol uri)))
(define results
(dict-map (get-symbols doc-text)
(λ (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 doc-text start)
#:end (abs-pos->Pos doc-text end)))
(SymbolInfo #:name text
#:kind kind
#:location (Location #:uri uri
#:range range)))))
(success-response id results)]
[_
(error-response id INVALID-PARAMS "textDocument/documentSymbol failed")]))
;; Full document formatting request
(define (formatting! id params)
(match params
;; We're ignoring 'options for now
[(hash-table ['textDocument (DocIdentifier #:uri uri)])
(match-define (doc doc-text doc-trace)
(hash-ref open-docs (string->symbol uri)))
(define end-pos (send doc-text last-position))
(range-formatting!
id
(hash-set params
'range (Range #:start (abs-pos->Pos doc-text 0)
#:end (abs-pos->Pos doc-text end-pos))))]
[_
(error-response id INVALID-PARAMS "textDocument/formatting failed")]))
;; Range Formatting request
(define (range-formatting! id params)
(match params
;; XXX We're ignoring 'options' for now
[(hash-table ['textDocument (DocIdentifier #:uri uri)]
['range (Range #:start start #:end end)])
(unless (uri-is-path? uri)
(error 'range-formatting! "uri is not a path"))
(match-define (doc doc-text doc-trace)
(hash-ref open-docs (string->symbol uri)))
(define indenter (send doc-trace get-indenter))
(define start-pos (Pos->abs-pos doc-text start))
(define end-pos (Pos->abs-pos doc-text end))
(define start-line (send doc-text position-paragraph start-pos))
(define end-line (send doc-text position-paragraph end-pos))
(define mut-doc-text
(if (is-a? doc-text racket:text%)
(let ([r-text (new racket:text%)]) (send r-text insert (send doc-text get-text)) r-text)
(send doc-text copy-self)))
(define results
(if (eq? indenter 'missing) (json-null)
(let loop ([line start-line])
(if (> line end-line)
null
(let ([edit (indent-line! mut-doc-text indenter line)])
(if edit
(cons edit (loop (add1 line)))
(loop (add1 line))))))))
(success-response id results)]
[_
(error-response id INVALID-PARAMS "textDocument/rangeFormatting failed")]))
;; On-type formatting request
(define (on-type-formatting! id params)
(match params
;; We're ignoring 'options for now
[(hash-table ['textDocument (DocIdentifier #:uri uri)]
['position (Pos #:line line #:char char)]
['ch ch])
(match-define (doc doc-text doc-trace)
(hash-ref open-docs (string->symbol uri)))
(define pos (- (line/char->pos doc-text line char) 1))
(define range
(match ch
["\n"
(Range
#:start (abs-pos->Pos doc-text (send doc-text paragraph-start-position line))
#:end (abs-pos->Pos doc-text (send doc-text paragraph-end-position line)))]
[_
(Range
#:start (abs-pos->Pos doc-text (or (find-containing-paren pos (send doc-text get-text)) 0))
#:end (abs-pos->Pos doc-text pos))]))
(range-formatting!
id
(hash-set params
'range range))]
[_
(error-response id INVALID-PARAMS "textDocument/onTypeformatting failed")]))
;; Returns a TextEdit, or #f if the line is already correct.
(define (indent-line! doc-text indenter line)
(define line-start (send doc-text paragraph-start-position line))
(define line-end (send doc-text paragraph-end-position line))
(define line-text (send doc-text get-text line-start line-end))
(define line-length (string-length line-text))
(define current-spaces
(let loop ([i 0])
(cond [(= i line-length) i]
[(char=? (string-ref line-text i) #\space) (loop (add1 i))]
[else i])))
(define desired-spaces
(if indenter
(indenter doc-text line-start)
(send doc-text compute-racket-amount-to-indent line-start)))
(cond
[(not (number? desired-spaces)) #f]
[(= current-spaces desired-spaces) #f]
[(< current-spaces desired-spaces)
;; Insert spaces
(define insert-count (- desired-spaces current-spaces))
(define new-text (make-string insert-count #\space))
(define pos (Pos #:line line #:char 0))
(send doc-text insert new-text line-start 'same)
(TextEdit #:range (Range #:start pos #:end pos)
#:newText new-text)]
[else
;; Delete spaces
(define span (- current-spaces desired-spaces))
(send doc-text delete line-start (+ line-start span))
(TextEdit #:range (Range #:start (Pos #:line line #:char 0)
#:end (Pos #:line line #:char span))
#:newText "")]))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(provide
(contract-out
[did-open! (jsexpr? . -> . void?)]
[did-close! (jsexpr? . -> . void?)]
[did-change! (jsexpr? . -> . void?)]
[hover (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?)]
[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?)]))