Skip to content

Commit 5f432f1

Browse files
6cdhjeapostrophe
authored andcommitted
feat(method): make semantic token method runs concurrently
1 parent daa12d0 commit 5f432f1

File tree

3 files changed

+29
-13
lines changed

3 files changed

+29
-13
lines changed

doc.rkt

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -349,9 +349,9 @@
349349
;;
350350
;; for the first token, its previous token is defined as a zero length fake token which
351351
;; has line number 0 and character position 0.
352-
(define (token-encoding doc token prev-pos)
353-
(define-values (line ch) (doc-line/ch doc (SemanticToken-start token)))
354-
(define-values (prev-line prev-ch) (doc-line/ch doc prev-pos))
352+
(define (token-encoding editor token prev-pos)
353+
(match-define (list line ch) (send editor pos->line/char (SemanticToken-start token)))
354+
(match-define (list prev-line prev-ch) (send editor pos->line/char prev-pos))
355355
(define delta-line (- line prev-line))
356356
(define delta-start
357357
(if (= line prev-line)
@@ -366,8 +366,8 @@
366366
;; the tokens whose range intersects the given range is included.
367367
;; the previous token of the first token in the result is defined as a zero length fake token which
368368
;; has line number 0 and character position 0.
369-
(define (doc-range-tokens doc path pos-start pos-end)
370-
(define tokens (collect-semantic-tokens (Doc-text doc) (uri->path path)))
369+
(define (doc-range-tokens editor uri pos-start pos-end)
370+
(define tokens (collect-semantic-tokens editor (uri->path uri)))
371371
(define tokens-in-range
372372
(filter-not (λ (tok) (or (<= (SemanticToken-end tok) pos-start)
373373
(>= (SemanticToken-start tok) pos-end)))
@@ -377,11 +377,12 @@
377377
#:result (flatten (reverse result)))
378378
([token tokens-in-range])
379379
(define-values (delta-line delta-start len type modifier)
380-
(token-encoding doc token prev-pos))
380+
(token-encoding editor token prev-pos))
381381
(values (cons (list delta-line delta-start len type modifier) result)
382382
(SemanticToken-start token))))
383383

384-
(provide Doc-trace
384+
(provide Doc-text
385+
Doc-trace
385386
new-doc
386387
doc-checked?
387388
doc-update!

methods.rkt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,15 @@
3131
['method (? string? method)])
3232
(define params (hash-ref msg 'params hasheq))
3333
(define response (process-request id method params))
34-
(display-message/flush response)]
34+
;; the result can be a response or a procedure which returns
35+
;; a response. If it's a procedure, then it's expected to run
36+
;; concurrently.
37+
(thread (λ ()
38+
(display-message/flush
39+
(if (procedure? response)
40+
(response)
41+
response))))
42+
(void)]
3543
;; Notification
3644
[(hash-table ['method (? string? method)])
3745
(define params (hash-ref msg 'params hasheq))

text-document.rkt

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -482,20 +482,27 @@
482482
(match params
483483
[(hash-table ['textDocument (DocIdentifier #:uri uri)])
484484
(define this-doc (hash-ref open-docs (string->symbol uri)))
485-
(success-response id (hash 'data (doc-range-tokens this-doc uri 0 (doc-endpos this-doc))))]
485+
(semantic-tokens uri id 0 (doc-endpos this-doc))]
486486
[_ (error-response id INVALID-PARAMS "textDocument/semanticTokens/full failed")]))
487487

488488
(define (range-semantic-tokens id params)
489489
(match params
490490
[(hash-table ['textDocument (DocIdentifier #:uri uri)]
491491
['range (Range #:start (Pos #:line st-ln #:char st-ch)
492-
#:end (Pos #:line ed-ln #:char ed-ch))])
492+
#:end (Pos #:line ed-ln #:char ed-ch))])
493493
(define this-doc (hash-ref open-docs (string->symbol uri)))
494494
(define start-pos (doc-pos this-doc st-ln st-ch))
495495
(define end-pos (doc-pos this-doc ed-ln ed-ch))
496-
(success-response id (hash 'data (doc-range-tokens this-doc uri start-pos end-pos)))]
496+
(semantic-tokens uri id start-pos end-pos)]
497497
[_ (error-response id INVALID-PARAMS "textDocument/semanticTokens/range failed")]))
498498

499+
(define (semantic-tokens uri id start-pos end-pos)
500+
(define this-doc (hash-ref open-docs (string->symbol uri)))
501+
(define new-editor (send (Doc-text this-doc) copy))
502+
(λ ()
503+
(define tokens (doc-range-tokens new-editor uri start-pos end-pos))
504+
(success-response id (hash 'data tokens))))
505+
499506
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
500507

501508
(provide
@@ -517,6 +524,6 @@
517524
[formatting! (exact-nonnegative-integer? jsexpr? . -> . jsexpr?)]
518525
[range-formatting! (exact-nonnegative-integer? jsexpr? . -> . jsexpr?)]
519526
[on-type-formatting! (exact-nonnegative-integer? jsexpr? . -> . jsexpr?)]
520-
[full-semantic-tokens (exact-nonnegative-integer? jsexpr? . -> . jsexpr?)]
521-
[range-semantic-tokens (exact-nonnegative-integer? jsexpr? . -> . jsexpr?)]))
527+
[full-semantic-tokens (exact-nonnegative-integer? jsexpr? . -> . (or/c jsexpr? (-> jsexpr?)))]
528+
[range-semantic-tokens (exact-nonnegative-integer? jsexpr? . -> . (or/c jsexpr? (-> jsexpr?)))]))
522529

0 commit comments

Comments
 (0)