From patchwork Tue Aug 2 21:44:15 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: 41106 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 AA4E427BBEA; Tue, 2 Aug 2022 23:45: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=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 255B127BBE9 for ; Tue, 2 Aug 2022 23:45:49 +0100 (BST) Received: from localhost ([::1]:60288 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1oIzjB-0003Rl-S0 for patchwork@mira.cbaines.net; Tue, 02 Aug 2022 17:47:01 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:44456) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oIzhJ-0001xB-AU for guix-patches@gnu.org; Tue, 02 Aug 2022 17:45:05 -0400 Received: from debbugs.gnu.org ([209.51.188.43]:55561) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1oIzhJ-0006QY-0t 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 1oIzhI-0003A9-U2 for guix-patches@gnu.org; Tue, 02 Aug 2022 17:45:04 -0400 X-Loop: help-debbugs@gnu.org Subject: [bug#56898] [PATCH 09/13] read-print: 'canonicalize-comment' leaves top-level comments unchanged. 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:04 +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.165947668512023 (code B ref 56898); Tue, 02 Aug 2022 21:45:04 +0000 Received: (at 56898) by debbugs.gnu.org; 2 Aug 2022 21:44:45 +0000 Received: from localhost ([127.0.0.1]:45293 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1oIzgy-00037l-SH for submit@debbugs.gnu.org; Tue, 02 Aug 2022 17:44:45 -0400 Received: from eggs.gnu.org ([209.51.188.92]:59580) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1oIzgs-00035e-Fg for 56898@debbugs.gnu.org; Tue, 02 Aug 2022 17:44:38 -0400 Received: from fencepost.gnu.org ([2001:470:142:3::e]:55166) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oIzgm-0006A9-U8; Tue, 02 Aug 2022 17:44:32 -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=gB9ZTp7URun+su0eqU4MpFljdsqgu/OtWTeD0CuD8AI=; b=hW5ZeySLY/TvtlsuATfo g0b3ECKB8PAeTXf1cdOkiFg+F0bfHXqZ1iM29ADlgpGU4biFGC9PIIZ/KCA3IaR5R/8oF3881emEx PSVIESKsaYJBRU26B12DQkbwA65km+vbjY4lqrcx0tQxbX2YFonJRYh/fjOioYy2Zjuo06fO9gaSx 9LTC4PV6LHCsBjn2BjM7xG4602VK4PVWWfFcAd23LovBjtWfXoCcYieRZqV/aQez1qQVWXTcNaaua OTknafl7WlE7x1jaKK14mkXFPpBPJWLY56cdvtOZ80RWqWKJ7qjIEt/hKtiuKTQ3+MH5kE4RWRcra goRJDhOQKZPqng==; 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 1oIzgm-0006W4-GU; Tue, 02 Aug 2022 17:44:32 -0400 From: Ludovic =?utf-8?q?Court=C3=A8s?= Date: Tue, 2 Aug 2022 23:44:15 +0200 Message-Id: <20220802214419.19013-9-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 This lets users use three leading semicolons, for instance, in top-level comments. * guix/read-print.scm (canonicalize-comment): Add INDENT parameter and honor it. (pretty-print-with-comments): Change default value of #:format-comment. Call FORMAT-COMMENT with INDENT as the second argument. * tests/read-print.scm: Adjust test accordingly. --- guix/read-print.scm | 35 +++++++++++++++++++---------------- tests/read-print.scm | 4 +++- 2 files changed, 22 insertions(+), 17 deletions(-) diff --git a/guix/read-print.scm b/guix/read-print.scm index 4a3afdd4f9..2fc3d85a25 100644 --- a/guix/read-print.scm +++ b/guix/read-print.scm @@ -371,23 +371,26 @@ (define (string-width str) "Return the \"width\" of STR--i.e., the width of the longest line of STR." (apply max (map string-length (string-split str #\newline)))) -(define (canonicalize-comment c) - "Canonicalize comment C, ensuring it has the \"right\" number of leading -semicolons." - (let ((line (string-trim-both - (string-trim (comment->string c) (char-set #\;))))) - (string->comment (string-append - (if (comment-margin? c) - ";" - (if (string-null? line) - ";;" ;no trailing space - ";; ")) - line "\n") - (comment-margin? c)))) +(define (canonicalize-comment comment indent) + "Canonicalize COMMENT, which is to be printed at INDENT, ensuring it has the +\"right\" number of leading semicolons." + (if (zero? indent) + comment ;leave top-level comments unchanged + (let ((line (string-trim-both + (string-trim (comment->string comment) (char-set #\;))))) + (string->comment (string-append + (if (comment-margin? comment) + ";" + (if (string-null? line) + ";;" ;no trailing space + ";; ")) + line "\n") + (comment-margin? comment))))) (define* (pretty-print-with-comments port obj #:key - (format-comment identity) + (format-comment + (lambda (comment indent) comment)) (format-vertical-space identity) (indent 0) (max-width 78) @@ -475,7 +478,7 @@ (define (special-form? head) (if (comment-margin? comment) (begin (display " " port) - (display (comment->string (format-comment comment)) + (display (comment->string (format-comment comment indent)) port)) (begin ;; When already at the beginning of a line, for example because @@ -483,7 +486,7 @@ (define (special-form? head) (unless (= column indent) (newline port) (display (make-string indent #\space) port)) - (display (comment->string (format-comment comment)) + (display (comment->string (format-comment comment indent)) port))) (display (make-string indent #\space) port) indent) diff --git a/tests/read-print.scm b/tests/read-print.scm index 94f018dd44..e3f23194af 100644 --- a/tests/read-print.scm +++ b/tests/read-print.scm @@ -274,6 +274,7 @@ (define-syntax-rule (test-pretty-print/sequence str args ...) (test-pretty-print/sequence " ;;; Hello! +;;; Notice that there are three semicolons here. (define-module (foo bar) #:use-module (guix) @@ -286,7 +287,8 @@ (define-module (foo bar) (locale \"eo_EO.UTF-8\") (services - (cons (service mcron-service-type) %base-services)))\n") + (cons (service mcron-service-type) %base-services)))\n" + #:format-comment canonicalize-comment) (test-equal "pretty-print-with-comments, canonicalize-comment" "\