Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify define-pretty macro #63

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 13 additions & 20 deletions conventions.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,10 @@
[(? visible?) (loop xs (sub1 pos) (cons (format-kw-arg x) acc))]
[_ (loop xs pos (cons (pretty x) acc))])])])))

(define-pretty (format-vertical/helper #:body-formatter [format-body #f]
#:kw-arg-formatter [format-kw-arg #f]
(define-pretty (format-vertical/helper #:body-formatter [format-body pretty]
#:kw-arg-formatter [format-kw-arg pretty]
#:kw-map [kw-map default-kw-map])
#:type list?
#:default [format-body pretty]
#:default [format-kw-arg pretty]

(let loop ([xs doc])
(define (v-append-if x xs)
Expand Down Expand Up @@ -106,12 +104,10 @@
[(list x xs ...) (v-append-if (format-body x) xs)])))

;; failable
(define-pretty (format-horizontal/helper #:body-formatter [format-body #f]
#:kw-arg-formatter [format-kw-arg #f]
(define-pretty (format-horizontal/helper #:body-formatter [format-body pretty]
#:kw-arg-formatter [format-kw-arg pretty]
#:kw-map [kw-map default-kw-map])
#:type list?
#:default [format-body pretty]
#:default [format-kw-arg pretty]

(flatten
(let loop ([xs doc])
Expand Down Expand Up @@ -153,10 +149,10 @@

(define-pretty format-#%app
#:type node?
#:let [xs (filter-not newl? (node-content doc))]
#:let [doc (struct-copy node doc [content xs])]
(define xs (filter-not newl? (node-content doc)))
(define doc-without-newlines (struct-copy node doc [content xs]))
(cond
[((current-app?) doc)
[((current-app?) doc-without-newlines)
(match/extract xs #:as unfits tail
;; mostly vertical
[([-head #f])
Expand All @@ -175,7 +171,8 @@
((format-horizontal/helper) (cons -head tail))))]
[_
;; pretty cases
((format-if-like/helper #:expel-first-comment? #f #:adjust #f (λ (d) fail)) doc)]))]
((format-if-like/helper #:expel-first-comment? #f #:adjust #f (λ (d) fail))
doc-without-newlines)]))]
;; perhaps full of comments, or there's nothing at all
[#:else (pretty-node #:adjust #f
(try-indent #:n 0 #:because-of xs ((format-vertical/helper) xs)))])]
Expand All @@ -189,13 +186,11 @@
(flatten (as-concat (map pretty xs))))))]))

(define-pretty (format-uniform-body/helper n
#:arg-formatter [format-arg #f]
#:body-formatter [format-body #f]
#:arg-formatter [format-arg pretty]
#:body-formatter [format-body pretty]
#:require-body? [require-body? #t]
#:kw-map [kw-map default-kw-map])
#:type node?
#:default [format-arg pretty]
#:default [format-body pretty]
(match (extract (node-content doc) (append (make-list n #t) (list #f)))
;; don't care
[#f (format-#%app doc)]
Expand Down Expand Up @@ -283,9 +278,8 @@
b)
#;(define (foo)
111111111111111111111111111111111)
(define-pretty (format-define #:head-formatter [format-head #f])
(define-pretty (format-define #:head-formatter [format-head pretty])
#:type node?
#:default [format-head pretty]
(match/extract (node-content doc) #:as unfits tail
[([-define #t] [-head #f])
;; general case
Expand Down Expand Up @@ -315,9 +309,8 @@
#;(define-values (xxxxxxxxxxx yyyyyyyyyyy) 1)
#;(define-values (xxxxxxxxxxx yyyyyyyyyyy)
11111111111111111111111111111111111111111111111111111111111111111111111)
(define-pretty (format-define-like #:head-formatter [format-head #f])
(define-pretty (format-define-like #:head-formatter [format-head pretty])
#:type node?
#:default [format-head pretty]
(match/extract (node-content doc) #:as unfits tail
[([-define #t] [-head #f])
;; general case
Expand Down
23 changes: 11 additions & 12 deletions core.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,13 @@
[_ (<$> (v-concat (map (unbox current-pretty) unfits)) doc*)]))

(define current-pretty (box #f))
(define-syntax-parameter pretty
(λ (stx) (raise-syntax-error #f "use of pretty outside its context" stx)))

(define (pretty d)
(define pretty-proc (unbox current-pretty))
(unless pretty-proc
(raise-arguments-error 'pretty "pretty can only be called during formatting"))
(pretty-proc d))

(define-syntax-parameter doc (λ (stx) (raise-syntax-error #f "use of doc outside its context" stx)))

(begin-for-syntax
Expand All @@ -125,19 +130,13 @@

(define-syntax-parse-rule (define-pretty head:header
#:type p?
{~seq #:default [from:id to]} ...
{~seq #:let [a:id b]} ...
body ...+)
#:with ooo (quote-syntax ...)
(define (head d)
(let ([pretty-proc (unbox current-pretty)])
(cond
[(p? d)
(syntax-parameterize ([pretty (make-rename-transformer #'pretty-proc)]
[doc (make-rename-transformer #'d)])
(let* ([from (or from to)] ... [a b] ...)
body ...))]
[else (raise-argument-error 'head.name (symbol->string 'p?) d)]))))
(unless (p? d)
(raise-argument-error 'head.name (symbol->string 'p?) d))
(syntax-parameterize ([doc (make-rename-transformer #'d)])
body ...)))

(define-syntax-parse-rule (pretty-node args ...)
(pretty-node* doc args ...))
Expand Down
Loading