mirror of
https://git.in.rschanz.org/ryan77627/guix.git
synced 2024-12-24 21:38:07 -05:00
read-print: 'canonicalize-comment' leaves top-level comments unchanged.
This lets users use three leading semicolons, for instance, in top-level comments. * guix/read-print.scm (canonicalize-comment): Add INDENT parameter and honor it. (pretty-print-with-comments): Change default value of #:format-comment. Call FORMAT-COMMENT with INDENT as the second argument. * tests/read-print.scm: Adjust test accordingly.
This commit is contained in:
parent
9b00c97de4
commit
90ef692e9b
2 changed files with 22 additions and 17 deletions
|
@ -371,23 +371,26 @@ (define (string-width str)
|
||||||
"Return the \"width\" of STR--i.e., the width of the longest line of 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))))
|
(apply max (map string-length (string-split str #\newline))))
|
||||||
|
|
||||||
(define (canonicalize-comment c)
|
(define (canonicalize-comment comment indent)
|
||||||
"Canonicalize comment C, ensuring it has the \"right\" number of leading
|
"Canonicalize COMMENT, which is to be printed at INDENT, ensuring it has the
|
||||||
semicolons."
|
\"right\" number of leading semicolons."
|
||||||
(let ((line (string-trim-both
|
(if (zero? indent)
|
||||||
(string-trim (comment->string c) (char-set #\;)))))
|
comment ;leave top-level comments unchanged
|
||||||
(string->comment (string-append
|
(let ((line (string-trim-both
|
||||||
(if (comment-margin? c)
|
(string-trim (comment->string comment) (char-set #\;)))))
|
||||||
";"
|
(string->comment (string-append
|
||||||
(if (string-null? line)
|
(if (comment-margin? comment)
|
||||||
";;" ;no trailing space
|
";"
|
||||||
";; "))
|
(if (string-null? line)
|
||||||
line "\n")
|
";;" ;no trailing space
|
||||||
(comment-margin? c))))
|
";; "))
|
||||||
|
line "\n")
|
||||||
|
(comment-margin? comment)))))
|
||||||
|
|
||||||
(define* (pretty-print-with-comments port obj
|
(define* (pretty-print-with-comments port obj
|
||||||
#:key
|
#:key
|
||||||
(format-comment identity)
|
(format-comment
|
||||||
|
(lambda (comment indent) comment))
|
||||||
(format-vertical-space identity)
|
(format-vertical-space identity)
|
||||||
(indent 0)
|
(indent 0)
|
||||||
(max-width 78)
|
(max-width 78)
|
||||||
|
@ -475,7 +478,7 @@ (define (special-form? head)
|
||||||
(if (comment-margin? comment)
|
(if (comment-margin? comment)
|
||||||
(begin
|
(begin
|
||||||
(display " " port)
|
(display " " port)
|
||||||
(display (comment->string (format-comment comment))
|
(display (comment->string (format-comment comment indent))
|
||||||
port))
|
port))
|
||||||
(begin
|
(begin
|
||||||
;; When already at the beginning of a line, for example because
|
;; When already at the beginning of a line, for example because
|
||||||
|
@ -483,7 +486,7 @@ (define (special-form? head)
|
||||||
(unless (= column indent)
|
(unless (= column indent)
|
||||||
(newline port)
|
(newline port)
|
||||||
(display (make-string indent #\space) port))
|
(display (make-string indent #\space) port))
|
||||||
(display (comment->string (format-comment comment))
|
(display (comment->string (format-comment comment indent))
|
||||||
port)))
|
port)))
|
||||||
(display (make-string indent #\space) port)
|
(display (make-string indent #\space) port)
|
||||||
indent)
|
indent)
|
||||||
|
|
|
@ -274,6 +274,7 @@ (define-syntax-rule (test-pretty-print/sequence str args ...)
|
||||||
|
|
||||||
(test-pretty-print/sequence "
|
(test-pretty-print/sequence "
|
||||||
;;; Hello!
|
;;; Hello!
|
||||||
|
;;; Notice that there are three semicolons here.
|
||||||
|
|
||||||
(define-module (foo bar)
|
(define-module (foo bar)
|
||||||
#:use-module (guix)
|
#:use-module (guix)
|
||||||
|
@ -286,7 +287,8 @@ (define-module (foo bar)
|
||||||
(locale \"eo_EO.UTF-8\")
|
(locale \"eo_EO.UTF-8\")
|
||||||
|
|
||||||
(services
|
(services
|
||||||
(cons (service mcron-service-type) %base-services)))\n")
|
(cons (service mcron-service-type) %base-services)))\n"
|
||||||
|
#:format-comment canonicalize-comment)
|
||||||
|
|
||||||
(test-equal "pretty-print-with-comments, canonicalize-comment"
|
(test-equal "pretty-print-with-comments, canonicalize-comment"
|
||||||
"\
|
"\
|
||||||
|
|
Loading…
Reference in a new issue