summary refs log tree commit diff
path: root/guix/read-print.scm
diff options
context:
space:
mode:
authorLudovic Courtès <ludo@gnu.org>2022-08-02 22:52:10 +0200
committerLudovic Courtès <ludo@gnu.org>2022-08-08 11:54:24 +0200
commit445a0d134ce735577d1e22f210c2e5ceafc56762 (patch)
treeef4d0a5c97465ad1e0ef50f8b7dcf620b988294a /guix/read-print.scm
parenta109ee90486981242beb4272142d429cd0d2e3c3 (diff)
downloadguix-445a0d134ce735577d1e22f210c2e5ceafc56762.tar.gz
read-print: Support printing multi-line comments.
* guix/read-print.scm (%not-newline): New variable.
(print-multi-line-comment): New procedure.
(pretty-print-with-comments): Use it.
* tests/read-print.scm ("pretty-print-with-comments, multi-line
comment"): New test.
Diffstat (limited to 'guix/read-print.scm')
-rw-r--r--guix/read-print.scm26
1 files changed, 24 insertions, 2 deletions
diff --git a/guix/read-print.scm b/guix/read-print.scm
index 2fc3d85a25..df25eb0f50 100644
--- a/guix/read-print.scm
+++ b/guix/read-print.scm
@@ -387,6 +387,27 @@ particular newlines, is left as is."
                           line "\n")
                          (comment-margin? comment)))))
 
+(define %not-newline
+  (char-set-complement (char-set #\newline)))
+
+(define (print-multi-line-comment str indent port)
+  "Print to PORT STR as a multi-line comment, with INDENT spaces preceding
+each line except the first one (they're assumed to be already there)."
+
+  ;; While 'read-with-comments' only returns one-line comments, user-provided
+  ;; comments might span multiple lines, which is why this is necessary.
+  (let loop ((lst (string-tokenize str %not-newline)))
+    (match lst
+      (() #t)
+      ((last)
+       (display last port)
+       (newline port))
+      ((head tail ...)
+       (display head port)
+       (newline port)
+       (display (make-string indent #\space) port)
+       (loop tail)))))
+
 (define* (pretty-print-with-comments port obj
                                      #:key
                                      (format-comment
@@ -486,8 +507,9 @@ FORMAT-VERTICAL-SPACE; a useful value of 'canonicalize-vertical-space'."
              (unless (= column indent)
                (newline port)
                (display (make-string indent #\space) port))
-             (display (comment->string (format-comment comment indent))
-                      port)))
+             (print-multi-line-comment (comment->string
+                                        (format-comment comment indent))
+                                       indent port)))
        (display (make-string indent #\space) port)
        indent)
       ((? vertical-space? space)