From patchwork Mon Jan 3 11:24:39 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: 35912 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 8010E27BBE9; Mon, 3 Jan 2022 11:25:49 +0000 (GMT) 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,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL, 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 94FEA27BBEA for ; Mon, 3 Jan 2022 11:25:48 +0000 (GMT) Received: from localhost ([::1]:46386 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1n4LTH-0005NV-Id for patchwork@mira.cbaines.net; Mon, 03 Jan 2022 06:25:47 -0500 Received: from eggs.gnu.org ([209.51.188.92]:51494) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1n4LSt-0005J8-Gx for guix-patches@gnu.org; Mon, 03 Jan 2022 06:25:23 -0500 Received: from debbugs.gnu.org ([209.51.188.43]:50958) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1n4LSa-0001pB-T5 for guix-patches@gnu.org; Mon, 03 Jan 2022 06:25:19 -0500 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1n4LSa-0005wh-MP for guix-patches@gnu.org; Mon, 03 Jan 2022 06:25:04 -0500 X-Loop: help-debbugs@gnu.org Subject: [bug#52974] [PATCH 5/5] style: '-S format' canonicalizes comments. Resent-From: Ludovic =?utf-8?q?Court=C3=A8s?= Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Mon, 03 Jan 2022 11:25:04 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 52974 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: 52974@debbugs.gnu.org Cc: Ludovic =?utf-8?q?Court=C3=A8s?= Received: via spool by 52974-submit@debbugs.gnu.org id=B52974.164120910222819 (code B ref 52974); Mon, 03 Jan 2022 11:25:04 +0000 Received: (at 52974) by debbugs.gnu.org; 3 Jan 2022 11:25:02 +0000 Received: from localhost ([127.0.0.1]:34266 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1n4LSX-0005vj-Ni for submit@debbugs.gnu.org; Mon, 03 Jan 2022 06:25:02 -0500 Received: from eggs.gnu.org ([209.51.188.92]:50552) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1n4LSU-0005uX-G8 for 52974@debbugs.gnu.org; Mon, 03 Jan 2022 06:24:58 -0500 Received: from [2001:470:142:3::e] (port=49194 helo=fencepost.gnu.org) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1n4LSP-0001l2-8B; Mon, 03 Jan 2022 06:24:53 -0500 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=wal42SYyj0LkuDwPy6Fz9nJewU+f00OX+CSsDjilEuE=; b=C/rJ1MXCBp4qogJUwMC+ hXUVfAEd19K8W6IgaS05Qo4MUV8Alq/3qznl1CMYGXX6KkvULzhLGti1IGqtDBqfqGM/GNzWl7dxp SDGxBlsbACnv4f8nQXXIkyXpCzo7W68dhpw3UHTJyDWeWaywOAbmf/XVlflxwML82rxS3tyacl3By uxN0V3xGeG9dT13RGUSTA8g+BzMFSS+3VJqdJsBx1LQfCpmq75MNv+c2d8rIrQR7Hnv1gn0gISrYk T7jtUBGbRTcx9LD+J/VYc5i95tsTVKHDRt87Bc77p6K5o5XVtyQSR6hVYGf41SzD5l/6NbElcM7e6 dluFbmCnQvhQBg==; Received: from [2a01:e0a:1d:7270:af76:b9b:ca24:c465] (port=42680 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 1n4LSP-0005n0-Bm; Mon, 03 Jan 2022 06:24:53 -0500 From: Ludovic =?utf-8?q?Court=C3=A8s?= Date: Mon, 3 Jan 2022 12:24:39 +0100 Message-Id: <20220103112439.14377-5-ludo@gnu.org> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20220103112439.14377-1-ludo@gnu.org> References: <20220103112439.14377-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/scripts/style.scm (canonicalize-comment): New procedure. (pretty-print-with-comments): Add #:format-comment. and honor it. (object->string*): Add 'args' and honor them. (format-package-definition): Pass #:format-comment to 'object->string*'. * tests/style.scm ("pretty-print-with-comments, canonicalize-comment"): New test. --- guix/scripts/style.scm | 36 +++++++++++++++++++++++++++++------- tests/style.scm | 25 +++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 7 deletions(-) diff --git a/guix/scripts/style.scm b/guix/scripts/style.scm index 47549c7e4a..3c83265f8d 100644 --- a/guix/scripts/style.scm +++ b/guix/scripts/style.scm @@ -47,6 +47,7 @@ (define-module (guix scripts style) #:use-module (srfi srfi-37) #:export (pretty-print-with-comments read-with-comments + canonicalize-comment guix-style)) @@ -226,8 +227,23 @@ (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 #\;))))) + (comment (string-append + (if (comment-margin? c) + ";" + (if (string-null? line) + ";;" ;no trailing space + ";; ")) + line "\n") + (comment-margin? c)))) + (define* (pretty-print-with-comments port obj #:key + (format-comment identity) (indent 0) (max-width 78) (long-list 5)) @@ -235,7 +251,9 @@ (define* (pretty-print-with-comments port obj and assuming the current column is INDENT. Comments present in OBJ are included in the output. -Lists longer than LONG-LIST are written as one element per line." +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'." (let loop ((indent indent) (column indent) (delimited? #t) ;true if comes after a delimiter @@ -300,14 +318,16 @@ (define (special-form? head) (if (comment-margin? comment) (begin (display " " port) - (display (comment->string comment) port)) + (display (comment->string (format-comment comment)) + port)) (begin ;; When already at the beginning of a line, for example because ;; COMMENT follows a margin comment, no need to emit a newline. (unless (= column indent) (newline port) (display (make-string indent #\space) port)) - (display (comment->string comment) port))) + (display (comment->string (format-comment comment)) + port))) (display (make-string indent #\space) port) indent) (('quote lst) @@ -442,11 +462,12 @@ (define new-column (display str port) (+ column (if delimited? 0 1) len)))))))) -(define (object->string* obj indent) +(define (object->string* obj indent . args) (call-with-output-string (lambda (port) - (pretty-print-with-comments port obj - #:indent indent)))) + (apply pretty-print-with-comments port obj + #:indent indent + args)))) ;;; @@ -706,7 +727,8 @@ (define* (format-package-definition package read-with-comments))) (object->string* exp (location-column - (package-definition-location package))))))) + (package-definition-location package)) + #:format-comment canonicalize-comment))))) (define (package-location