From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp12.migadu.com ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms0.migadu.com with LMTPS id 8BeHBwbe0mGJ/QAAgWs5BA (envelope-from ) for ; Mon, 03 Jan 2022 12:29:10 +0100 Received: from aspmx1.migadu.com ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp12.migadu.com with LMTPS id z1MZBAbe0mGeJgEAauVa8A (envelope-from ) for ; Mon, 03 Jan 2022 12:29:10 +0100 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by aspmx1.migadu.com (Postfix) with ESMTPS id A1F8F17D81 for ; Mon, 3 Jan 2022 12:29:09 +0100 (CET) Received: from localhost ([::1]:53302 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1n4LWW-0001pn-LX for larch@yhetil.org; Mon, 03 Jan 2022 06:29:08 -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 Content-Transfer-Encoding: 8bit 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+larch=yhetil.org@gnu.org Sender: "Guix-patches" X-Migadu-Flow: FLOW_IN X-Migadu-Country: US ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=yhetil.org; s=key1; t=1641209349; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding:resent-cc: resent-from:resent-sender:resent-message-id:in-reply-to:in-reply-to: references:references:list-id:list-help:list-unsubscribe: list-subscribe:list-post:dkim-signature; bh=kgnUpt6bbMYm5XE6frWguH/faGAWMPvPyvjIYCw2pyQ=; b=PbaMVzRvvEypEiRIacNE9YwXWzba/7Z4urJi9o/XfvK3OrC43l8mh0Xf8z5zW7Ux9jNast b1vU5SYvqqPhjynooP6/kSPFF7mz3QkHDwJH+VRjg1Rn8N4vkCMWApiTLtfSiEWRlx/QIR OfrXMUNbWCrEsYnXn2J5w6gEsNfrIbM9yaWOUVadURKxshec+XSsRCgZeNzy3d0zFzvGDb WSF3I2VxVVjpe31SwxX1ZzP+7H/ZTV1ydyjwTc86ZNTCLmAOq8OxEuJyqk40VVCU0P7f4K 6qT1GnG7hjr5D9lxnGZZxFDn4Vns/zNwgn8B//GI5MUMaUxWKXXfTyhxZADKRg== ARC-Seal: i=1; s=key1; d=yhetil.org; t=1641209349; a=rsa-sha256; cv=none; b=T+uKzcFH0gM4RWiO+XUxcuJH1lv6t1XQBE/334GHsDcJxUinx5DLBsH462XAhyqUtmH/LE TWgTsxx3GC+ouz/z2D9OIn3hBRm8BARdF9LIe/wrMnCcAbzZ1P3cliCOcVm5KyT2Lbt/Zp nmFrpZwOUZaFYXwW1d4lqLEjkTi3BWOlkNDYr1zTjzU2W2TrOvPUKb19/B4vhTsbC/zQ51 CaAKkBEow2TFhAtWABS2LXu4svXFAKrE3N1gHWPliqhH1FnQ9gAKTqyNL1NdXp2cRXXUJm X1ZGxO4BLe+49GiUT8rNB08BoDBBQ3mQdlZDnV+iQPoW+qdqW8JyrZQlNw8e+A== ARC-Authentication-Results: i=1; aspmx1.migadu.com; dkim=fail ("headers rsa verify failed") header.d=gnu.org header.s=fencepost-gnu-org header.b=mj2P1D38; dmarc=pass (policy=none) header.from=gnu.org; spf=pass (aspmx1.migadu.com: domain of "guix-patches-bounces+larch=yhetil.org@gnu.org" designates 209.51.188.17 as permitted sender) smtp.mailfrom="guix-patches-bounces+larch=yhetil.org@gnu.org" X-Migadu-Spam-Score: -3.09 Authentication-Results: aspmx1.migadu.com; dkim=fail ("headers rsa verify failed") header.d=gnu.org header.s=fencepost-gnu-org header.b=mj2P1D38; dmarc=pass (policy=none) header.from=gnu.org; spf=pass (aspmx1.migadu.com: domain of "guix-patches-bounces+larch=yhetil.org@gnu.org" designates 209.51.188.17 as permitted sender) smtp.mailfrom="guix-patches-bounces+larch=yhetil.org@gnu.org" X-Migadu-Queue-Id: A1F8F17D81 X-Spam-Score: -3.09 X-Migadu-Scanner: scn0.migadu.com X-TUID: 7w4fBlWO2CLR 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: -- 2.33.0