diff options
| author | Ludovic Courtès <ludo@gnu.org> | 2022-08-02 17:39:55 +0200 |
|---|---|---|
| committer | Ludovic Courtès <ludo@gnu.org> | 2022-08-08 11:22:32 +0200 |
| commit | 90ef692e9b48732ae2e3921ff5d101e186506a85 (patch) | |
| tree | 877464d69241abfe3a7c4b36a637a7224faed785 | |
| parent | 9b00c97de41165beefe3eff936470f8e081ca600 (diff) | |
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.
| -rw-r--r-- | guix/read-print.scm | 35 | ||||
| -rw-r--r-- | tests/read-print.scm | 4 |
2 files changed, 22 insertions, 17 deletions
diff --git a/guix/read-print.scm b/guix/read-print.scm index 4a3afdd4f9e..2fc3d85a251 100644 --- a/guix/read-print.scm +++ b/guix/read-print.scm | |||
| @@ -371,23 +371,26 @@ particular newlines, is left as is." | |||
| 371 | "Return the \"width\" of STR--i.e., the width of the longest line of STR." | 371 | "Return the \"width\" of STR--i.e., the width of the longest line of STR." |
| 372 | (apply max (map string-length (string-split str #\newline)))) | 372 | (apply max (map string-length (string-split str #\newline)))) |
| 373 | 373 | ||
| 374 | (define (canonicalize-comment c) | 374 | (define (canonicalize-comment comment indent) |
| 375 | "Canonicalize comment C, ensuring it has the \"right\" number of leading | 375 | "Canonicalize COMMENT, which is to be printed at INDENT, ensuring it has the |
| 376 | semicolons." | 376 | \"right\" number of leading semicolons." |
| 377 | (let ((line (string-trim-both | 377 | (if (zero? indent) |
| 378 | (string-trim (comment->string c) (char-set #\;))))) | 378 | comment ;leave top-level comments unchanged |
| 379 | (string->comment (string-append | 379 | (let ((line (string-trim-both |
| 380 | (if (comment-margin? c) | 380 | (string-trim (comment->string comment) (char-set #\;))))) |
| 381 | ";" | 381 | (string->comment (string-append |
| 382 | (if (string-null? line) | 382 | (if (comment-margin? comment) |
| 383 | ";;" ;no trailing space | 383 | ";" |
| 384 | ";; ")) | 384 | (if (string-null? line) |
| 385 | line "\n") | 385 | ";;" ;no trailing space |
| 386 | (comment-margin? c)))) | 386 | ";; ")) |
| 387 | line "\n") | ||
| 388 | (comment-margin? comment))))) | ||
| 387 | 389 | ||
| 388 | (define* (pretty-print-with-comments port obj | 390 | (define* (pretty-print-with-comments port obj |
| 389 | #:key | 391 | #:key |
| 390 | (format-comment identity) | 392 | (format-comment |
| 393 | (lambda (comment indent) comment)) | ||
| 391 | (format-vertical-space identity) | 394 | (format-vertical-space identity) |
| 392 | (indent 0) | 395 | (indent 0) |
| 393 | (max-width 78) | 396 | (max-width 78) |
| @@ -475,7 +478,7 @@ FORMAT-VERTICAL-SPACE; a useful value of 'canonicalize-vertical-space'." | |||
| 475 | (if (comment-margin? comment) | 478 | (if (comment-margin? comment) |
| 476 | (begin | 479 | (begin |
| 477 | (display " " port) | 480 | (display " " port) |
| 478 | (display (comment->string (format-comment comment)) | 481 | (display (comment->string (format-comment comment indent)) |
| 479 | port)) | 482 | port)) |
| 480 | (begin | 483 | (begin |
| 481 | ;; When already at the beginning of a line, for example because | 484 | ;; When already at the beginning of a line, for example because |
| @@ -483,7 +486,7 @@ FORMAT-VERTICAL-SPACE; a useful value of 'canonicalize-vertical-space'." | |||
| 483 | (unless (= column indent) | 486 | (unless (= column indent) |
| 484 | (newline port) | 487 | (newline port) |
| 485 | (display (make-string indent #\space) port)) | 488 | (display (make-string indent #\space) port)) |
| 486 | (display (comment->string (format-comment comment)) | 489 | (display (comment->string (format-comment comment indent)) |
| 487 | port))) | 490 | port))) |
| 488 | (display (make-string indent #\space) port) | 491 | (display (make-string indent #\space) port) |
| 489 | indent) | 492 | indent) |
diff --git a/tests/read-print.scm b/tests/read-print.scm index 94f018dd443..e3f23194af3 100644 --- a/tests/read-print.scm +++ b/tests/read-print.scm | |||
| @@ -274,6 +274,7 @@ mnopqrstuvwxyz.\")" | |||
| 274 | 274 | ||
| 275 | (test-pretty-print/sequence " | 275 | (test-pretty-print/sequence " |
| 276 | ;;; Hello! | 276 | ;;; Hello! |
| 277 | ;;; Notice that there are three semicolons here. | ||
| 277 | 278 | ||
| 278 | (define-module (foo bar) | 279 | (define-module (foo bar) |
| 279 | #:use-module (guix) | 280 | #:use-module (guix) |
| @@ -286,7 +287,8 @@ mnopqrstuvwxyz.\")" | |||
| 286 | (locale \"eo_EO.UTF-8\") | 287 | (locale \"eo_EO.UTF-8\") |
| 287 | 288 | ||
| 288 | (services | 289 | (services |
| 289 | (cons (service mcron-service-type) %base-services)))\n") | 290 | (cons (service mcron-service-type) %base-services)))\n" |
| 291 | #:format-comment canonicalize-comment) | ||
| 290 | 292 | ||
| 291 | (test-equal "pretty-print-with-comments, canonicalize-comment" | 293 | (test-equal "pretty-print-with-comments, canonicalize-comment" |
| 292 | "\ | 294 | "\ |
