Skip to content

Commit b8ef8f1

Browse files
committed
Miser
1 parent 6733b96 commit b8ef8f1

File tree

1 file changed

+40
-22
lines changed

1 file changed

+40
-22
lines changed

src/basilisp/pprint.lpy

Lines changed: 40 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
threading))
1515

1616
;; TODO:
17-
;; - miser and fill newlines
17+
;; - fill newlines
1818

1919
(declare simple-dispatch code-dispatch write-out)
2020

@@ -27,6 +27,16 @@
2727
*print-base*
2828
10)
2929

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+
3040
(def ^{:doc "The dispatch function used for pretty printing.
3141

3242
Default is :lpy:fn:`simple-dispatch`."
@@ -260,15 +270,24 @@
260270
(set! (.-indent block) indent)
261271
(set! (.-start-col block) indent)))
262272

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+
263278
;; Return `true` if the given newline type should be emitted.
264279
(emit-nl? [token section]
265280
(condp = (.-kind token)
266281
: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)))
269289
;; TODO: figure out how to handle these newline types
270-
:fill nil
271-
:miser nil))
290+
:fill nil))
272291

273292
;; Generate the newline and subsequent indent from a newline token token.
274293
(gen-nl [token]
@@ -703,23 +722,22 @@
703722

704723
This function performs cycle detection on input values."
705724
[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)))
723741

724742
(defn pprint
725743
"Pretty print ``object`` to the given ``writer``.

0 commit comments

Comments
 (0)