[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[bug#56898] [PATCH 11/13] read-print: Support printing multi-line commen
From: |
Ludovic Courtès |
Subject: |
[bug#56898] [PATCH 11/13] read-print: Support printing multi-line comments. |
Date: |
Tue, 2 Aug 2022 23:44:17 +0200 |
* 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.
---
guix/read-print.scm | 26 ++++++++++++++++++++++++--
tests/read-print.scm | 14 ++++++++++++++
2 files changed, 38 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 @@ (define (canonicalize-comment comment indent)
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 @@ (define (special-form? head)
(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)
diff --git a/tests/read-print.scm b/tests/read-print.scm
index e3f23194af..004fcff19f 100644
--- a/tests/read-print.scm
+++ b/tests/read-print.scm
@@ -341,4 +341,18 @@ (define-module (foo bar)
#:format-vertical-space
canonicalize-vertical-space)))))
+(test-equal "pretty-print-with-comments, multi-line comment"
+ "\
+(list abc
+ ;; This comment spans
+ ;; two lines.
+ def)"
+ (call-with-output-string
+ (lambda (port)
+ (pretty-print-with-comments port
+ `(list abc ,(comment "\
+;; This comment spans\n
+;; two lines.\n")
+ def)))))
+
(test-end)
--
2.37.1
- [bug#56898] [PATCH 00/13] Put the pretty printer to good use, Ludovic Courtès, 2022/08/02
- [bug#56898] [PATCH 01/13] style: Move reader and printer to (guix read-print)., Ludovic Courtès, 2022/08/02
- [bug#56898] [PATCH 02/13] read-print: Add System and Home special forms., Ludovic Courtès, 2022/08/02
- [bug#56898] [PATCH 03/13] read-print: Expose comment constructor., Ludovic Courtès, 2022/08/02
- [bug#56898] [PATCH 04/13] read-print: Introduce <blank> parent class of <comment>., Ludovic Courtès, 2022/08/02
- [bug#56898] [PATCH 07/13] read-print: Recognize page breaks., Ludovic Courtès, 2022/08/02
- [bug#56898] [PATCH 05/13] style: Adjust test to not emit blank lines., Ludovic Courtès, 2022/08/02
- [bug#56898] [PATCH 09/13] read-print: 'canonicalize-comment' leaves top-level comments unchanged., Ludovic Courtès, 2022/08/02
- [bug#56898] [PATCH 13/13] installer: Add comments and vertical space to the generated config., Ludovic Courtès, 2022/08/02
- [bug#56898] [PATCH 11/13] read-print: Support printing multi-line comments.,
Ludovic Courtès <=
- [bug#56898] [PATCH 08/13] read-print: Add code to read and write sequences of expressions/blanks., Ludovic Courtès, 2022/08/02
- [bug#56898] [PATCH 06/13] read-print: Read and render vertical space., Ludovic Courtès, 2022/08/02
- [bug#56898] [PATCH 10/13] style: Add '--whole-file' option., Ludovic Courtès, 2022/08/02
- [bug#56898] [PATCH 12/13] installer: Render the final configuration with (guix read-print)., Ludovic Courtès, 2022/08/02
- [bug#56898] [PATCH 00/13] Put the pretty printer to good use, Mathieu Othacehe, 2022/08/07
- bug#56898: [PATCH 00/13] Put the pretty printer to good use, Ludovic Courtès, 2022/08/09