Supporting multi-in
In racket-mode I improved support for the multi-in
form provided by racket/require
.
What is multi-in
?
Instead of:
1 2 3 4 5 |
(require net/uri-codec net/url racket/contract racket/format racket/string) |
You can say:
1 2 3 |
One detail: The racket/require
must appear before any multi-in
forms. Any sorting must make an exception for this.
What are the racket-{tidy trim base}-requires
commands?
-
racket-tidy-requires
gathers multiplerequire
forms into one. Within that, it groups phase level sub-forms such asfor-syntax
. Finally it sorts absolute module paths likefoo
before relative paths like"foo.rkt"
, and sorts each of those alphabetically. -
racket-trim-requires
uses theshow-requires
function frommacro-debugger/analysis/check-requires
to analyze requires and delete any unused. Also it tidies. -
racket-base-requires
uses the analysis to change a#lang racket
file to#lang racket/base
, adding requires as necessary. Also it trims and tidies.
What are the changes?
-
Sorting makes sure to keep
racket/require
first. -
If
racket/require
is present in the originalrequire
form(s), thenmulti-in
is used when tidying. -
Any analysis that says requires should be removed, should handle
multi-in
forms.
Essentially, the commands first “expand” or “explode” multi-in
forms to individual requires, do any analysis and modifications, then try to “unexpand” or “implode” them back again.1
All together, these changes close issues 355, 356, and 369.
Example without racket/require
Here’s an example where racket/require
is not in the original require
forms:
1 2 3 4 5 6 7 |
#lang racket (require net/url) (require net/uri-codec) ;; Just some expressions using imported definitions string-join ~a get-pure-port uri-decode (match-define (list n) (list 1)) |
After
M-x racket-base-requires
:
1 2 3 4 5 6 7 8 9 10 |
#lang racket/base (require net/uri-codec net/url racket/format racket/match racket/string) ;; Just some expressions using imported definitions string-join ~a get-pure-port uri-decode (match-define (list n) (list 1)) |
Example with racket/require
Here’s an example where racket/require
is in the original require
forms:
1 2 3 4 5 6 7 8 |
#lang racket (require racket/require) ;new (require net/url) (require net/uri-codec) ;; Just some expressions using imported definitions string-join ~a get-pure-port uri-decode (match-define (list n) (list 1)) |
After
M-x racket-base-requires
:
1 2 3 4 5 6 7 8 |
#lang racket/base (require racket/require (multi-in net (uri-codec url)) (multi-in racket (format match string))) ;; Just some expressions using imported definitions string-join ~a get-pure-port uri-decode (match-define (list n) (list 1)) |
-
Not everything survives a round-trip, exactly. A Cartesian product like
(multi-in (a b) (c d))
will end up as a(multi-in a (c d))
and a(multi-in b (c d))
— equivalent but not as concise. ↩