Formatting Code
CIDER has its own code formatting (indentation) engine, described in
Indentation, but it can also drive the popular
cljfmt formatter and pretty-print EDN, both over nREPL.
Formatting Code with cljfmt
While CIDER has its own code formatting (indentation) engine, you can also
use it together with cljfmt - that’s useful if you’re working on a team
that uses different editors and IDEs.
CIDER provides several commands to interact with cljfmt:
-
cider-format-defun -
cider-format-region -
cider-format-buffer
Generally it’s a good idea to add some hook like this one to make sure on each save operation your buffers are properly formatted:
(add-hook 'before-save-hook 'cider-format-buffer t t)
Notice that you want to apply cljfmt prior to saving the buffer in question.
You can supply additional configuration to cljfmt via the configuration variable
cider-format-code-options. Here’s an example:
;; Let's assume you want to pass the following config
;;
;; {:indents {org.me/foo [[:inner 0]]}
;; :alias-map {\"me\" \"org.me\"}}
;;
;; You'll need to encode it as an Emacs Lisp plist:
(setq cider-format-code-options
'(("indents" (("org.me/foo" (("inner" 0)))))
("alias-map" (("me" "org.me")))))
As of cider-nrepl 0.61, formatting also picks up the project’s own cljfmt
configuration (for example a .cljfmt.edn file at the project root), so a
team can share the same formatting rules regardless of editor. You don’t
have to do anything to opt in - the middleware reads it automatically.
cider-format-code-options continues to let you pass extra cljfmt options
from Emacs.
CIDER doesn’t shell out to cljfmt - it interacts with it via nREPL
(there’s format middleware in cider-nrepl), which is faster than
shelling out.
|