-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathiracket-complete.rkt
36 lines (32 loc) · 1.22 KB
/
iracket-complete.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
#lang racket/base
(require racket/string
racket/match
racket/contract
racket/sandbox
json
net/base64
(prefix-in ipy: "./ipython-message.rkt")
(prefix-in ipy: "./ipython-services.rkt")
(prefix-in ipy: "./ipython.rkt"))
(provide complete)
;; complete_request
(define/contract (string-prefix? prefix word)
(string? string? . -> . boolean?)
(define len (string-length prefix))
(equal? prefix (substring word 0 (min (string-length word) len))))
(define/contract (complete e msg)
(any/c ipy:message? . -> . jsexpr?)
(define code (hash-ref (ipy:message-content msg) 'code))
(define cursor-pos (hash-ref (ipy:message-content msg) 'cursor_pos))
(define prefix (car (regexp-match #px"[^\\s,)(]*$" code 0 cursor-pos)))
(define suffix (car (regexp-match #px"^[^\\s,)(]*" code (sub1 cursor-pos))))
(define words (call-in-sandbox-context e namespace-mapped-symbols))
(define matches
(sort (filter (λ (w) (string-prefix? prefix w))
(map symbol->string words))
string<=?))
(hasheq
'matches matches
'cursor_start (- cursor-pos (string-length prefix))
'cursor_end (+ cursor-pos (string-length suffix) -1)
'status "ok"))