From patchwork Tue Aug 2 21:44:17 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: 4178 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 911DD27BBEA; Wed, 3 Aug 2022 00:34:24 +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=unavailable 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 AB16427BBE9 for ; Wed, 3 Aug 2022 00:34:23 +0100 (BST) Received: from localhost ([::1]:33008 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1oIzjh-00047Z-09 for patchwork@mira.cbaines.net; Tue, 02 Aug 2022 17:47:33 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:44460) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oIzhJ-0001xs-Pb for guix-patches@gnu.org; Tue, 02 Aug 2022 17:45:05 -0400 Received: from debbugs.gnu.org ([209.51.188.43]:55562) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1oIzhJ-0006Qh-Fs for guix-patches@gnu.org; Tue, 02 Aug 2022 17:45:05 -0400 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1oIzhJ-0003AH-D3 for guix-patches@gnu.org; Tue, 02 Aug 2022 17:45:05 -0400 X-Loop: help-debbugs@gnu.org Subject: [bug#56898] [PATCH 11/13] read-print: Support printing multi-line comments. 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:05 +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.165947668512031 (code B ref 56898); Tue, 02 Aug 2022 21:45:05 +0000 Received: (at 56898) by debbugs.gnu.org; 2 Aug 2022 21:44:45 +0000 Received: from localhost ([127.0.0.1]:45295 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1oIzgz-00037s-8t for submit@debbugs.gnu.org; Tue, 02 Aug 2022 17:44:45 -0400 Received: from eggs.gnu.org ([209.51.188.92]:59588) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1oIzgt-00035h-BH for 56898@debbugs.gnu.org; Tue, 02 Aug 2022 17:44:40 -0400 Received: from fencepost.gnu.org ([2001:470:142:3::e]:55170) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oIzgo-0006AT-3r; Tue, 02 Aug 2022 17:44:34 -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=Uu20pag2ZjXZAsB2uO02hiBMS5iRlVv/Ghp+wid3TO0=; b=D/bwoI7pbITG3gwaubxh iPAykjtbXM8cF8POlYRmCX17ThcW/IpfOCxWkVzIgiPTm+4rXMNNB88ysZLJUkfjtgkBtNw/ooQ7e pDKVzlLJMwj5vEsFDiF9LdlPFYcysLQsdZMT+bBz+xIMSk+hlSghEHaK6IayHFJw5to+XPzwE3wCF LVX3pkDG7xPdB2dyWfgwhmAcrcazO3DmA0bxpSAW15bSFce6PlBWqd2DPui7LHYFkm8QIFumrQjK2 JSE3foM8Zj3DclnreHv4icMz78t81okFEspu30wT1eLMCvgGRu9g3PGQFk69v2jtHr66/O5pETCcJ dvvCHE/mN/OKVg==; 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 1oIzgn-0006W4-O0; Tue, 02 Aug 2022 17:44:33 -0400 From: Ludovic =?utf-8?q?Court=C3=A8s?= Date: Tue, 2 Aug 2022 23:44:17 +0200 Message-Id: <20220802214419.19013-11-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 (%not-newline): New variable. (print-multi-line-comment): New procedure. (pretty-print-with-comments): Use it. * tests/read-print.scm ("pretty-print-with-comments, multi-line comment"): New test. --- guix/read-print.scm | 26 ++++++++++++++++++++++++-- tests/read-print.scm | 14 ++++++++++++++ 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/guix/read-print.scm b/guix/read-print.scm index 2fc3d85a25..df25eb0f50 100644 --- a/guix/read-print.scm +++ b/guix/read-print.scm @@ -387,6 +387,27 @@ (define (canonicalize-comment comment indent) line "\n") (comment-margin? comment))))) +(define %not-newline + (char-set-complement (char-set #\newline))) + +(define (print-multi-line-comment str indent port) + "Print to PORT STR as a multi-line comment, with INDENT spaces preceding +each line except the first one (they're assumed to be already there)." + + ;; While 'read-with-comments' only returns one-line comments, user-provided + ;; comments might span multiple lines, which is why this is necessary. + (let loop ((lst (string-tokenize str %not-newline))) + (match lst + (() #t) + ((last) + (display last port) + (newline port)) + ((head tail ...) + (display head port) + (newline port) + (display (make-string indent #\space) port) + (loop tail))))) + (define* (pretty-print-with-comments port obj #:key (format-comment @@ -486,8 +507,9 @@ (define (special-form? head) (unless (= column indent) (newline port) (display (make-string indent #\space) port)) - (display (comment->string (format-comment comment indent)) - port))) + (print-multi-line-comment (comment->string + (format-comment comment indent)) + indent port))) (display (make-string indent #\space) port) indent) ((? vertical-space? space) diff --git a/tests/read-print.scm b/tests/read-print.scm index e3f23194af..004fcff19f 100644 --- a/tests/read-print.scm +++ b/tests/read-print.scm @@ -341,4 +341,18 @@ (define-module (foo bar) #:format-vertical-space canonicalize-vertical-space))))) +(test-equal "pretty-print-with-comments, multi-line comment" + "\ +(list abc + ;; This comment spans + ;; two lines. + def)" + (call-with-output-string + (lambda (port) + (pretty-print-with-comments port + `(list abc ,(comment "\ +;; This comment spans\n +;; two lines.\n") + def))))) + (test-end)