Skip to content

Commit a1141f0

Browse files
shocomanjeapostrophe
authored andcommitted
show documentation in hover response
1 parent 490c72a commit a1141f0

5 files changed

Lines changed: 366 additions & 17 deletions

File tree

doc-trace.rkt

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
data/interval-map
88
net/url
99
"interfaces.rkt"
10-
"responses.rkt")
10+
"responses.rkt"
11+
"docs-helpers.rkt")
1112

1213
(struct Decl (require? id left right) #:transparent)
1314

@@ -86,18 +87,12 @@
8687
(when url
8788
(when (= start finish)
8889
(set! finish (add1 finish)))
89-
(define url (path->url path))
90-
(define url2 (if url-tag
91-
(make-url (url-scheme url)
92-
(url-user url)
93-
(url-host url)
94-
(url-port url)
95-
(url-path-absolute? url)
96-
(url-path url)
97-
(url-query url)
98-
url-tag)
99-
url))
100-
(interval-map-set! docs start finish (list (url->string url2) def-tag))))
90+
(define path-url (path->url path))
91+
(define link+tag (cond
92+
[url-tag (struct-copy url path-url [fragment url-tag])]
93+
[def-tag (struct-copy url path-url [fragment (def-tag->html-anchor-tag def-tag)])]
94+
[else path-url]))
95+
(interval-map-set! docs start finish (list (url->string link+tag) def-tag))))
10196
(define/override (syncheck:add-jump-to-definition source-obj start end id filename submods)
10297
(define decl (Decl filename id 0 0))
10398
(interval-map-set! sym-bindings start (add1 end) decl))

docs-helpers.rkt

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
racket/dict
88
setup/collects
99
racket/string
10-
scribble/xref)
10+
scribble/xref
11+
net/url-string
12+
racket/format)
1113

1214
(define the-bluebox-cache (make-blueboxes-cache #t))
1315
(define pkg-cache (make-hash))
@@ -54,7 +56,54 @@
5456
[else (list strs #f)])]
5557
[else (list #f #f)]))
5658

59+
;; Examples:
60+
;; Input: "file:///C:/Program Files/Racket/doc/reference/module.html#(form._((quote._~23~25kernel)._module))" #f
61+
;; Output: https://docs.racket-lang.org/reference/module.html#%28form._%28%28quote._%7E23%7E25kernel%29._module%29%29
62+
;; (i.e. https://docs.racket-lang.org/ + left trimmed `url`)
63+
;; Input: "pairs.html#(def._((lib._racket/list..rkt)._add-between))" "C:/Program Files/Racket/doc/reference/strings.html"
64+
;; Output: https://docs.racket-lang.org/reference/pairs.html#%28def._%28%28lib._racket%2Flist..rkt%29._add-between%29%29
65+
;; (i.e. https://docs.racket-lang.org/ + /reference/ from `docs-path` + `url`)
66+
(define (make-proper-url-for-online-documentation url [docs-path #f])
67+
(define online-docs-url "https://docs.racket-lang.org/")
68+
(define (absolute-web-url? url) (and (string-contains? url "://") (not (string-prefix? url "file"))))
69+
(define (get-relative-docs-url url) ;; e.g. "reference/module.html#(form._((quote._~23~25kernel)._module))"
70+
(last (string-split url #rx"/doc/(racket/)?"))) ; "(racket/)?" in case docs are installed in 'usr/share/doc/racket' on linux
71+
(define (strip-off-last-path-segment url) (string-join (drop-right (string-split url "/") 1) "/" #:after-last "/"))
72+
(define (encode-url url-string)
73+
(define url-struct (string->url url-string))
74+
;; particularly encode chars '(', ')' and '~' from Markdown. Both VSCode's and Atom's Md parsers don't like them in links.
75+
(current-url-encode-mode 'unreserved)
76+
(define encoded-url (string-replace (url->string url-struct) "~" "%7E"))
77+
;; Rarely, there are `redirecting` links that require putting `&` back in query to work properly
78+
(string-replace encoded-url "&" "&"))
79+
80+
(define encoded-url (encode-url url))
81+
(cond
82+
[(absolute-web-url? encoded-url) encoded-url]
83+
[docs-path
84+
(define ending (get-relative-docs-url docs-path))
85+
(~a online-docs-url
86+
(if (or (string-prefix? encoded-url "#") (zero? (string-length encoded-url)))
87+
ending
88+
(strip-off-last-path-segment ending))
89+
encoded-url)]
90+
[else (~a online-docs-url (get-relative-docs-url encoded-url))]))
91+
92+
;; Example: '(def ((quote #%kernel) hasheq)) => "(def._((quote._~23~25kernel)._hasheq))"
93+
;; mostly a copy of a closed function `anchor-name` in `scribble-lib/scribble/html-render.rkt`
94+
(define (def-tag->html-anchor-tag v)
95+
(define (encode-byte b) (string-append (if (< b 16) "~0" "~") (number->string b 16)))
96+
(define (encode-bytes str) (string->bytes/utf-8 (encode-byte (bytes-ref str 0))))
97+
(let* ([v (string->bytes/utf-8 (format "~a" v))]
98+
[v (regexp-replace* #rx#"[A-Z.]" v #".&")]
99+
[v (regexp-replace* #rx#" " v #"._")]
100+
[v (regexp-replace* #rx#"\"" v #".'")]
101+
[v (regexp-replace* #rx#"[^-a-zA-Z0-9_!+*'()/.,]" v encode-bytes)])
102+
(bytes->string/utf-8 v)))
103+
57104

58105
(provide find-containing-paren
59106
get-docs-for-tag
60-
id-to-tag)
107+
id-to-tag
108+
make-proper-url-for-online-documentation
109+
def-tag->html-anchor-tag)

0 commit comments

Comments
 (0)