Is there an online tool to auto-indent and format Clojure code like there are many for JSON?
Asked Answered
F

4

20

There are a lot of tools online that take a JSON text and show you formatted and well indented format of the same.

Some go even further and make a nice tree-like structure: http://jsonviewer.stack.hu/

Do we have something similar for Clojure code ?

Or something that can at least auto-indent it.

If the text that I have is this :

(defn prime? [n known](loop [cnt (dec (count known)) acc []](if (< cnt 0) (not (any? acc))
(recur (dec cnt) (concat acc [(zero? (mod n (nth known cnt)))])))))

It should auto-indent to something like this:

(defn prime? [n known]
  (loop [cnt (dec (count known)) acc []]
    (if (< cnt 0) (not (any? acc))
    (recur (dec cnt) (concat acc [(zero? (mod n (nth known cnt)))])))))
Fever answered 24/7, 2013 at 9:4 Comment(1)
You may want to have a look at my edited answer in case that you are still interested.Matz
M
25

Have a look at https://github.com/xsc/rewrite-clj It is brand new and does exactly what you are asking for.

EDIT I am still getting upvotes for this. I believe I found a better solution: You can easily do this with clojure.pprint utilizing code-dispatch without using an external library.

(clojure.pprint/write '(defn prime? [n known](loop [cnt (dec (count known)) acc []](if (< cnt 0) (not (any? acc))                                                                                                 (recur (dec cnt) (concat acc [(zero? (mod n (nth known cnt)))]))))) 
  :dispatch clojure.pprint/code-dispatch)
=> 
(defn prime? [n known]
  (loop [cnt (dec (count known)) acc []]
    (if (< cnt 0)
      (not (any? acc))
      (recur
        (dec cnt)
        (concat acc [(zero? (mod n (nth known cnt)))])))))
Matz answered 24/7, 2013 at 9:47 Comment(4)
If you wish to have an affect similar to pprint on a whole file, use zprint as in this answer.Dupery
@JasonBasanese Why can't I just use pprint?Matz
It is simply much easier to use a lein tool, and I do not think that pprint can take files as input.Dupery
the pprint solution is good for small line of codes, If you have more number of lines it will actually try to compile the large functions and will give you function too large exception .Aruspex
R
7

I'm not aware of any online services which do this, but there are Clojure libraries which serve this purpose. clojure.pprint comes with Clojure (the key function is clojure.pprint/pprint); Brandon Bloom's fipp is a significantly faster alternative.

Note that neither of these is particularly likely to format code as a programmer armed with Emacs would; they're close enough to be useful, however, and for literal data (not intended to be interpreted as code) may well match human standards.

Relapse answered 24/7, 2013 at 9:28 Comment(2)
Thanks. You have almost answered all of my questions regarding Clojure !Fever
fipps primary goal is for high performance printing not code refactoring just FYICypher
P
5

Following up on this - there is now http://pretty-print.net which will serve this very purpose for EDN and Clojure Code.

Piglet answered 4/3, 2015 at 16:8 Comment(4)
broken though :( just prints "Uncaught Invalid match arg: /^#/" in the javascript console and renders nothing.Brecciate
@Brecciate - sorry about that. That clojurescript version got a bit janky with the leiningen version running on the server. I've since moved this to it's own dedicated server and fixed that problem.Piglet
The link is deadDraughty
Luckily it still exists on Wayback web.archive.org/web/20170707155649/http://pretty-print.net:80Lacteous
S
1

There is now https://github.com/weavejester/cljfmt for that purpose

Instructions

Add it in your Leiningen plugins:

:plugins [[lein-cljfmt "0.6.1"]]

Then, to autoformat all code in your project:

lein cljfmt fix

Sample

Your sample code will become:

(defn prime? [n known] (loop [cnt (dec (count known)) acc []] (if (< cnt 0) (not (any? acc))
                                                                  (recur (dec cnt) (concat acc [(zero? (mod n (nth known cnt)))])))))

After adding some linebreaks and reformatting again:

(defn prime? [n known]
  (loop [cnt (dec (count known)) acc []]
    (if (< cnt 0) (not (any? acc))
        (recur (dec cnt) (concat acc [(zero? (mod n (nth known cnt)))])))))
Saltish answered 1/11, 2018 at 15:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.