forked from jeapostrophe/racket-langserver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
text-document.rkt
659 lines (622 loc) · 26.5 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
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
#lang racket/base
(require data/interval-map
framework
json
racket/gui
syntax-color/module-lexer
syntax-color/racket-lexer
drracket/check-syntax
syntax/modread
"check-syntax.rkt"
"error-codes.rkt"
"interfaces.rkt"
"json-util.rkt"
"path-util.rkt"
"responses.rkt"
"symbol-kinds.rkt"
"docs-helpers.rkt"
"doc-trace.rkt"
"queue-only-latest.rkt"
"documentation-parser.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?])
;;
;; 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
;; 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 doc-text 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])
#:when (uri-is-path? uri)
(match-define (doc doc-text doc-trace)
(hash-ref open-docs (string->symbol uri)))
(define act (interval-map-ref (send doc-trace get-quickfixs)
(Pos->abs-pos doc-text 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)])
(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 (get-def path doc-text id)
(define collector
(new (class (annotations-mixin object%)
(define defs (make-hash))
(define/public (get id) (hash-ref defs id #f))
(define/override (syncheck:add-definition-target source-obj start end id mods)
(hash-set! defs id (cons start end)))
(super-new))))
(define-values (src-dir _file _dir?)
(split-path path))
(define in (open-input-string (send doc-text get-text)))
(define ns (make-base-namespace))
(define-values (add-syntax done)
(make-traversal ns src-dir))
(parameterize ([current-annotations collector]
[current-namespace ns]
[current-load-relative-directory src-dir])
(define stx (expand (with-module-reading-parameterization
(λ () (read-syntax path in)))))
(add-syntax stx))
(send collector get id))
(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 #f id start end)
(Location #:uri uri
#:range (start/end->Range doc-text start end))]
[(Decl path id 0 0)
(define doc-text (new text%))
(send doc-text load-file path)
(match-define (cons start end) (get-def path doc-text id))
(Location #:uri (path->uri path)
#: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? id 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? id 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? id left right)
(cond [req? (json-null)]
[else
(define ranges (cons (start/end->Range doc-text 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))
(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? 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 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")]))
;; Inlay Hint
(define (inlay-hint id params)
(match params
[(hash-table ['textDocument (DocIdentifier #:uri uri)])
#:when (not (uri-is-path? uri))
(error-response id INVALID-PARAMS "document-symbol: uri is not a path")]
[(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
;; 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 (max 0 (sub1 (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 skip-this-line? #f)
(define results
(if (eq? indenter 'missing) (json-null)
(let loop ([line start-line])
(define line-start (send mut-doc-text paragraph-start-position line))
(define line-end (send mut-doc-text paragraph-end-position line))
(for ([i (range line-start (add1 line-end))])
(when (and (char=? #\" (send mut-doc-text get-character i))
(not (char=? #\\ (send mut-doc-text get-character (sub1 i)))))
(set! skip-this-line? (not skip-this-line?))))
(if (> line end-line)
null
(append (filter-map
identity
; NOTE: The order is important somehow
(list (remove-trailing-space! mut-doc-text skip-this-line? line)
(indent-line! mut-doc-text indenter 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 a part of multiple-line string
(define (remove-trailing-space! doc-text in-string? 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))
(cond
[(not in-string?)
(define from (string-length (string-trim line-text #px"\\s+" #:left? #f)))
(define to (string-length line-text))
(send doc-text delete (+ line-start from) (+ line-start to))
(TextEdit #:range (Range #:start (Pos #:line line #:char from)
#:end (Pos #:line line #:char to))
#:newText "")]
[else #f]))
;; 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
(or (indenter doc-text line-start)
(send doc-text compute-racket-amount-to-indent line-start))
(send doc-text compute-racket-amount-to-indent line-start)))
(cond
[(not (number? desired-spaces)) #f]
[(= current-spaces desired-spaces) #f]
[(= line-length 0) #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?)]
[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?)]))