Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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)))])))))
like image 423
Amogh Talpallikar Avatar asked Jul 24 '13 09:07

Amogh Talpallikar


People also ask

What is the best code Formatter?

1. Prettier – Code formatter. It is an opinionated code formatter that enforces a consistent style by parsing your code and re-printing it with its own rules that take the maximum line length into account, wrapping code when necessary. Besides, it supports many languages.

How do I get beautify code in Visual Studio?

The code formatting is available in Visual Studio Code (VSCode) through the following shortcuts or key combinations: On Windows Shift + Alt + F. On macOS Shift + Option + F. On Linux Ctrl + Shift + I.


2 Answers

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)))])))))
like image 191
Leon Grapenthin Avatar answered Oct 12 '22 03:10

Leon Grapenthin


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.

like image 42
Michał Marczyk Avatar answered Oct 12 '22 03:10

Michał Marczyk