Skip to content

Instantly share code, notes, and snippets.

@Metaxal
Last active June 23, 2025 13:23
Show Gist options
  • Save Metaxal/b45c16c22ff9986977f47408a01d0c47 to your computer and use it in GitHub Desktop.
Save Metaxal/b45c16c22ff9986977f47408a01d0c47 to your computer and use it in GitHub Desktop.
exports quickscript
#lang racket/base
;;; Copyright 2025 [email protected]
;;; License: [Apache License, Version 2.0](http://www.apache.org/licenses/LICENSE-2.0) or
;;; [MIT license](http://opensource.org/licenses/MIT) at your option.
(require quickscript
racket/dict
racket/gui/base
racket/class
racket/port
racket/path
search-list-box)
(script-help-string "Lists the exports of the module name under the cursor")
;; Currently only works for libs, not yet for modules as strings
(define (string->mod-path str)
(define obj (with-input-from-string str read))
(cond [(string? obj)
(list 'file obj)]
[(symbol? obj)
obj
#;(list 'lib obj)]
[else (error "Unrecognized module form: ~v" str)]))
;; Returns a replacement string for the selected string `selection`
;; ("" if no text is selected), or `#f` to leave the selection as is.
(define-script exports
#:label "E&xports"
(λ (selection #:editor ed #:file f)
(define mod-str
(cond
[(string=? "" selection)
(define pos (send ed get-start-position))
(send ed get-text
(send ed get-backward-sexp pos)
(send ed get-forward-sexp pos)
#;flattened?: #true)]
[else selection])) ; in case something is selected
(define lib (string->mod-path mod-str))
(parameterize ([current-directory
(if f
(path-only f) ; directory of the enclosing module
(current-directory))])
(dynamic-require lib (void)) ; visited but not instantiated
(define-values (a b) (module->exports lib))
(define exports
(sort
(map car
(append (dict-ref a 0 '()) ; TODO: more phases, perhaps prefix with "~{phase}:"
(dict-ref b 0 '())))
symbol<?))
(define out-str #f)
(define dia (new dialog% [label (format "Exports of ~a" mod-str)]
[width 400] [height 400]))
(new message% [parent dia] [label "Press down, then space to select"])
(define slb
(new search-list-box%
[parent dia]
[contents exports]
[callback
(λ (idx str content)
#;(writeln (list idx str content))
(set! out-str str)
(send dia show #f))]))
(send dia show #t)
out-str))) ; Probably not a great idea :/
(module+ drracket
(exports "racket/base" #:editor #f #:file #f))
(module url2script-info racket/base
(provide url)
(define filename "exports.rkt")
(define url "https://gist.github.com/Metaxal/b45c16c22ff9986977f47408a01d0c47"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment