unofficial mirror of guix-patches@gnu.org 
 help / color / mirror / code / Atom feed
From: "Ludovic Courtès" <ludo@gnu.org>
To: 52974@debbugs.gnu.org
Cc: "Ludovic Courtès" <ludo@gnu.org>
Subject: [bug#52974] [PATCH 5/5] style: '-S format' canonicalizes comments.
Date: Mon,  3 Jan 2022 12:24:39 +0100	[thread overview]
Message-ID: <20220103112439.14377-5-ludo@gnu.org> (raw)
In-Reply-To: <20220103112439.14377-1-ludo@gnu.org>

* 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))))
 
 \f
 ;;;
@@ -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<? p1 p2)
   "Return true if P1's location is \"before\" P2's."
diff --git a/tests/style.scm b/tests/style.scm
index 7dae543860..8c6d37a661 100644
--- a/tests/style.scm
+++ b/tests/style.scm
@@ -485,6 +485,31 @@ (define file
    '(#:phases %standard-phases
      #:tests? #f)))")
 
+(test-equal "pretty-print-with-comments, canonicalize-comment"
+  "\
+(list abc
+      ;; Not a margin comment.
+      ;; Ditto.
+      ;;
+      ;; There's a blank line above.
+      def ;margin comment
+      ghi)"
+  (let ((sexp (call-with-input-string
+                  "\
+(list abc
+  ;Not a margin comment.
+  ;;;  Ditto.
+  ;;;;;
+  ; There's a blank line above.
+  def  ;; margin comment
+  ghi)"
+                read-with-comments)))
+    (call-with-output-string
+      (lambda (port)
+        (pretty-print-with-comments port sexp
+                                    #:format-comment
+                                    canonicalize-comment)))))
+
 (test-end)
 
 ;; Local Variables:
-- 
2.33.0





  parent reply	other threads:[~2022-01-03 11:25 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-01-03 10:53 [bug#52974] [PATCH 0/5] Formatting package definitions with 'guix style' Ludovic Courtès
2022-01-03 11:24 ` [bug#52974] [PATCH 1/5] style: Improve pretty printer and add tests Ludovic Courtès
2022-01-03 11:24   ` [bug#52974] [PATCH 2/5] style: Allow special forms to be scoped Ludovic Courtès
2022-01-03 11:24   ` [bug#52974] [PATCH 3/5] style: Add support for "newline forms" Ludovic Courtès
2022-01-03 11:24   ` [bug#52974] [PATCH 4/5] style: Add '--styling' option Ludovic Courtès
2022-01-03 11:24   ` Ludovic Courtès [this message]
2022-01-10 14:03 ` bug#52974: [PATCH 0/5] Formatting package definitions with 'guix style' Ludovic Courtès

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://guix.gnu.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220103112439.14377-5-ludo@gnu.org \
    --to=ludo@gnu.org \
    --cc=52974@debbugs.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/guix.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).