aboutsummaryrefslogtreecommitdiff
path: root/guix/read-print.scm
diff options
context:
space:
mode:
authorLudovic Courtès <ludo@gnu.org>2022-08-02 11:57:39 +0200
committerLudovic Courtès <ludo@gnu.org>2022-08-08 11:22:32 +0200
commitf687e27e0385c7f9bab8d967293061158fc3f504 (patch)
tree13ea17d05751f61e8f3b6c6ad057a076ec739488 /guix/read-print.scm
parent3eb3901d7f1d4aae134cb64aa703af67c3c27cdf (diff)
downloadguix-f687e27e0385c7f9bab8d967293061158fc3f504.tar
guix-f687e27e0385c7f9bab8d967293061158fc3f504.tar.gz
read-print: Read and render vertical space.
* 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.
Diffstat (limited to 'guix/read-print.scm')
-rw-r--r--guix/read-print.scm54
1 files changed, 52 insertions, 2 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 @@
blank?
+ vertical-space
+ vertical-space?
+ vertical-space-height
+ canonicalize-vertical-space
+
comment
comment?
comment->string
@@ -58,6 +63,26 @@
(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 @@ end with newline, otherwise an error is raised."
(&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 @@ end with newline, otherwise an error is raised."
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 @@ semicolons."
(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 @@ included in the output.
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 @@ FORMAT-COMMENT is 'canonicalize-comment'."
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)