From patchwork Mon Jan 3 11:24:36 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: 35915 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 8202C27BBEA; Mon, 3 Jan 2022 11:29:19 +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 CA16E27BBE9 for ; Mon, 3 Jan 2022 11:29:18 +0000 (GMT) Received: from localhost ([::1]:53748 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1n4LWf-00027W-VN for patchwork@mira.cbaines.net; Mon, 03 Jan 2022 06:29:17 -0500 Received: from eggs.gnu.org ([209.51.188.92]:51506) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1n4LSt-0005JF-Go for guix-patches@gnu.org; Mon, 03 Jan 2022 06:25:23 -0500 Received: from debbugs.gnu.org ([209.51.188.43]:50957) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1n4LSa-0001ox-2i 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 1n4LSZ-0005wK-V4 for guix-patches@gnu.org; Mon, 03 Jan 2022 06:25:03 -0500 X-Loop: help-debbugs@gnu.org Subject: [bug#52974] [PATCH 2/5] style: Allow special forms to be scoped. 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:03 +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.164120910022781 (code B ref 52974); Mon, 03 Jan 2022 11:25:03 +0000 Received: (at 52974) by debbugs.gnu.org; 3 Jan 2022 11:25:00 +0000 Received: from localhost ([127.0.0.1]:34264 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1n4LSW-0005vI-7K for submit@debbugs.gnu.org; Mon, 03 Jan 2022 06:25:00 -0500 Received: from eggs.gnu.org ([209.51.188.92]:50526) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1n4LSS-0005uS-IC for 52974@debbugs.gnu.org; Mon, 03 Jan 2022 06:24:57 -0500 Received: from [2001:470:142:3::e] (port=49188 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 1n4LSN-0001kN-A4; Mon, 03 Jan 2022 06:24:51 -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=dbVUc/ju0fBSJUMrMz65uedPu3n6kuRNBg3iqIk4zjk=; b=Mdu2Fp48AZHC0Z26EWYt 9g+BBPRZuNiMcUC0zhy7twf6GqsQG6Zs/eQkngfMGRrkNsC05zen2dREXG2EGn5ekBLXdNWaW8uwV NaDOm+/I59sHXCnYrXHvUQijD40OjbpIe7qIkQAxo+CFMaal4kkvODpyUBRMr9HWdj0h58IVBVOdZ Crt+7Wt3fjs3DMwXK+APAmfWQt6syGZNEb6feKP0cfWFchBFCQyVHaDqB+4eZ0y2FOqws2SMfJyq3 xHrqSMpnjmyT01Cj9NGpccsTiTuD9pMwGZtOm5EGgW99y2ejtDkBy5jFffke3a07vRgTDk8gutEEj NnRe4RhzPnPJPQ==; 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 1n4LSN-0005n0-E1; Mon, 03 Jan 2022 06:24:51 -0500 From: Ludovic =?utf-8?q?Court=C3=A8s?= Date: Mon, 3 Jan 2022 12:24:36 +0100 Message-Id: <20220103112439.14377-2-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 (vhashq): Add clause for 'lst, and change default clause. (%special-forms): Add context for 'add-after and 'add-before. Add 'replace. (prefix?, special-form-lead): New procedures. (special-form?): Remove. (pretty-print-with-comments): Add 'context' to the threaded state. Adjust 'print-sequence' and adjust 'loop' calls accordingly. * tests/style.scm: Add tests for 'replace. --- guix/scripts/style.scm | 88 +++++++++++++++++++++++++++++------------- tests/style.scm | 12 ++++++ 2 files changed, 73 insertions(+), 27 deletions(-) diff --git a/guix/scripts/style.scm b/guix/scripts/style.scm index a5204d02ef..625e942613 100644 --- a/guix/scripts/style.scm +++ b/guix/scripts/style.scm @@ -114,14 +114,19 @@ (define (read-with-comments port) ;;; (define-syntax vhashq - (syntax-rules () + (syntax-rules (quote) ((_) vlist-null) + ((_ (key (quote (lst ...))) rest ...) + (vhash-consq key '(lst ...) (vhashq rest ...))) ((_ (key value) rest ...) - (vhash-consq key value (vhashq rest ...))))) + (vhash-consq key '((() . value)) (vhashq rest ...))))) (define %special-forms ;; Forms that are indented specially. The number is meant to be understood - ;; like Emacs' 'scheme-indent-function' symbol property. + ;; like Emacs' 'scheme-indent-function' symbol property. When given an + ;; alist instead of a number, the alist gives "context" in which the symbol + ;; is a special form; for instance, context (modify-phases) means that the + ;; symbol must appear within a (modify-phases ...) expression. (vhashq ('begin 1) ('lambda 2) @@ -148,9 +153,9 @@ (define %special-forms ('operating-system 1) ('modify-inputs 2) ('modify-phases 2) - ('add-after 3) - ('add-before 3) - ;; ('replace 2) + ('add-after '(((modify-phases) . 3))) + ('add-before '(((modify-phases) . 3))) + ('replace '(((modify-phases) . 2))) ;different from 'modify-inputs' ('substitute* 2) ('substitute-keyword-arguments 2) ('call-with-input-file 2) @@ -158,8 +163,30 @@ (define %special-forms ('with-output-to-file 2) ('with-input-from-file 2))) -(define (special-form? symbol) - (vhash-assq symbol %special-forms)) +(define (prefix? candidate lst) + "Return true if CANDIDATE is a prefix of LST." + (let loop ((candidate candidate) + (lst lst)) + (match candidate + (() #t) + ((head1 . rest1) + (match lst + (() #f) + ((head2 . rest2) + (and (equal? head1 head2) + (loop rest1 rest2)))))))) + +(define (special-form-lead symbol context) + "If SYMBOL is a special form in the given CONTEXT, return its number of +arguments; otherwise return #f. CONTEXT is a stack of symbols lexically +surrounding SYMBOL." + (match (vhash-assq symbol %special-forms) + (#f #f) + ((_ . alist) + (any (match-lambda + ((prefix . level) + (and (prefix? prefix context) (- level 1)))) + alist)))) (define (escaped-string str) "Return STR with backslashes and double quotes escaped. Everything else, in @@ -192,8 +219,9 @@ (define* (pretty-print-with-comments port obj (let loop ((indent indent) (column indent) (delimited? #t) ;true if comes after a delimiter + (context '()) ;list of "parent" symbols (obj obj)) - (define (print-sequence indent column lst delimited?) + (define (print-sequence context indent column lst delimited?) (define long? (> (length lst) long-list)) @@ -223,6 +251,7 @@ (define newline? (comment? item) (loop indent column (or newline? delimited?) + context item))))))) (define (sequence-would-protrude? indent lst) @@ -243,6 +272,9 @@ (define (sequence-would-protrude? indent lst) #f)) lst)) + (define (special-form? head) + (special-form-lead head context)) + (match obj ((? comment? comment) (if (comment-margin? comment) @@ -261,45 +293,46 @@ (define (sequence-would-protrude? indent lst) (('quote lst) (unless delimited? (display " " port)) (display "'" port) - (loop indent (+ column (if delimited? 1 2)) #t lst)) + (loop indent (+ column (if delimited? 1 2)) #t context lst)) (('quasiquote lst) (unless delimited? (display " " port)) (display "`" port) - (loop indent (+ column (if delimited? 1 2)) #t lst)) + (loop indent (+ column (if delimited? 1 2)) #t context lst)) (('unquote lst) (unless delimited? (display " " port)) (display "," port) - (loop indent (+ column (if delimited? 1 2)) #t lst)) + (loop indent (+ column (if delimited? 1 2)) #t context lst)) (('unquote-splicing lst) (unless delimited? (display " " port)) (display ",@" port) - (loop indent (+ column (if delimited? 2 3)) #t lst)) + (loop indent (+ column (if delimited? 2 3)) #t context lst)) (('gexp lst) (unless delimited? (display " " port)) (display "#~" port) - (loop indent (+ column (if delimited? 2 3)) #t lst)) + (loop indent (+ column (if delimited? 2 3)) #t context lst)) (('ungexp obj) (unless delimited? (display " " port)) (display "#$" port) - (loop indent (+ column (if delimited? 2 3)) #t obj)) + (loop indent (+ column (if delimited? 2 3)) #t context obj)) (('ungexp-native obj) (unless delimited? (display " " port)) (display "#+" port) - (loop indent (+ column (if delimited? 2 3)) #t obj)) + (loop indent (+ column (if delimited? 2 3)) #t context obj)) (('ungexp-splicing lst) (unless delimited? (display " " port)) (display "#$@" port) - (loop indent (+ column (if delimited? 3 4)) #t lst)) + (loop indent (+ column (if delimited? 3 4)) #t context lst)) (('ungexp-native-splicing lst) (unless delimited? (display " " port)) (display "#+@" port) - (loop indent (+ column (if delimited? 3 4)) #t lst)) + (loop indent (+ column (if delimited? 3 4)) #t context lst)) (((? special-form? head) arguments ...) ;; Special-case 'let', 'lambda', 'modify-inputs', etc. so the second ;; and following arguments are less indented. - (let* ((lead (- (cdr (vhash-assq head %special-forms)) 1)) - (head (symbol->string head)) - (total (length arguments))) + (let* ((lead (special-form-lead head context)) + (context (cons head context)) + (head (symbol->string head)) + (total (length arguments))) (unless delimited? (display " " port)) (display "(" port) (display head port) @@ -327,14 +360,14 @@ (define new-column (() column) ((head . tail) (inner (- n 1) tail - (loop initial-indent - column + (loop initial-indent column (= n lead) + context head))))))) ;; Print the remaining arguments. (let ((column (print-sequence - indent new-column + context indent new-column (drop arguments (min lead total)) #t))) (display ")" port) @@ -343,14 +376,15 @@ (define new-column (let* ((overflow? (>= column max-width)) (column (if overflow? (+ indent 1) - (+ column (if delimited? 1 2))))) + (+ column (if delimited? 1 2)))) + (context (cons head context))) (if overflow? (begin (newline port) (display (make-string indent #\space) port)) (unless delimited? (display " " port))) (display "(" port) - (let* ((new-column (loop column column #t head)) + (let* ((new-column (loop column column #t context head)) (indent (if (or (>= new-column max-width) (not (symbol? head)) (sequence-would-protrude? @@ -358,7 +392,7 @@ (define new-column column (+ new-column 1)))) (define column - (print-sequence indent new-column tail #f)) + (print-sequence context indent new-column tail #f)) (display ")" port) (+ column 1)))) (_ diff --git a/tests/style.scm b/tests/style.scm index d9e8d803f4..6c449cb72e 100644 --- a/tests/style.scm +++ b/tests/style.scm @@ -453,6 +453,18 @@ (define file \"abcdefghijklmnopqrstuvwxyz\")" #:max-width 33) +(test-pretty-print "\ +(modify-phases %standard-phases + (replace 'build + ;; Nicely indented in 'modify-phases' context. + (lambda _ + #t)))") + +(test-pretty-print "\ +(modify-inputs inputs + ;; Regular indentation for 'replace' here. + (replace \"gmp\" gmp))") + (test-end) ;; Local Variables: From patchwork Mon Jan 3 11:24:37 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: 35914 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 5F72127BBEA; Mon, 3 Jan 2022 11:29:12 +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 D9B3927BBE9 for ; Mon, 3 Jan 2022 11:29:11 +0000 (GMT) Received: from localhost ([::1]:53302 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1n4LWY-0001pn-WE for patchwork@mira.cbaines.net; Mon, 03 Jan 2022 06:29:11 -0500 Received: from eggs.gnu.org ([209.51.188.92]:51440) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1n4LSs-0005Iq-PH for guix-patches@gnu.org; Mon, 03 Jan 2022 06:25:22 -0500 Received: from debbugs.gnu.org ([209.51.188.43]:50956) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1n4LSZ-0001om-Fu for guix-patches@gnu.org; Mon, 03 Jan 2022 06:25:15 -0500 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1n4LSZ-0005wC-CK for guix-patches@gnu.org; Mon, 03 Jan 2022 06:25:03 -0500 X-Loop: help-debbugs@gnu.org Subject: [bug#52974] [PATCH 3/5] style: Add support for "newline forms". 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:03 +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.164120910022775 (code B ref 52974); Mon, 03 Jan 2022 11:25:03 +0000 Received: (at 52974) by debbugs.gnu.org; 3 Jan 2022 11:25:00 +0000 Received: from localhost ([127.0.0.1]:34262 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1n4LSV-0005vA-Q1 for submit@debbugs.gnu.org; Mon, 03 Jan 2022 06:25:00 -0500 Received: from eggs.gnu.org ([209.51.188.92]:50542) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1n4LST-0005uT-88 for 52974@debbugs.gnu.org; Mon, 03 Jan 2022 06:24:57 -0500 Received: from [2001:470:142:3::e] (port=49190 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 1n4LSO-0001kb-0d; Mon, 03 Jan 2022 06:24:52 -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=kgnUpt6bbMYm5XE6frWguH/faGAWMPvPyvjIYCw2pyQ=; b=mj2P1D387mHHQdSmlWZE 7ADvm41xgpfL3oZM9xVzx9Luk+ec6cWfAzCQ4KdXd0hvJP8IjAJtXhOmHopVHYPquBgKVo9cZbYfo PhfhpVjCtZKijay/TG8HklhOeCmOauREQXoZfz7y+9YxcY7JZpplD+lUeZKxoVy9jeeDs1PkuyowE 1iHF35h6ygfLohow23sAXorc8LPBIrhBeECUcZhZeukT1gPv2R5yAmZgTUhUWRDAeNtQBQ+n0EQKf 3Cc2MJjdSwx/4ipyH8eYdBrbynyJxWTBM/CC1AX41AU+OryMRXKAJjKzBff9SdaZR/1+7bpJdOHVs ZbNW0y4gV539nw==; 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 1n4LSO-0005n0-1i; Mon, 03 Jan 2022 06:24:52 -0500 From: Ludovic =?utf-8?q?Court=C3=A8s?= Date: Mon, 3 Jan 2022 12:24:37 +0100 Message-Id: <20220103112439.14377-3-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 This allows us to express cases where a newline should be inserted immediately after the head symbol of a list. * guix/scripts/style.scm (%newline-forms): New variable. (newline-form?): New procedure. (pretty-print-with-comments): Handle "newline forms". * tests/style.scm: Add test. --- guix/scripts/style.scm | 40 +++++++++++++++++++++++++++++++++++----- tests/style.scm | 13 +++++++++++++ 2 files changed, 48 insertions(+), 5 deletions(-) diff --git a/guix/scripts/style.scm b/guix/scripts/style.scm index 625e942613..00680daa23 100644 --- a/guix/scripts/style.scm +++ b/guix/scripts/style.scm @@ -163,6 +163,18 @@ (define %special-forms ('with-output-to-file 2) ('with-input-from-file 2))) +(define %newline-forms + ;; List heads that must be followed by a newline. The second argument is + ;; the context in which they must appear. This is similar to a special form + ;; of 1, except that indent is 1 instead of 2 columns. + (vhashq + ('arguments '(package)) + ('sha256 '(origin source package)) + ('base32 '(sha256 origin)) + ('search-paths '(package)) + ('native-search-paths '(package)) + ('search-path-specification '()))) + (define (prefix? candidate lst) "Return true if CANDIDATE is a prefix of LST." (let loop ((candidate candidate) @@ -188,6 +200,14 @@ (define (special-form-lead symbol context) (and (prefix? prefix context) (- level 1)))) alist)))) +(define (newline-form? symbol context) + "Return true if parenthesized expressions starting with SYMBOL must be +followed by a newline." + (match (vhash-assq symbol %newline-forms) + (#f #f) + ((_ . prefix) + (prefix? prefix context)))) + (define (escaped-string str) "Return STR with backslashes and double quotes escaped. Everything else, in particular newlines, is left as is." @@ -377,6 +397,7 @@ (define new-column (column (if overflow? (+ indent 1) (+ column (if delimited? 1 2)))) + (newline? (newline-form? head context)) (context (cons head context))) (if overflow? (begin @@ -384,17 +405,26 @@ (define new-column (display (make-string indent #\space) port)) (unless delimited? (display " " port))) (display "(" port) + (let* ((new-column (loop column column #t context head)) (indent (if (or (>= new-column max-width) (not (symbol? head)) (sequence-would-protrude? - (+ new-column 1) tail)) + (+ new-column 1) tail) + newline?) column (+ new-column 1)))) - (define column - (print-sequence context indent new-column tail #f)) - (display ")" port) - (+ column 1)))) + (when newline? + ;; Insert a newline right after HEAD. + (newline port) + (display (make-string indent #\space) port)) + + (let ((column + (print-sequence context indent + (if newline? indent new-column) + tail newline?))) + (display ")" port) + (+ column 1))))) (_ (let* ((str (if (string? obj) (escaped-string obj) diff --git a/tests/style.scm b/tests/style.scm index 6c449cb72e..8022688419 100644 --- a/tests/style.scm +++ b/tests/style.scm @@ -465,6 +465,19 @@ (define file ;; Regular indentation for 'replace' here. (replace \"gmp\" gmp))") +(test-pretty-print "\ +(package + ;; Here 'sha256', 'base32', and 'arguments' must be + ;; immediately followed by a newline. + (source (origin + (method url-fetch) + (sha256 + (base32 + \"not a real base32 string\")))) + (arguments + '(#:phases %standard-phases + #:tests? #f)))") + (test-end) ;; Local Variables: From patchwork Mon Jan 3 11:24:38 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Ludovic_Court=C3=A8s?= X-Patchwork-Id: 35913 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 218B527BBEA; Mon, 3 Jan 2022 11:26:59 +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 AFDEF27BBE9 for ; Mon, 3 Jan 2022 11:26:56 +0000 (GMT) Received: from localhost ([::1]:47072 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1n4LU3-0005rA-To for patchwork@mira.cbaines.net; Mon, 03 Jan 2022 06:26:39 -0500 Received: from eggs.gnu.org ([209.51.188.92]:51522) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1n4LSt-0005JJ-Gq for guix-patches@gnu.org; Mon, 03 Jan 2022 06:25:23 -0500 Received: from debbugs.gnu.org ([209.51.188.43]:50959) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1n4LSb-0001pI-CV 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 1n4LSb-0005wz-9i for guix-patches@gnu.org; Mon, 03 Jan 2022 06:25:05 -0500 X-Loop: help-debbugs@gnu.org Subject: [bug#52974] [PATCH 4/5] style: Add '--styling' option. 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:05 +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.164120910422850 (code B ref 52974); Mon, 03 Jan 2022 11:25:05 +0000 Received: (at 52974) by debbugs.gnu.org; 3 Jan 2022 11:25:04 +0000 Received: from localhost ([127.0.0.1]:34268 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1n4LSY-0005vy-Fa for submit@debbugs.gnu.org; Mon, 03 Jan 2022 06:25:04 -0500 Received: from eggs.gnu.org ([209.51.188.92]:50548) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1n4LST-0005uW-SV for 52974@debbugs.gnu.org; Mon, 03 Jan 2022 06:24:58 -0500 Received: from [2001:470:142:3::e] (port=49192 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 1n4LSO-0001kq-KA; Mon, 03 Jan 2022 06:24:52 -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=7my62j56F3RyE2kVG1/jXIksbfi535hcf8Gd/4jpKqM=; b=YWvBx0i+NuTFd45dUosn as+9AX5cKecoq+5uC14gf0IoDe4AduK9PoNFaf8tmishh3xrDZoN/spH+XvbXiQ697JbHaexyqfdY HOtZgWIv8ShtdlDN8jP53NLkZS6kiRjJkDRilH8F+gn4ZofKX06cKGXBWs5LwRTAgbW/YVrFSwJeg 6qEo3NKDcmYaU7Agf8RgdlNfciurRemilNxTdauxlFyZo4aQCZ1+73uXeqgDedQ4nYZZBGEEqTzgS vccfR64YdYUzWs4gID61oea+JZtnznNjjThmeMHXxL5iSOwIchcHRqR4iOeeMeopJlp+Ud28Zr/U1 4o8c6UNVFmGI0w==; 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 1n4LSO-0005n0-LL; Mon, 03 Jan 2022 06:24:52 -0500 From: Ludovic =?utf-8?q?Court=C3=A8s?= Date: Mon, 3 Jan 2022 12:24:38 +0100 Message-Id: <20220103112439.14377-4-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 (format-package-definition): New procedure. (%options, show-help): Add "--styling". (%default-options): Add 'styling-procedure'. (guix-style): Honor it. * tests/style.scm (with-test-package) ("input labels, 'safe' policy") ("input labels, 'safe' policy, nothing changed") ("input labels, margin comment") ("input labels, margin comment on long list") ("input labels, line comment") ("input labels, modify-inputs and margin comment"): Pass "-S inputs". * etc/indent-code.el: Remove. * doc/contributing.texi (Formatting Code): Mention "guix style" instead of "etc/indent-code.el". (Submitting Patches): Add item for "guix style". * doc/guix.texi (Invoking guix style): Document "-S" and update. --- doc/contributing.texi | 18 +++---- doc/guix.texi | 60 +++++++++++++++++---- etc/indent-code.el | 120 ----------------------------------------- guix/scripts/style.scm | 43 +++++++++++++-- tests/style.scm | 17 ++++-- 5 files changed, 111 insertions(+), 147 deletions(-) delete mode 100755 etc/indent-code.el diff --git a/doc/contributing.texi b/doc/contributing.texi index 72f5ce1e0e..9f97788c0b 100644 --- a/doc/contributing.texi +++ b/doc/contributing.texi @@ -959,17 +959,11 @@ If you do not use Emacs, please make sure to let your editor knows these rules. To automatically indent a package definition, you can also run: @example -./etc/indent-code.el gnu/packages/@var{file}.scm @var{package} +./pre-inst-env guix style @var{package} @end example @noindent -This automatically indents the definition of @var{package} in -@file{gnu/packages/@var{file}.scm} by running Emacs in batch mode. To -indent a whole file, omit the second argument: - -@example -./etc/indent-code.el gnu/services/@var{file}.scm -@end example +@xref{Invoking guix style}, for more information. @cindex Vim, Scheme code editing If you are editing code with Vim, we recommend that you run @code{:set @@ -1038,6 +1032,10 @@ Run @code{guix lint @var{package}}, where @var{package} is the name of the new or modified package, and fix any errors it reports (@pxref{Invoking guix lint}). +@item +Run @code{guix style @var{package}} to format the new package definition +according to the project's conventions (@pxref{Invoking guix style}). + @item Make sure the package builds on your platform, using @code{guix build @var{package}}. @@ -1175,8 +1173,8 @@ Examples of unrelated changes include the addition of several packages, or a package update along with fixes to that package. @item -Please follow our code formatting rules, possibly running the -@command{etc/indent-code.el} script to do that automatically for you +Please follow our code formatting rules, possibly running +@command{guix style} script to do that automatically for you (@pxref{Formatting Code}). @item diff --git a/doc/guix.texi b/doc/guix.texi index b72a3d1263..01d4c14e9c 100644 --- a/doc/guix.texi +++ b/doc/guix.texi @@ -12771,8 +12771,16 @@ otherwise. The @command{guix style} command helps packagers style their package definitions according to the latest fashionable trends. The command -currently focuses on one aspect: the style of package inputs. It may -eventually be extended to handle other stylistic matters. +currently provides the providing styling rules: + +@itemize +@item +formatting package definitions according to the project's conventions +(@pxref{Formatting Code}); + +@item +rewriting package inputs to the ``new style'', as explained below. +@end itemize The way package inputs are written is going through a transition (@pxref{package Reference}, for more on package inputs). Until version @@ -12803,7 +12811,7 @@ Package Variants}, for more info on @code{modify-inputs}). In the vast majority of cases, this is a purely mechanical change on the surface syntax that does not even incur a package rebuild. Running -@command{guix style} can do that for you, whether you're working on +@command{guix style -S inputs} can do that for you, whether you're working on packages in Guix proper or in an external channel. The general syntax is: @@ -12813,15 +12821,48 @@ guix style [@var{options}] @var{package}@dots{} @end example This causes @command{guix style} to analyze and rewrite the definition -of @var{package}@dots{}. It does so in a conservative way: preserving -comments and bailing out if it cannot make sense of the code that -appears in an inputs field. The available options are listed below. +of @var{package}@dots{} or, when @var{package} is omitted, of @emph{all} +the packages. The @option{--styling} or @option{-S} option allows you +to select the style rule, the default rule being @code{format}---see +below. + +The available options are listed below. @table @code @item --dry-run @itemx -n Show source file locations that would be edited but do not modify them. +@item --styling=@var{rule} +@itemx -S @var{rule} +Apply @var{rule}, one of the following styling rules: + +@table @code +@item format +Format the given package definition(s)---this is the default styling +rule. For example, a packager running Guix on a checkout +(@pxref{Running Guix Before It Is Installed}) might want to reformat the +definition of the Coreutils package like so: + +@example +./pre-inst-env guix style coreutils +@end example + +@item inputs +Rewrite package inputs to the ``new style'', as described above. This +is how you would rewrite inputs of package @code{whatnot} in your own +channel: + +@example +guix style -L ~/my/channel -S inputs whatnot +@end example + +Rewriting is done in a conservative way: preserving comments and bailing +out if it cannot make sense of the code that appears in an inputs field. +The @option{--input-simplification} option described below provides +fine-grain control over when inputs should be simplified. +@end table + @item --load-path=@var{directory} @itemx -L @var{directory} Add @var{directory} to the front of the package module search path @@ -12840,9 +12881,10 @@ guix style -e '(@@ (gnu packages gcc) gcc-5)' styles the @code{gcc-5} package definition. @item --input-simplification=@var{policy} -Specify the package input simplification policy for cases where an input -label does not match the corresponding package name. @var{policy} may -be one of the following: +When using the @code{inputs} styling rule, with @samp{-S inputs}, this +option specifies the package input simplification policy for cases where +an input label does not match the corresponding package name. +@var{policy} may be one of the following: @table @code @item silent diff --git a/etc/indent-code.el b/etc/indent-code.el deleted file mode 100755 index bdea8ee8bf..0000000000 --- a/etc/indent-code.el +++ /dev/null @@ -1,120 +0,0 @@ -:;exec emacs --batch --quick --load="$0" --funcall=main "$@" -;;; indent-code.el --- Run Emacs to indent a package definition. - -;; Copyright © 2017 Alex Kost -;; Copyright © 2017 Ludovic Courtès -;; Copyright © 2020 Maxim Cournoyer -;; Copyright © 2020 Tobias Geerinckx-Rice - -;; This file is part of GNU Guix. - -;; GNU Guix is free software; you can redistribute it and/or modify -;; it under the terms of the GNU General Public License as published by -;; the Free Software Foundation, either version 3 of the License, or -;; (at your option) any later version. - -;; GNU Guix is distributed in the hope that it will be useful, -;; but WITHOUT ANY WARRANTY; without even the implied warranty of -;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -;; GNU General Public License for more details. - -;; You should have received a copy of the GNU General Public License -;; along with this program. If not, see . - -;;; Commentary: - -;; This scripts indents the given file or package definition in the specified -;; file using Emacs. - -;;; Code: - -;; Load Scheme indentation rules from ".dir-locals.el". -(with-temp-buffer - (scheme-mode) - (let ((default-directory (file-name-as-directory load-file-name)) - (enable-local-variables :all)) - (hack-dir-local-variables) - (hack-local-variables-apply))) - -;; Add indentation info for Scheme constructs that are not Guix-specific. -;; This is normally provided by Geiser but this file is for people who may not -;; be running Geiser, so we just copy it here (from 'geiser-syntax.el'). -(defmacro guix-syntax--scheme-indent (&rest pairs) - `(progn ,@(mapcar (lambda (p) - `(put ',(car p) 'scheme-indent-function ',(cadr p))) - pairs))) - -(guix-syntax--scheme-indent - (and-let* 1) - (case-lambda 0) - (catch defun) - (class defun) - (dynamic-wind 0) - (guard 1) - (let*-values 1) - (let-values 1) - (let/ec 1) - (letrec* 1) - (match 1) - (match-lambda 0) - (match-lambda* 0) - (match-let scheme-let-indent) - (match-let* 1) - (match-letrec 1) - (opt-lambda 1) - (parameterize 1) - (parameterize* 1) - (receive 2) - (require-extension 0) - (syntax-case 2) - (test-approximate 1) - (test-assert 1) - (test-eq 1) - (test-equal 1) - (test-eqv 1) - (test-group-with-cleanup 1) - (test-runner-on-bad-count! 1) - (test-runner-on-bad-end-name! 1) - (test-runner-on-final! 1) - (test-runner-on-group-begin! 1) - (test-runner-on-group-end! 1) - (test-runner-on-test-begin! 1) - (test-runner-on-test-end! 1) - (test-with-runner 1) - (unless 1) - (when 1) - (while 1) - (with-exception-handler 1) - (with-syntax 1)) - - -(defun main () - (pcase command-line-args-left - (`(,file-name ,package-name) - ;; Indent the definition of PACKAGE-NAME in FILE-NAME. - (find-file file-name) - (goto-char (point-min)) - (if (re-search-forward (concat "^(define\\(\\|-public\\) +" - package-name) - nil t) - (let ((indent-tabs-mode nil)) - (beginning-of-defun) - (mark-sexp) - (untabify (point) (mark)) - (indent-sexp) - (save-buffer) - (message "Done!")) - (error "Package '%s' not found in '%s'" - package-name file-name))) - (`(,file-name) - ;; Indent all of FILE-NAME. - (find-file file-name) - (let ((indent-tabs-mode nil)) - (untabify (point-min) (point-max)) - (indent-region (point-min) (point-max)) - (save-buffer) - (message "Done!"))) - (x - (error "Usage: indent-code.el FILE [PACKAGE]")))) - -;;; indent-code.el ends here diff --git a/guix/scripts/style.scm b/guix/scripts/style.scm index 00680daa23..47549c7e4a 100644 --- a/guix/scripts/style.scm +++ b/guix/scripts/style.scm @@ -685,6 +685,29 @@ (define matches? (list package-inputs package-native-inputs package-propagated-inputs))) + +;;; +;;; Formatting package definitions. +;;; + +(define* (format-package-definition package + #:key policy + (edit-expression edit-expression)) + "Reformat the definition of PACKAGE." + (unless (package-definition-location package) + (leave (package-location package) + (G_ "no definition location for package ~a~%") + (package-full-name package))) + + (edit-expression + (location->source-properties (package-definition-location package)) + (lambda (str) + (let ((exp (call-with-input-string str + read-with-comments))) + (object->string* exp + (location-column + (package-definition-location package))))))) + (define (package-locationsymbol arg))) @@ -731,6 +763,9 @@ (define %options (define (show-help) (display (G_ "Usage: guix style [OPTION]... [PACKAGE]... Update package definitions to the latest style.\n")) + (display (G_ " + -S, --styling=RULE apply RULE, a styling rule")) + (newline) (display (G_ " -n, --dry-run display files that would be edited but do nothing")) (display (G_ " @@ -751,7 +786,8 @@ (define (show-help) (define %default-options ;; Alist of default option values. - '((input-simplification-policy . silent))) + `((input-simplification-policy . silent) + (styling-procedure . ,format-package-definition))) ;;; @@ -778,11 +814,12 @@ (define (parse-options) (edit (if (assoc-ref opts 'dry-run?) edit-expression/dry-run edit-expression)) + (style (assoc-ref opts 'styling-procedure)) (policy (assoc-ref opts 'input-simplification-policy))) (with-error-handling (for-each (lambda (package) - (simplify-package-inputs package #:policy policy - #:edit-expression edit)) + (style package #:policy policy + #:edit-expression edit)) ;; Sort package by source code location so that we start editing ;; files from the bottom and going upward. That way, the ;; 'location' field of records is not invalidated as diff --git a/tests/style.scm b/tests/style.scm index 8022688419..7dae543860 100644 --- a/tests/style.scm +++ b/tests/style.scm @@ -78,7 +78,8 @@ (define file (string-append directory "/my-packages.scm")) ;; Run as a separate process to make sure FILE is reloaded. - (system* "guix" "style" "-L" directory "my-coreutils") + (system* "guix" "style" "-L" directory "-S" "inputs" + "my-coreutils") (system* "cat" file) (load file) @@ -237,6 +238,7 @@ (define file (string-append directory "/my-packages.scm")) (system* "guix" "style" "-L" directory "my-coreutils" + "-S" "inputs" "--input-simplification=safe") (load file) @@ -258,6 +260,7 @@ (define file (string-append directory "/my-packages.scm")) (system* "guix" "style" "-L" directory "my-coreutils" + "-S" "inputs" "--input-simplification=safe") (load file) @@ -284,7 +287,8 @@ (define file " ;another one\n"))) (system* "cat" file) - (system* "guix" "style" "-L" directory "my-coreutils") + (system* "guix" "style" "-L" directory "-S" "inputs" + "my-coreutils") (load file) (list (package-inputs (@ (my-packages) my-coreutils)) @@ -317,7 +321,8 @@ (define file " ;margin comment\n"))) (system* "cat" file) - (system* "guix" "style" "-L" directory "my-coreutils") + (system* "guix" "style" "-L" directory "-S" "inputs" + "my-coreutils") (load file) (list (package-inputs (@ (my-packages) my-coreutils)) @@ -338,7 +343,8 @@ (define file ((",gmp\\)(.*)$" _ rest) (string-append ",gmp)\n ;; line comment!\n" rest))) - (system* "guix" "style" "-L" directory "my-coreutils") + (system* "guix" "style" "-L" directory "-S" "inputs" + "my-coreutils") (load file) (list (package-inputs (@ (my-packages) my-coreutils)) @@ -364,7 +370,8 @@ (define file ((",acl\\)(.*)$" _ rest) (string-append ",acl) ;another one\n" rest))) - (system* "guix" "style" "-L" directory "my-coreutils") + (system* "guix" "style" "-L" directory "-S" "inputs" + "my-coreutils") (load file) (list (package-inputs (@ (my-packages) my-coreutils)) 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