Formatting

Here's how to maintain your code neat and tidy.

Reformatting code

Cursive will try to maintain the formatting of your code while you're editing, but in case you want to reformat some or all of it, you can use Ctrl+Alt+L (Code→Reformat Code) to do so. You can even reformat the whole project or selected directories by using this command in the project window.

Indent line

If you've configured Emacs Tab as recommended in our User Interface page, you can re-indent the current line at any time by pressing tab. Otherwise you can invoke the Auto-Indent Lines action with Ctrl+Alt+I (Code→Auto-Indent Lines).

Code style settings

You'll find the Code Style settings for Clojure code at Settings→Editor→Code Style→Clojure -- there you can modify some general settings. However to adjust the indentation settings for a particular macro, you'll need to do the following. Find an instance of the macro in your code, and position the cursor over the macro symbol. You'll get an intention lightbulb which will allow you to modify the indentation using Alt+Enter.

Here’s what those parameters mean.

Macro forms will often have some number of parameters, followed by any number of body forms. Consider condp:

(condp pred expr
  test result
  ...)

condp can be considered to have two arguments, and then its body. This means that its indent value is 2 - it has two parameters that should be indented differently to the rest.

Generally, the parameters are aligned, which means that you’ll get e.g. this:

(condp pred
       expr
  test result
  ...)

So if there’s a line break between the parameters, they will be aligned. All the "body" elements (i.e. elements after the parameter count) are just indented two spaces.

The default indent is two spaces, so if you have a line break before the first parameter, you’ll get this:

(condp
  pred
  expr
  test result
  ...)

"Only Indent" is for people who don’t want to think about any of this, and just always want two-space indent after a line break. This is also often useful for forms like proxy, defrecord, extend-protocol and the like. There’s also a setting Settings→Editor→Code Style→Clojure→General→Default to Only Indent which sets that for all forms which don’t have an explicit setting. See Nikita Prokopov’s great article on why you might want that.

"Function" is usually the default, and treats all form elements after the head as parameters and aligns them, i.e. functions have no body. This is why you’ll get:

(my-fn a
       b
       c)

Again, with a line break before the first param, params will be indented 2 spaces:

(my-fn
  a
  b
  c)

Matching indentation to Emacs

In mixed-editor teams, it's usually desirable to match the formatting in different editors. Here's how to configure Cursive to best match Emacs' formatting.

You can set the formatting for individual forms as described above. This corresponds to put-clojure-indent/define-clojure-indent as described here. Setting that to "Indent only" corresponds to clojure-defun-indents. Note that Cursive does not currently support the indentation specification linked in that section.

Under Settings→Editor→Code Style→Clojure→General, you'll want "One space list indent" set. You can also choose there whether you want map values and let binding values aligned, like this. If you're using clojure-defun-style-default-indent, set "Default to Only indent".


Documentation Test Integration