zprint is a program which will "pretty-print" Clojure(script) source or 
objects.

https://github.com/kkinnear/zprint

*Major new features:*

Some people want a formatting tool to enforce a particular style, regardless 
of the input. That is what classic zprint does -- it enforces the style 
configured in the options map, almost completely ignoring how the input was 
formatted.  Filling that need is why zprint exists!

However, about a year ago, it became clear that some people wanted a source 
formatting tool that will take into account some amount of the formatting 
that is already present in the input file. Two new capabilities for this 
are now available in zprint:

*Respect Newlines *- {:style :respect-nl}

This approach to source formatting is similar to classic zprint, but it 
will not remove any newlines. It will add them as necessary based on the 
formatting configuration given to zprint. It is effective when you want 
zprint to "pretty up" your source, but not entirely ignore the newlines 
that you sent in.


*See below for some examples.*
In many cases, the classic zprint formatting works fine, but sometimes there 
are one or two functions that would be better handled with :style 
:respect-nl. If you have a function "func" which would be better handled 
with :style :respect-nl, then an options map of {:fn-map {"func" [:none 
{:style :respect-nl}]}} will do that for you.

*Indent Only *- {:style :indent-only}

This approach to source formatting will not remove nor add any newlines. 
Every line stays as it is -- the only change zprint will make is to indent 
each line and to regularize whitespace on the remainder of the line. It 
bears little relationship to classic zprint, and doesn't format functions 
based on the :fn-map styles configured. It decides whether to hang or flow 
a list based on the locations of the first three elements in a list -- if 
the first two are on the same line, and the third is on the next line 
aligned with the second on previous line, it will hang the list, otherwise it 
will flow the list. The indent-only approach is for people who want the 
code they put on each line to stay on that line!

You can do this for a whole file by specifying the options map {:style 
:indent-only}, or for a single function (say "func") by  giving this 
options map: {:fn-map {"func" [:none {:style :indent-only}]}}.

*See below for examples.*

If all you ever want is for zprint to do {:style :indent-only}, put {:style 
:indent-only} in your $HOME/.zprintrc file.



In addition, some people wanted a source formatter that enforces a particular 
configuration approach, and does it without need for or any option to 
change its configuration. This is in line with the "nobody loves the format 
produced by xxx, but since it is standard, everyone can live with it".

Now you have:

*Default mode*

If you give the uberjar (or graalVM binaries) -d or --default on the 
command line, they will run zprint with no external configuration. The 
default configuration is what you get, and no other options are read from 
anywhere. In particular, the $HOME/.zprintrc file is not examined, nor is 
any other external configuration file. No matter who runs it, running 
zprint with "-d" will produce the same output from the same input file. 
Note that lein-zprint also implements these same switches.   I don't really 
expect that anyone will find this all that useful, but it demonstrates what 
is possible.

I am open to supporting two additional switches: "-s" "--standard", which 
could be considered a standard for those folks that want a no-config source 
formatter.  The only challenge is, of course, what is the "one size mostly 
fits all" configuration for that no-config source formatter?  Who decides? 
If a reasonable proposal can be constructed, I'll implement it as "-s" and "
--standard" in zprint.

--------------------------------------------


*Examples for :respect-nl and :indent-only:*
You really have to try it yourself on your sources to see what :respect-nl and 
:indent-only will do for you, but let's see what we can learn from some 
contrived examples. Note that these only work on zprint 0.5.3 and later:

The data:

zprint.core=> lexp
"(let [a b c \n d] (this is a test) (this is only a test) \n (we need to 
make the line very long))"

It looks like this -- three lines:

zprint.core=> (print lexp)
(let [a b c 
 d] (this is a test) (this is only a test) 
 (we need to make the line very long))nil

*Classic zprint:*

> (czprint lexp {:parse-string? true})
(let [a b
      c d]
  (this is a test)
  (this is only a test)
  (we need to make the line very long))


*Respect-nl:*

> (czprint lexp {:parse-string? true :style :respect-nl})
(let [a b
      c
        d]
  (this is a test)
  (this is only a test)
  (we need to make the line very long))

It kept the newline after the "c".  If you don't like indented second 
elements of a pair, you can get that this way (two styles at once):

> (czprint lexp {:parse-string? true :style [:respect-nl :community]})
(let [a b
      c
      d]
  (this is a test)
  (this is only a test)
  (we need to make the line very long))


*Indent-only:*

> (czprint lexp {:parse-string? true :style :indent-only})
(let [a b c
      d] (this is a test) (this is only a test)
  (we need to make the line very long))

Zprint did the best it could, but there were only three lines in, so there 
are only three lines out. It did indent each of the lines correctly.

An interesting (configurable) capability of :indent-only is how it handles 
"hangs" 
and "flows".

Two different inputs, which differ by exactly one space -- the indent depth for 
"a":

> hexp1
"(this is\n      a\ntest)"
> (print hexp1)
(this is
      a
test)

> hexp2
"(this is\n     a\ntest)"
> (print hexp2)
(this is
     a
test)

For hexp1, :indent-only will recognize that the "a" is aligned directly 
under the "is", and keep the "hang":

> (czprint hexp1 {:parse-string? true :style :indent-only})
(this is
      a
      test)

For hexp2, since there is no exact alignment, :indent-only will interpret 
this as a "flow":

> (czprint hexp2 {:parse-string? true :style :indent-only})
(this is
  a
  test)

As with most everything in zprint, you can turn this off if you don't like 
it: {:list {:indent-only-style :input-hang}} is the default, but you can 
disable this behavior by {:list {:indent-only-style :none}}.

I hope that you find these new capabilities useful!

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/clojure/f9b98191-9e7d-4cea-a05c-1fc87df0480e%40googlegroups.com.

Reply via email to