read-print: 'pretty-print-with-comments' keeps newlines on long strings.

* guix/read-print.scm (printed-string)[preserve-newlines?]: New
procedure.
Use it to preserve newlines on long strings.
* tests/read-print.scm: Add test.
This commit is contained in:
Ludovic Courtès 2023-04-24 10:10:00 +02:00 committed by Ludovic Courtès
parent 74e96c4cb1
commit 7931ac810b
No known key found for this signature in database
GPG key ID: 090B11993D9AEBB5
2 changed files with 14 additions and 2 deletions

View file

@ -420,11 +420,18 @@ (define %natural-whitespace-string-forms
(define (printed-string str context)
"Return the read syntax for STR depending on CONTEXT."
(define (preserve-newlines? str)
(and (> (string-length str) 40)
(string-index str #\newline)))
(match context
(()
(object->string str))
(if (preserve-newlines? str)
(escaped-string str)
(object->string str)))
((head . _)
(if (memq head %natural-whitespace-string-forms)
(if (or (memq head %natural-whitespace-string-forms)
(preserve-newlines? str))
(escaped-string str)
(object->string str)))))

View file

@ -194,6 +194,11 @@ (define-syntax-rule (test-pretty-print/sequence str args ...)
(test-pretty-print "\
(string-append \"a\\tb\" \"\\n\")")
(test-pretty-print "\
(display \"This is a very long string.
It contains line breaks, which are preserved,
because it's a long string.\")")
(test-pretty-print "\
(description \"abcdefghijkl
mnopqrstuvwxyz.\")"