I want to be able to format an entire clojure file to look nice. The nicest thing I have found is clojures pprint. It does indentation and line breaks at correct locations. However it can only read clojure litterals. Clojure files are read in as strings. read-string
will only take the first parenthesis of a string. Mapping read-string over the whole sequence has a host of issues I am running into. Does someone know of an automatic way to make a clojure file look pretty? Not just indent it correctly?
相关问题
- Better Sequence Duplicate Remover
- Installation of Leiningen 2.X in Mac OS X
- pretty print not working for c++ stl list
- Questions about Lists and other stuff in Clojure
- How do I add CORS to a compojure-api app?
相关文章
- Factor Clojure code setting many different fields
- Does learning one Lisp help in learning the other?
- Better way to nest if-let in clojure
- Quoting YAML (for Travis CI)
- Idiomatic approach for structuring Clojure source
- Formatting a double in JSF
- Is a “transparent” macrolet possible?
- .NET GUID uppercase string format
You can use weavejester/cljfmt:
Check its README for the supported formatting options.
You can use lein-zprint which will run the zprint library over your source files. If you are a boot user, you can use boot-fmt to process your files, which also uses zprint.
The zprint library will completely reformat your source from scratch to be as pretty as it knows how to make it. It actually tries a couple of things at each level to see which is "better", and has a number of heuristics built in to try to produce something that fits as much information as it can in the vertical space while still looking "pretty". It knows a lot about Clojure (and Clojurescript) source code, and knows which functions need different kinds of processing as well as handling the new clojure.spec (cljs.spec) files well too.
It is almost absurdly configurable, so with a little work you can tune it to output the code the way that you want to see it. But even without any configuration, it generally does a good job making your code look nice.
Using it with lein-zprint is pretty trivial. Place
[lein-zprint "0.1.16"]
into the :plugins vector of your project.clj::plugins [[lein-zprint "0.1.16"]]
Then, to format a source file, simply invoke lein zprint on that file:
$ lein zprint src/<project>/<file-name>.clj
Unless you tell it otherwise (which is trivial to do in your project.clj), it will rename the existing file to
<file-name>.clj.old
so that you have them to compare while you are trying it out.Here is an example (obviously poorly formatted):
after formatting with
$lein zprint 70 src/example/apply.clj
which formats it for 70 columns, to make it fit better for this answer: