Skip to content

feat: make indentation of special arguments customisable #582

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* [#571](https://github.com/clojure-emacs/clojure-mode/issues/571): Remove `project.el` integration.
* [#574](https://github.com/clojure-emacs/clojure-mode/issues/574): Remove `clojure-view-grimoire` command.
* Stop `clojure-sort-ns` from calling `redisplay`
* Add 'clojure-special-arg-indent-factor' to control special argument indentation.

## 5.12.0 (2020-08-13)

Expand Down
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,24 @@ For instructions on how to write these specifications, see
[this document](https://docs.cider.mx/cider/indent_spec.html).
The only difference is that you're allowed to use lists instead of vectors.

The indentation of special arguments is controlled by
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this explanation to be a bit confusing, because I don't think we mention the meaning of special arguments anywhere in the readme and at least for me - the special argument is this case (and most of the time) is the body. I understand the examples, but I'm wondering if others won't be confused by them.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, forget about this - https://docs.cider.mx/cider/indent_spec.html#special-arguments I no longer remember some of the things I wrote myself. :D

`clojure-special-arg-indent-factor`, which by default indents special arguments
a further `lisp-body-indent` when compared to ordinary arguments.

An example of the default formatting is:

```clojure
(defrecord MyRecord
[my-field])
```

Setting `clojure-special-arg-indent-factor` to 1, results in:

```clojure
(defrecord MyRecord
[my-field])
```

### Indentation of Comments

`clojure-mode` differentiates between comments like `;`, `;;`, etc.
Expand Down
10 changes: 9 additions & 1 deletion clojure-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -1136,6 +1136,13 @@ will align the values like this:
:safe #'listp
:type '(repeat string))

(defcustom clojure-special-arg-indent-factor
2
"Factor of the 'lisp-body-indent' used to indent special arguments."
:package-version '(clojure-mode . "5.13")
:type 'integer
:safe 'integerp)

(defvar clojure--beginning-of-reader-conditional-regexp
"#\\?@(\\|#\\?("
"Regexp denoting the beginning of a reader conditional.")
Expand Down Expand Up @@ -1503,7 +1510,8 @@ This function also returns nil meaning don't specify the indentation."
(clojure--normal-indent last-sexp 'always-align))
;; Special arg. Rigidly indent with a large indentation.
(t
(+ (* 2 lisp-body-indent) containing-form-column)))))
(+ (* clojure-special-arg-indent-factor lisp-body-indent)
containing-form-column)))))
(`:defn
(+ lisp-body-indent containing-form-column))
((pred functionp)
Expand Down
45 changes: 45 additions & 0 deletions test/clojure-mode-indentation-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,51 @@ DESCRIPTION is a string with the description of the spec."
(ala/bala top
|one)"))

(describe "specify an indentation for symbol"
(put-clojure-indent 'cala 1)

(when-indenting-with-point-it "should handle a symbol with ns"
"
(cala top
|one)"
"
(cala top
|one)")
(when-indenting-with-point-it "should handle special arguments"
"
(cala
|top
one)"
"
(cala
|top
one)"))
(describe "should respect special argument indentation"
:var (clojure-special-arg-indent-factor)
(before-each
(setq clojure-special-arg-indent-factor 1))
(after-each
(setq clojure-special-arg-indent-factor 2))

(put-clojure-indent 'cala 1)

(when-indenting-with-point-it "should handle a symbol with ns"
"
(cala top
|one)"
"
(cala top
|one)")
(when-indenting-with-point-it "should handle special arguments"
"
(cala
|top
one)"
"
(cala
|top
one)"))

(describe "we can pass a lambda to explicitly set the column"
(put-clojure-indent 'arsymbol (lambda (indent-point state) 0))

Expand Down