From: "Ludovic Courtès" <ludo@gnu.org>
To: 56898@debbugs.gnu.org
Cc: "Ludovic Courtès" <ludo@gnu.org>
Subject: [bug#56898] [PATCH 06/13] read-print: Read and render vertical space.
Date: Tue, 2 Aug 2022 23:44:12 +0200 [thread overview]
Message-ID: <20220802214419.19013-6-ludo@gnu.org> (raw)
In-Reply-To: <20220802214419.19013-1-ludo@gnu.org>
* guix/read-print.scm (<vertical-space>, vertical-space?)
(vertical-space, vertical-space-height): New variables.
(combine-vertical-space, canonicalize-vertical-space)
(read-vertical-space): New procedures.
(read-with-comments): Use it in the #\newline case.
(pretty-print-with-comments): Add #:format-vertical-space and honor it.
Add case for 'vertical-space?'.
* guix/scripts/style.scm (format-package-definition): Pass
#:format-vertical-space to 'object->string*'.
* tests/read-print.scm ("read-with-comments: list with blank line")
("read-with-comments: list with multiple blank lines")
("read-with-comments: top-level blank lines")
("pretty-print-with-comments, canonicalize-vertical-space"): New tests.
Add a couple of additional round-trip tests.
---
guix/read-print.scm | 54 ++++++++++++++++++++++++++++--
guix/scripts/style.scm | 3 +-
tests/read-print.scm | 76 +++++++++++++++++++++++++++++++++++++++++-
3 files changed, 129 insertions(+), 4 deletions(-)
diff --git a/guix/read-print.scm b/guix/read-print.scm
index 732d0dc1f8..2b626ba281 100644
--- a/guix/read-print.scm
+++ b/guix/read-print.scm
@@ -30,6 +30,11 @@ (define-module (guix read-print)
blank?
+ vertical-space
+ vertical-space?
+ vertical-space-height
+ canonicalize-vertical-space
+
comment
comment?
comment->string
@@ -58,6 +63,26 @@ (define <blank>
(define blank? (record-predicate <blank>))
+(define <vertical-space>
+ (make-record-type '<vertical-space> '(height)
+ #:parent <blank>
+ #:extensible? #f))
+
+(define vertical-space? (record-predicate <vertical-space>))
+(define vertical-space (record-type-constructor <vertical-space>))
+(define vertical-space-height (record-accessor <vertical-space> 'height))
+
+(define (combine-vertical-space x y)
+ "Return vertical space as high as the combination of X and Y."
+ (vertical-space (+ (vertical-space-height x)
+ (vertical-space-height y))))
+
+(define canonicalize-vertical-space
+ (let ((unit (vertical-space 1)))
+ (lambda (space)
+ "Return a vertical space corresponding to a single blank line."
+ unit)))
+
(define <comment>
;; Comments.
(make-record-type '<comment> '(str margin?)
@@ -80,6 +105,19 @@ (define* (comment str #:optional margin?)
(&message (message "invalid comment string")))))
(string->comment str margin?))
+(define (read-vertical-space port)
+ "Read from PORT until a non-vertical-space character is met, and return a
+single <vertical-space> record."
+ (define (space? chr)
+ (char-set-contains? char-set:whitespace chr))
+
+ (let loop ((height 1))
+ (match (read-char port)
+ (#\newline (loop (+ 1 height)))
+ ((? eof-object?) (vertical-space height))
+ ((? space?) (loop height))
+ (chr (unread-char chr port) (vertical-space height)))))
+
(define (read-with-comments port)
"Like 'read', but include <blank> objects when they're encountered."
;; Note: Instead of implementing this functionality in 'read' proper, which
@@ -107,7 +145,9 @@ (define (reverse/dot lst)
eof) ;oops!
(chr
(cond ((eqv? chr #\newline)
- (loop #t return))
+ (if blank-line?
+ (read-vertical-space port)
+ (loop #t return)))
((char-set-contains? char-set:whitespace chr)
(loop blank-line? return))
((memv chr '(#\( #\[))
@@ -297,6 +337,7 @@ (define (canonicalize-comment c)
(define* (pretty-print-with-comments port obj
#:key
(format-comment identity)
+ (format-vertical-space identity)
(indent 0)
(max-width 78)
(long-list 5))
@@ -306,7 +347,8 @@ (define* (pretty-print-with-comments port obj
Lists longer than LONG-LIST are written as one element per line. Comments are
passed through FORMAT-COMMENT before being emitted; a useful value for
-FORMAT-COMMENT is 'canonicalize-comment'."
+FORMAT-COMMENT is 'canonicalize-comment'. Vertical space is passed through
+FORMAT-VERTICAL-SPACE; a useful value of 'canonicalize-vertical-space'."
(define (list-of-lists? head tail)
;; Return true if HEAD and TAIL denote a list of lists--e.g., a list of
;; 'let' bindings.
@@ -394,6 +436,14 @@ (define (special-form? head)
port)))
(display (make-string indent #\space) port)
indent)
+ ((? vertical-space? space)
+ (unless delimited? (newline port))
+ (let loop ((i (vertical-space-height (format-vertical-space space))))
+ (unless (zero? i)
+ (newline port)
+ (loop (- i 1))))
+ (display (make-string indent #\space) port)
+ indent)
(('quote lst)
(unless delimited? (display " " port))
(display "'" port)
diff --git a/guix/scripts/style.scm b/guix/scripts/style.scm
index 5c0ecc0896..2e14bc68fd 100644
--- a/guix/scripts/style.scm
+++ b/guix/scripts/style.scm
@@ -316,7 +316,8 @@ (define* (format-package-definition package
(object->string* exp
(location-column
(package-definition-location package))
- #:format-comment canonicalize-comment)))))
+ #:format-comment canonicalize-comment
+ #:format-vertical-space canonicalize-vertical-space)))))
(define (package-location<? p1 p2)
"Return true if P1's location is \"before\" P2's."
diff --git a/tests/read-print.scm b/tests/read-print.scm
index e9ba1127d4..f915b7e2d2 100644
--- a/tests/read-print.scm
+++ b/tests/read-print.scm
@@ -19,7 +19,8 @@
(define-module (tests-style)
#:use-module (guix read-print)
#:use-module (guix gexp) ;for the reader extensions
- #:use-module (srfi srfi-64))
+ #:use-module (srfi srfi-64)
+ #:use-module (ice-9 match))
(define-syntax-rule (test-pretty-print str args ...)
"Test equality after a round-trip where STR is passed to
@@ -40,6 +41,35 @@ (define-syntax-rule (test-pretty-print str args ...)
(call-with-input-string "(a . b)"
read-with-comments))
+(test-equal "read-with-comments: list with blank line"
+ `(list with ,(vertical-space 1) blank line)
+ (call-with-input-string "\
+(list with
+
+ blank line)\n"
+ read-with-comments))
+
+(test-equal "read-with-comments: list with multiple blank lines"
+ `(list with ,(comment ";multiple\n" #t)
+ ,(vertical-space 3) blank lines)
+ (call-with-input-string "\
+(list with ;multiple
+
+
+
+ blank lines)\n"
+ read-with-comments))
+
+(test-equal "read-with-comments: top-level blank lines"
+ (list (vertical-space 2) '(a b c) (vertical-space 2))
+ (call-with-input-string "
+
+(a b c)\n\n"
+ (lambda (port)
+ (list (read-with-comments port)
+ (read-with-comments port)
+ (read-with-comments port)))))
+
(test-pretty-print "(list 1 2 3 4)")
(test-pretty-print "((a . 1) (b . 2))")
(test-pretty-print "(a b c . boom)")
@@ -181,6 +211,24 @@ (define-syntax-rule (test-pretty-print str args ...)
`(cons \"--without-any-problem\"
,flags)))")
+(test-pretty-print "\
+(vertical-space one:
+
+ two:
+
+
+ three:
+
+
+
+ end)")
+
+(test-pretty-print "\
+(vertical-space one
+
+ ;; Comment after blank line.
+ two)")
+
(test-equal "pretty-print-with-comments, canonicalize-comment"
"\
(list abc
@@ -206,4 +254,30 @@ (define-syntax-rule (test-pretty-print str args ...)
#:format-comment
canonicalize-comment)))))
+(test-equal "pretty-print-with-comments, canonicalize-vertical-space"
+ "\
+(list abc
+
+ def
+
+ ;; last one
+ ghi)"
+ (let ((sexp (call-with-input-string
+ "\
+(list abc
+
+
+
+ def
+
+
+;; last one
+ ghi)"
+ read-with-comments)))
+ (call-with-output-string
+ (lambda (port)
+ (pretty-print-with-comments port sexp
+ #:format-vertical-space
+ canonicalize-vertical-space)))))
+
(test-end)
--
2.37.1
next prev parent reply other threads:[~2022-08-02 21:47 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-08-02 21:42 [bug#56898] [PATCH 00/13] Put the pretty printer to good use Ludovic Courtès
2022-08-02 21:44 ` [bug#56898] [PATCH 01/13] style: Move reader and printer to (guix read-print) Ludovic Courtès
2022-08-02 21:44 ` [bug#56898] [PATCH 02/13] read-print: Add System and Home special forms Ludovic Courtès
2022-08-02 21:44 ` [bug#56898] [PATCH 03/13] read-print: Expose comment constructor Ludovic Courtès
2022-08-02 21:44 ` [bug#56898] [PATCH 04/13] read-print: Introduce <blank> parent class of <comment> Ludovic Courtès
2022-08-02 21:44 ` [bug#56898] [PATCH 05/13] style: Adjust test to not emit blank lines Ludovic Courtès
2022-08-02 21:44 ` Ludovic Courtès [this message]
2022-08-02 21:44 ` [bug#56898] [PATCH 07/13] read-print: Recognize page breaks Ludovic Courtès
2022-08-02 21:44 ` [bug#56898] [PATCH 08/13] read-print: Add code to read and write sequences of expressions/blanks Ludovic Courtès
2022-08-02 21:44 ` [bug#56898] [PATCH 09/13] read-print: 'canonicalize-comment' leaves top-level comments unchanged Ludovic Courtès
2022-08-02 21:44 ` [bug#56898] [PATCH 10/13] style: Add '--whole-file' option Ludovic Courtès
2022-08-02 21:44 ` [bug#56898] [PATCH 11/13] read-print: Support printing multi-line comments Ludovic Courtès
2022-08-02 21:44 ` [bug#56898] [PATCH 12/13] installer: Render the final configuration with (guix read-print) Ludovic Courtès
2022-08-02 21:44 ` [bug#56898] [PATCH 13/13] installer: Add comments and vertical space to the generated config Ludovic Courtès
2022-08-07 10:50 ` [bug#56898] [PATCH 00/13] Put the pretty printer to good use Mathieu Othacehe
2022-08-07 20:18 ` Ludovic Courtès
2022-08-09 9:42 ` bug#56898: " Ludovic Courtès
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://guix.gnu.org/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20220802214419.19013-6-ludo@gnu.org \
--to=ludo@gnu.org \
--cc=56898@debbugs.gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://git.savannah.gnu.org/cgit/guix.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).