From f687e27e0385c7f9bab8d967293061158fc3f504 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ludovic=20Court=C3=A8s?= Date: Tue, 2 Aug 2022 11:57:39 +0200 Subject: read-print: Read and render vertical space. * guix/read-print.scm (, 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 +++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 52 insertions(+), 2 deletions(-) (limited to 'guix/read-print.scm') 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 )) +(define + (make-record-type ' '(height) + #:parent + #:extensible? #f)) + +(define vertical-space? (record-predicate )) +(define vertical-space (record-type-constructor )) +(define vertical-space-height (record-accessor '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 ;; Comments. (make-record-type ' '(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 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 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) -- cgit v1.2.3