Skip to content

Commit dbf5e28

Browse files
committed
[Fix #977] Properly indent the result of cider-format-region
1 parent 11fdeb4 commit dbf5e28

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ non-functioning `cider-test-jump` from test reports.
3232
`user` namespace when using `cider-interactive-eval`.
3333
* [#954](https://github.com/clojure-emacs/cider/issues/954): Detect properly a project's root
3434
when in buffer that's not visiting a file (e.g. a REPL buffer).
35+
* [#977](https://github.com/clojure-emacs/cider/issues/977): `cider-format-region` now respects indentation of the region start position
3536

3637
## 0.8.2 / 2014-12-21
3738

cider-interaction.el

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1794,17 +1794,24 @@ of the buffer into a formatted string."
17941794
(cider--format-buffer (lambda (edn)
17951795
(cider-sync-request:format-edn edn fill-column))))
17961796

1797+
(defun cider--format-reindent (formatted start)
1798+
"Reindent FORMATTED to align with buffer position START."
1799+
(let* ((start-column (save-excursion (goto-char start) (current-column)))
1800+
(indent-line (concat "\n" (make-string start-column ? ))))
1801+
(replace-regexp-in-string "\n" indent-line formatted)))
1802+
17971803
(defun cider--format-region (start end formatter)
17981804
"Format the contents of the given region.
17991805
18001806
START and END are the character positions of the start and end of the
18011807
region. FORMATTER is a function of one argument which is used to convert
18021808
the string contents of the region into a formatted string."
18031809
(let* ((original (buffer-substring-no-properties start end))
1804-
(formatted (funcall formatter original)))
1810+
(formatted (funcall formatter original))
1811+
(indented (cider--format-reindent formatted start)))
18051812
(unless (equal original indented)
18061813
(delete-region start end)
1807-
(insert formatted))))
1814+
(insert indented))))
18081815

18091816
(defun cider-format-region (start end)
18101817
"Format the Clojure code in the current region."

0 commit comments

Comments
 (0)