Re: [racket-users] Help with pretty printing
I’m glad to hear it! I think that it may not fail in nice ways for deeply nested s-expressions, but that not be an issue for you. I do think that there should be a nicer way than using a text%. John > On Apr 4, 2019, at 11:14 AM, Stephen Foster wrote: > > Thanks, John. Actually, when you distinguished between the line breaks and > the indentation, that helped me come up with the following algorithm. It > basically, 1) lets pretty-print do its thing (inserting more line breaks than > I need), 2) uses a regex to scrub out all line breaks after a keywords, and > 3) fixes up the indentation after the fact. > > (define (idiomatic-pretty-format d) > (define too-many-line-breaks (pretty-format d 0)) > > (define (fix-line-breaks s) > (regexp-replace* #px"(#:\\S*)\\s*" s "\\1 “)) > > (define (fix-indentation s) > (local-require framework) > > (define t (new racket:text%)) > (send t insert s) > (send t tabify-all) > (send t get-text)) > > (fix-indentation (fix-line-breaks too-many-line-breaks)) ) > > > I wasn't able to see anything in the pretty-print hooks documentation that > seemed to promise a more elegant solution. However, perhaps I can build my > own printer with this pretty printing library: > https://docs.racket-lang.org/pprint/index.html > > NOTE: The above function does have the drawback that it relies on > racket:text% -- which seems both a little excessive and also will trigger the > "Cannot require racket/gui/base a second time" error if you try to call it > from a Scribble doc. > > Still on the hunt for a better way... > > On Tue, Apr 2, 2019 at 4:47 PM John Clements > wrote: > One note about this: it’s not really a question about indentation, which > isn’t ludicrously hard, it’s a question about inserting linebreaks, which > IMHO is much harder. Specifically, you’re asking for a pretty-printer that > treats keywords differently from other items. Have you looked through all of > the pretty-print hooks at > > https://docs.racket-lang.org/reference/pretty-print.html?q=pretty-print#%28def._%28%28lib._racket%2Fpretty..rkt%29._pretty-print%29%29 > > ? > > I’m pretty sure that what you want is not in there, but you should take a > look anyway. > > John > > > > On Apr 2, 2019, at 3:47 PM, Stephen Foster wrote: > > > > Hi all, > > > > Suppose, I have a datum that represents valid racket code, like '(test #:a > > a #:b b #: c). I'd love to render (arbitrarily deeply nested) datums like > > this to a string that displays like this: > > > > (test > > #:a a > > #:b b > > #:c c) > > > > Pretty printing almost works: > > > > (displayln > >(pretty-format > > '(test #:a a #:b b #:c c) > >24)) > > > > '(test > > #:a > > a > > #:b > > b > > #:c > > c) > > > > I thought that fiddling with the columns parameter would help, but it > > doesn't: > > > > (displayln > >(pretty-format > > '(test #:a a #:b b #:c c) > >25)) > > > > (test #:a a #:b b #:c c) > > > > Maybe there's some way can get racket/pretty to do what I want -- though I > > didn't see anything jump out at me in docs. > > > > I was about to write my own pretty printer, but I figured that with so much > > stuff out there for typesetting Racket code (e.g in Scribble), I should at > > least ask if someone knows of something that does what I want: Basically, > > magically formats datums according to Rackety indentation idioms. > > > > Thanks, > > Stephen > > > > > > -- > > You received this message because you are subscribed to the Google Groups > > "Racket Users" group. > > To unsubscribe from this group and stop receiving emails from it, send an > > email to racket-users+unsubscr...@googlegroups.com. > > For more options, visit https://groups.google.com/d/optout. > > > > > > -- > > > Stephen Foster > ThoughtSTEM Co-Founder > 318-792-2035 -- You received this message because you are subscribed to the Google Groups "Racket Users" group. To unsubscribe from this group and stop receiving emails from it, send an email to racket-users+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Re: [racket-users] Help with pretty printing
Thanks, John. Actually, when you distinguished between the line breaks and the indentation, that helped me come up with the following algorithm. It basically, 1) lets pretty-print do its thing (inserting more line breaks than I need), 2) uses a regex to scrub out all line breaks after a keywords, and 3) fixes up the indentation after the fact. (define (idiomatic-pretty-format d) (define too-many-line-breaks (pretty-format d 0)) (define (fix-line-breaks s) (regexp-replace* #px"(#:\\S*)\\s*" s "\\1 ")) (define (fix-indentation s) (local-require framework) (define t (new racket:text%)) (send t insert s) (send t tabify-all) (send t get-text)) (fix-indentation (fix-line-breaks too-many-line-breaks)) ) I wasn't able to see anything in the pretty-print hooks documentation that seemed to promise a more elegant solution. However, perhaps I can build my own printer with this pretty printing library: https://docs.racket-lang.org/pprint/index.html NOTE: The above function does have the drawback that it relies on racket:text% -- which seems both a little excessive and also will trigger the "Cannot require racket/gui/base a second time" error if you try to call it from a Scribble doc. Still on the hunt for a better way... On Tue, Apr 2, 2019 at 4:47 PM John Clements wrote: > One note about this: it’s not really a question about indentation, which > isn’t ludicrously hard, it’s a question about inserting linebreaks, which > IMHO is much harder. Specifically, you’re asking for a pretty-printer that > treats keywords differently from other items. Have you looked through all > of the pretty-print hooks at > > > https://docs.racket-lang.org/reference/pretty-print.html?q=pretty-print#%28def._%28%28lib._racket%2Fpretty..rkt%29._pretty-print%29%29 > > ? > > I’m pretty sure that what you want is not in there, but you should take a > look anyway. > > John > > > > On Apr 2, 2019, at 3:47 PM, Stephen Foster > wrote: > > > > Hi all, > > > > Suppose, I have a datum that represents valid racket code, like '(test > #:a a #:b b #: c). I'd love to render (arbitrarily deeply nested) datums > like this to a string that displays like this: > > > > (test > > #:a a > > #:b b > > #:c c) > > > > Pretty printing almost works: > > > > (displayln > >(pretty-format > > '(test #:a a #:b b #:c c) > >24)) > > > > '(test > > #:a > > a > > #:b > > b > > #:c > > c) > > > > I thought that fiddling with the columns parameter would help, but it > doesn't: > > > > (displayln > >(pretty-format > > '(test #:a a #:b b #:c c) > >25)) > > > > (test #:a a #:b b #:c c) > > > > Maybe there's some way can get racket/pretty to do what I want -- though > I didn't see anything jump out at me in docs. > > > > I was about to write my own pretty printer, but I figured that with so > much stuff out there for typesetting Racket code (e.g in Scribble), I > should at least ask if someone knows of something that does what I want: > Basically, magically formats datums according to Rackety indentation > idioms. > > > > Thanks, > > Stephen > > > > > > -- > > You received this message because you are subscribed to the Google > Groups "Racket Users" group. > > To unsubscribe from this group and stop receiving emails from it, send > an email to racket-users+unsubscr...@googlegroups.com. > > For more options, visit https://groups.google.com/d/optout. > > > > -- Stephen Foster ThoughtSTEM Co-Founder 318-792-2035 -- You received this message because you are subscribed to the Google Groups "Racket Users" group. To unsubscribe from this group and stop receiving emails from it, send an email to racket-users+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Re: [racket-users] Help with pretty printing
One note about this: it’s not really a question about indentation, which isn’t ludicrously hard, it’s a question about inserting linebreaks, which IMHO is much harder. Specifically, you’re asking for a pretty-printer that treats keywords differently from other items. Have you looked through all of the pretty-print hooks at https://docs.racket-lang.org/reference/pretty-print.html?q=pretty-print#%28def._%28%28lib._racket%2Fpretty..rkt%29._pretty-print%29%29 ? I’m pretty sure that what you want is not in there, but you should take a look anyway. John > On Apr 2, 2019, at 3:47 PM, Stephen Foster wrote: > > Hi all, > > Suppose, I have a datum that represents valid racket code, like '(test #:a a > #:b b #: c). I'd love to render (arbitrarily deeply nested) datums like this > to a string that displays like this: > > (test > #:a a > #:b b > #:c c) > > Pretty printing almost works: > > (displayln >(pretty-format > '(test #:a a #:b b #:c c) >24)) > > '(test > #:a > a > #:b > b > #:c > c) > > I thought that fiddling with the columns parameter would help, but it doesn't: > > (displayln >(pretty-format > '(test #:a a #:b b #:c c) >25)) > > (test #:a a #:b b #:c c) > > Maybe there's some way can get racket/pretty to do what I want -- though I > didn't see anything jump out at me in docs. > > I was about to write my own pretty printer, but I figured that with so much > stuff out there for typesetting Racket code (e.g in Scribble), I should at > least ask if someone knows of something that does what I want: Basically, > magically formats datums according to Rackety indentation idioms. > > Thanks, > Stephen > > > -- > You received this message because you are subscribed to the Google Groups > "Racket Users" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to racket-users+unsubscr...@googlegroups.com. > For more options, visit https://groups.google.com/d/optout. -- You received this message because you are subscribed to the Google Groups "Racket Users" group. To unsubscribe from this group and stop receiving emails from it, send an email to racket-users+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
[racket-users] Help with pretty printing
Hi all, Suppose, I have a datum that represents valid racket code, like '(test #:a a #:b b #: c). I'd love to render (arbitrarily deeply nested) datums like this to a string that displays like this: (test #:a a #:b b #:c c) Pretty printing almost works: (displayln (pretty-format '(test #:a a #:b b #:c c) 24)) '(test #:a a #:b b #:c c) I thought that fiddling with the columns parameter would help, but it doesn't: (displayln (pretty-format '(test #:a a #:b b #:c c) 25)) (test #:a a #:b b #:c c) Maybe there's some way can get racket/pretty to do what I want -- though I didn't see anything jump out at me in docs. I was about to write my own pretty printer, but I figured that with so much stuff out there for typesetting Racket code (e.g in Scribble), I should at least ask if someone knows of something that does what I want: Basically, magically formats datums according to Rackety indentation idioms. Thanks, Stephen -- You received this message because you are subscribed to the Google Groups "Racket Users" group. To unsubscribe from this group and stop receiving emails from it, send an email to racket-users+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.