Skip to content

Commit 5d0b4d7

Browse files
authored
[Fix #2907] Provide ability to customise cljfmt code formatting (#2982)
1 parent 1d6232d commit 5d0b4d7

File tree

3 files changed

+60
-8
lines changed

3 files changed

+60
-8
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
### New features
66

77
* [#2930](https://github.com/clojure-emacs/cider/issues/2930): Add new customization variable `cider-test-default-include-selectors` and `cider-test-default-exclude-selectors` for specifying default test selectors when running commands such as `cider-test-run-ns-tests`.
8+
* [#2907](https://github.com/clojure-emacs/cider/issues/2907): Add new customization variable `cider-format-code-options` to specify options used by `cljfmt` to format code when running commands `cider-format-buffer`, `cider-format-region` and `cider-format-defun`.
89
* [#3002](https://github.com/clojure-emacs/cider/pull/3002): [Inspector] Make collection member truncation limits configurable.
910

1011
### Bugs fixed

cider-client.el

Lines changed: 54 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,47 @@ buffer, defaults to (cider-current-repl)."
210210
If NS is non-nil, include it in the eval request."
211211
(nrepl-sync-request:eval input (or connection (cider-current-repl nil 'ensure)) ns))
212212

213+
(defcustom cider-format-code-options nil
214+
"A map of options that will be passed to `cljfmt' to format code.
215+
Assuming this is the Clojure map you want to use as `cljfmt options':
216+
217+
{:indents {org.me/foo [[:inner 0]]}
218+
:alias-map {\"me\" \"org.me\"}}
219+
220+
you need to encode it as the following plist:
221+
222+
'((\"indents\" ((\"org.me/foo\" ((\"inner\" 0))))) (\"alias-map\" ((\"me\" \"org.me\"))))"
223+
:type 'list
224+
:group 'cider
225+
:package-version '(cider . "1.0.0"))
226+
227+
(defun cider--nrepl-format-code-request-map (&optional format-options)
228+
"Map to merge into requests that require code formatting.
229+
If non-nil, FORMAT-OPTIONS specifies the options cljfmt will use to format
230+
the code. See `cider-format-code-options` for details."
231+
(when format-options
232+
(let* ((indents-dict (when (assoc "indents" format-options)
233+
(thread-last
234+
(cadr (assoc "indents" format-options))
235+
(map-pairs)
236+
(seq-mapcat #'identity)
237+
(apply #'nrepl-dict))))
238+
(alias-map-dict (when (assoc "alias-map" format-options)
239+
(thread-last
240+
(cadr (assoc "alias-map" format-options))
241+
(map-pairs)
242+
(seq-mapcat #'identity)
243+
(apply #'nrepl-dict)))))
244+
(thread-last
245+
(map-merge 'list
246+
(when indents-dict
247+
`(("indents" ,indents-dict)))
248+
(when alias-map-dict
249+
`(("alias-map" ,alias-map-dict))))
250+
(map-pairs)
251+
(seq-mapcat #'identity)
252+
(apply #'nrepl-dict)))))
253+
213254
(defcustom cider-print-fn 'pprint
214255
"Sets the function to use for printing.
215256
@@ -712,12 +753,19 @@ The result entries are relative to the classpath."
712753
(cider-nrepl-send-sync-request)
713754
(nrepl-dict-get "fn-deps")))
714755

715-
(defun cider-sync-request:format-code (code)
716-
"Perform nREPL \"format-code\" op with CODE."
717-
(thread-first `("op" "format-code"
718-
"code" ,code)
719-
(cider-nrepl-send-sync-request)
720-
(nrepl-dict-get "formatted-code")))
756+
(defun cider-sync-request:format-code (code &optional format-options)
757+
"Perform nREPL \"format-code\" op with CODE.
758+
FORMAT-OPTIONS is an optional configuration map for cljfmt."
759+
(let* ((request `("op" "format-code"
760+
"options" ,(cider--nrepl-format-code-request-map format-options)
761+
"code" ,code))
762+
(response (cider-nrepl-send-sync-request request))
763+
(err (nrepl-dict-get response "err")))
764+
(when err
765+
;; err will be a stacktrace with a first line that looks like:
766+
;; "clojure.lang.ExceptionInfo: Unmatched delimiter ]"
767+
(error (car (split-string err "\n"))))
768+
(nrepl-dict-get response "formatted-code")))
721769

722770
(defun cider-sync-request:format-edn (edn right-margin)
723771
"Perform \"format-edn\" op with EDN and RIGHT-MARGIN."

cider-format.el

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,9 @@ Uses the following heuristic to try to maintain point position:
8585
START and END represent the region's boundaries."
8686
(interactive "r")
8787
(cider-ensure-connected)
88-
(cider--format-region start end #'cider-sync-request:format-code))
88+
(cider--format-region start end
89+
(lambda (buf)
90+
(cider-sync-request:format-code buf cider-format-code-options))))
8991

9092

9193
;;; Format defun
@@ -114,7 +116,8 @@ of the buffer into a formatted string."
114116
(interactive)
115117
(check-parens)
116118
(cider-ensure-connected)
117-
(cider--format-buffer #'cider-sync-request:format-code))
119+
(cider--format-buffer (lambda (buf)
120+
(cider-sync-request:format-code buf cider-format-code-options))))
118121

119122

120123
;;; Format EDN

0 commit comments

Comments
 (0)