Fork me on GitHub
#beginners
<
2023-10-24
>
λ18:10:22

Is there a built in tool for formatting?

seancorfield18:10:13

Some editors have built-in formatting support. There are several command-line tools as well.

seancorfield18:10:13

cljfmt is probably the most widely used https://github.com/weavejester/cljfmt Calva uses this for its formatting in VS Code as well.

hiredman18:10:03

pprint has a code formatting mode, doesn't see a lot of use

hiredman18:10:30

(require '[clojure.pprint :as pp])

(let [sexp '(let [jsons (.getBytes (str
                              (json/write-str {})
                              (json/write-str {:a 42})
                              (json/write-str {})))]
        (with-open [rdr (-> (io/reader jsons)
                            (java.io.PushbackReader. 64))]
          (dotimes [_ 5]
            (prn (#'json/-read rdr false ::eof {})))))]
  (prn sexp)
  (pp/with-pprint-dispatch
    pp/code-dispatch
    (pp/pprint
     sexp)))

practicalli-johnny05:10:44

cljstyle and cljfmt are common tools, also found in the setup-clojure GitHub action for use in CI workflows. cljstyle is also part of MegaLinter, along with clj-kondo Both cljstyle and cljfmt follow the fundamentals of the community Clojure style guide (although personally I found cljstyle easier to work with) zprint is a very powerful and general purpose code format and text manipulation library. There is a zprint profile that formats along the lines of the community Clojure style guide. zprint arguably has a steeper learning curve than the other tools, although can do far more.

1