Skip to content

Indentation examples

vemv edited this page Jan 30, 2020 · 3 revisions

Metadata examples

For most macros/functions, adding :style/indent n as usual will suffice. formatting-stack will understand it (translating it for cljfmt, provided the spec is simply a number, or the special value :defn), and any spec-compliant editor will understand it too.

(defmacro render
  {:style/indent 1} ;; CIDER will use `1`, cljfmt will receive the translated value of [[:block 1]]
  [options & body]
  ...)

By default, :block indentation is assumed for cljfmt (see its README or these examples for a primer on :format / :inner). You can use :inner via :style.cljfmt/type:

(defmacro render
  {:style/indent 1 ;; CIDER will use `1`
   :style.cljfmt/type :inner} ;; cljfmt will receive the translated value of [[:inner 1]]
  [options & body]
  ...)

As said, formatting-stack can only translate (from CIDER format to cljfmt format):

  • simple, single-digit specs like {:style/indent 1}
  • the special value :defn: {:style/indent :defn}.

If you need anything more complex, use :style.cljfmt/indent. The values will be passed verbatim to cljfmt.

(defmacro render
  {:style/indent 1 ;; CIDER will use `1`
   :style.cljfmt/indent [[:block 2] [:inner 1]]} ;; cljfmt will use `[[:block 2] [:inner 1]]`
  [...]
  ...)

If you want to avoid the situation where you have to author complex indentation specs in two formats, I'd recommend:

  • avoid macros that parse the arguments in compile time (a trick occasionally found in the wild).
    • Use standard Clojure calling conventions and facilities (e.g. destructuring)
  • avoid superfluous multi-arity signatures: just always accept an options argument ({} or & {}, your choice)
    • Or in any case avoid multi-arity signatures where the same positional argument can have different meanings depending on the arity.

Config examples

As mentioned, you should favor metadata over config. Anyway, the config format is simply a map of symbols to specs:

'{fulcro-spec.core/assertions {:style/indent 0
                               :style.cljfmt/type :inner}}

The specs have the same semantics described in the previous section.

This config is passed to formatting-stack.core/format!, either via Component/Integrant or by direct invocation.

Clone this wiki locally