|
14 | 14 | threading))
|
15 | 15 |
|
16 | 16 | ;; TODO:
|
17 |
| -;; - miser and fill newlines |
| 17 | +;; - fill newlines |
18 | 18 |
|
19 | 19 | (declare simple-dispatch code-dispatch write-out)
|
20 | 20 |
|
|
27 | 27 | *print-base*
|
28 | 28 | 10)
|
29 | 29 |
|
| 30 | +(def ^{:doc "The text column number to start using miser style. |
| 31 | + |
| 32 | + Not all dispatch functions support using a miser style, so the effect |
| 33 | + of this value depends on the value of :lpy:var:`*print-pprint-dispatch*`. |
| 34 | + |
| 35 | + Default is 40. May be set to ``nil`` to disable." |
| 36 | + :dynamic true} |
| 37 | + *print-miser-width* |
| 38 | + 40) |
| 39 | + |
30 | 40 | (def ^{:doc "The dispatch function used for pretty printing.
|
31 | 41 |
|
32 | 42 | Default is :lpy:fn:`simple-dispatch`."
|
|
260 | 270 | (set! (.-indent block) indent)
|
261 | 271 | (set! (.-start-col block) indent)))
|
262 | 272 |
|
| 273 | + ;; Return `true` if a `:linear` newline type should be emitted. |
| 274 | + (emit-linear-nl? [token section] |
| 275 | + (or (.-force-nl? (.-block token)) |
| 276 | + (not (tokens-fit? section)))) |
| 277 | + |
263 | 278 | ;; Return `true` if the given newline type should be emitted.
|
264 | 279 | (emit-nl? [token section]
|
265 | 280 | (condp = (.-kind token)
|
266 | 281 | :mandatory true
|
267 |
| - :linear (or (.-force-nl? (.-block token)) |
268 |
| - (not (tokens-fit? section))) |
| 282 | + :linear (emit-linear-nl? token section) |
| 283 | + :miser (let [miser-width *print-miser-width* |
| 284 | + max-col (:max @writer)] |
| 285 | + (and miser-width |
| 286 | + max-col |
| 287 | + (>= (.-start-col (.-block token)) (- max miser-width)) |
| 288 | + (emit-linear-nl? token section))) |
269 | 289 | ;; TODO: figure out how to handle these newline types
|
270 |
| - :fill nil |
271 |
| - :miser nil)) |
| 290 | + :fill nil)) |
272 | 291 |
|
273 | 292 | ;; Generate the newline and subsequent indent from a newline token token.
|
274 | 293 | (gen-nl [token]
|
|
703 | 722 |
|
704 | 723 | This function performs cycle detection on input values."
|
705 | 724 | [object]
|
706 |
| - (let [length-reached? (and *current-length* |
707 |
| - *print-length* |
708 |
| - (>= *current-length* *print-length*))] |
709 |
| - (if *print-pretty* |
710 |
| - (if length-reached? |
711 |
| - (print "...") |
712 |
| - (do |
713 |
| - (when-let [l *current-length*] |
714 |
| - (set! *current-length* (inc l))) |
715 |
| - (let [obj-id (python/id object)] |
716 |
| - (if (contains? *recursion-context* obj-id) |
717 |
| - (print (.format "<Recursion on {} with id={}>" |
718 |
| - (.-__name__ (class object)) |
719 |
| - (python/hex obj-id))) |
720 |
| - (binding [*recursion-context* (conj *recursion-context* obj-id)] |
721 |
| - (*print-pprint-dispatch* object)))))) |
722 |
| - (pr object)))) |
| 725 | + (if *print-pretty* |
| 726 | + (if (and *current-length* |
| 727 | + *print-length* |
| 728 | + (>= *current-length* *print-length*)) |
| 729 | + (print "...") |
| 730 | + (do |
| 731 | + (when-let [l *current-length*] |
| 732 | + (set! *current-length* (inc l))) |
| 733 | + (let [obj-id (python/id object)] |
| 734 | + (if (contains? *recursion-context* obj-id) |
| 735 | + (print (.format "<Recursion on {} with id={}>" |
| 736 | + (.-__name__ (class object)) |
| 737 | + (python/hex obj-id))) |
| 738 | + (binding [*recursion-context* (conj *recursion-context* obj-id)] |
| 739 | + (*print-pprint-dispatch* object)))))) |
| 740 | + (pr object))) |
723 | 741 |
|
724 | 742 | (defn pprint
|
725 | 743 | "Pretty print ``object`` to the given ``writer``.
|
|
0 commit comments