Skip to content

Instantly share code, notes, and snippets.

@inscapist
Last active July 30, 2021 20:34
Show Gist options
  • Save inscapist/f1abbccc211bf8c89801ccf3af51ed27 to your computer and use it in GitHub Desktop.
Save inscapist/f1abbccc211bf8c89801ccf3af51ed27 to your computer and use it in GitHub Desktop.
Cljfmt notes #clojure #clojurescript formatting

My notes on Cljfmt

https://github.com/weavejester/cljfmt

Default indents rules https://github.com/weavejester/cljfmt/blob/master/cljfmt/resources/cljfmt/indents/clojure.clj

Location

.lsp/config.edn

Example:

{:cljfmt {:remove-multiple-non-indenting-spaces? true
          :split-keypairs-over-multiple-lines? false
          :indents {assoc [[:inner 0]]
                    assoc-in [[:block 1]]
                    update [[:inner 0]]
                    update-in [[:block 1]]
                    swap! [[:block 2]]
                    select-keys [[:inner 0]]
                    reg-event-fx [[:inner 0]]
                    reg-event-db [[:inner 0]]
                    reg-fx [[:inner 0]]
                    db-get [[:inner 0]]
                    on-value [[:inner 0]]}}}

Indents rule

There are only 2 kinds of indentations so far.

By default, when the first form arg is not on the same line as symbol, it will be inner, else it will be block

Inner indentation

Inner indentation helps produces more compact code for concision.

(sym arg
  arg
  arg)

cljfmt allows customizing the behavior by forcing inner indentation at a form-depth and form-index A form is the entire "parenthesis + symbol + args". And a form depth is always relative to the symbol. A form depth of zero relative to foo will include the entirety of foo.

So given the following rule, (assoc will always be inner indented regardless of how many form params are on the same line as form symbol)

:indents {assoc [[:inner 0]]}

Block indentation

Block indentation helps emphasize an important form (eg. let, letfn) by indenting arguments to match the level of first form argument.

(sym arg
     arg
     arg)

cljfmt allows conditional block indentation by specifying a rule argument line-arg-count-threshold. line-arg-count is counted from the number of form arguments following form symbol on the same line.

For eg. the following form will compute line-arg-count-threshold == 3

(foo a b c
c d 
)

Now, if the count is more than the threshold, block indentation will be activated, otherwise it falls back to inner

Given {foo [[:block 3]]}:

(foo a b c
d
e)

;; is still inner, hence

(foo a b c
  d
  e)
  
;; whereas
(foo a b c d
e
f)

;; will become block, hence
(foo a b c d
     e
     f)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment