From patchwork Tue Aug 2 21:44:12 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?q?Ludovic_Court=C3=A8s?= X-Patchwork-Id: 41109 Return-Path: X-Original-To: patchwork@mira.cbaines.net Delivered-To: patchwork@mira.cbaines.net Received: by mira.cbaines.net (Postfix, from userid 113) id 3F47B27BBEA; Tue, 2 Aug 2022 23:57:49 +0100 (BST) X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on mira.cbaines.net X-Spam-Level: X-Spam-Status: No, score=-2.7 required=5.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,MAILING_LIST_MULTI,SPF_HELO_PASS,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mira.cbaines.net (Postfix) with ESMTPS id 5FF5627BBE9 for ; Tue, 2 Aug 2022 23:57:48 +0100 (BST) Received: from localhost ([::1]:60876 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1oIzjI-0003s0-LC for patchwork@mira.cbaines.net; Tue, 02 Aug 2022 17:47:08 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:44444) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oIzhI-0001v9-72 for guix-patches@gnu.org; Tue, 02 Aug 2022 17:45:04 -0400 Received: from debbugs.gnu.org ([209.51.188.43]:55558) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1oIzhH-0006KT-Tm for guix-patches@gnu.org; Tue, 02 Aug 2022 17:45:03 -0400 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1oIzhH-00039n-Py for guix-patches@gnu.org; Tue, 02 Aug 2022 17:45:03 -0400 X-Loop: help-debbugs@gnu.org Subject: [bug#56898] [PATCH 06/13] read-print: Read and render vertical space. Resent-From: Ludovic =?utf-8?q?Court=C3=A8s?= Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Tue, 02 Aug 2022 21:45:03 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 56898 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: 56898@debbugs.gnu.org Cc: Ludovic =?utf-8?q?Court=C3=A8s?= Received: via spool by 56898-submit@debbugs.gnu.org id=B56898.165947668412001 (code B ref 56898); Tue, 02 Aug 2022 21:45:03 +0000 Received: (at 56898) by debbugs.gnu.org; 2 Aug 2022 21:44:44 +0000 Received: from localhost ([127.0.0.1]:45287 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1oIzgx-00037O-Dj for submit@debbugs.gnu.org; Tue, 02 Aug 2022 17:44:43 -0400 Received: from eggs.gnu.org ([209.51.188.92]:59552) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1oIzgq-00035V-8c for 56898@debbugs.gnu.org; Tue, 02 Aug 2022 17:44:36 -0400 Received: from fencepost.gnu.org ([2001:470:142:3::e]:55160) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oIzgl-00069m-0U; Tue, 02 Aug 2022 17:44:31 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=gnu.org; s=fencepost-gnu-org; h=MIME-Version:References:In-Reply-To:Date:Subject:To: From; bh=kR5dSnJ5x/ASJtlbQSE9/nsQz3ihJBLIwzHylHZweAE=; b=WlhD+agcaUV+Is9h80n0 Jwpac5mTcR0W2pLOJXJ0XrwQE+AXgLh9ATJRG1spPIyDjuzvdjpfv1lqYVVBQspytNgwH1tW83vpX 8kAOyg233r8cHVtpeQiMXZI6u+RlyQNclap1L+WuOwpBVATRFFuK8wKsXv1ydu0TLLLsjOqYSwyUX 9ak0Q8jxYFZlu4gmeFuBTwR7Jntf9iQyablXVJGfpeowTvS+r35CfIX1nbD8cPMfvkxtzPWb5wuac OGYtreKaJtudi7qvnaqvUdfNNXnoRqVPdJ0lTAv4mc4GkTHFQlnc116bvj6btX7Eabqv2rs1z9hJd 5/PzQBXy3LAdNw==; Received: from 91-160-117-201.subs.proxad.net ([91.160.117.201]:52235 helo=gnu.org) by fencepost.gnu.org with esmtpsa (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1oIzgk-0006W4-Kp; Tue, 02 Aug 2022 17:44:30 -0400 From: Ludovic =?utf-8?q?Court=C3=A8s?= Date: Tue, 2 Aug 2022 23:44:12 +0200 Message-Id: <20220802214419.19013-6-ludo@gnu.org> X-Mailer: git-send-email 2.37.1 In-Reply-To: <20220802214419.19013-1-ludo@gnu.org> References: <20220802214419.19013-1-ludo@gnu.org> MIME-Version: 1.0 X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: guix-patches@gnu.org List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guix-patches-bounces+patchwork=mira.cbaines.net@gnu.org Sender: "Guix-patches" X-getmail-retrieved-from-mailbox: Patches * 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 ++++++++++++++++++++++++++++-- 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 (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 @@ (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 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 @@ (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