brandonbloom / fipp

Fast Idiomatic Pretty Printer for Clojure
525 stars 44 forks source link

Handle mixed lists/vectors #16

Closed neapel closed 10 years ago

neapel commented 10 years ago

I was generating a tree of mixed persistent lists and vectors and noticed because of this case that they don't produce the same output.

(pprint-document [:span "foo {" [:align "bar" :line "baz"] "}"] {:width 500}) produces the indended alignment

foo {bar
     baz}

(pprint-document [:span "foo {" '(:align "bar" :line "baz") "}"] {:width 500}) ignores the alignment

foo {bar
baz}

Is there a reason why seq? and vector? are different cases?

brandonbloom commented 10 years ago

This is expected behavior. Seqs are shorthand for [:span ...] to support laziness in document construction. The fully-realized non-lazy pretty document trees never actually exist because shorthands such as seqs->spans are expanded during the serialization traversal. This lets you pretty print a massive document without having to realize the entire document in memory all at once.

However, if there was a hypothetical "expand" function in which all the shorthands were resolved, your second example would become:

[:span [:text "foo {"] [:span [:align] [:text "bar"] [:line " "] [:text "baz"]] [:text "}"]]

This should make it clear why the alignment is ignored: It has no children. The reason for the :key -> [:key] rule is to enable the use of nullary :line.

neapel commented 10 years ago

ok, thanks for the explanation