Skip to content

Commit

Permalink
[Fix #977] Properly indent the result of cider-format-region
Browse files Browse the repository at this point in the history
  • Loading branch information
cichli committed Feb 16, 2015
1 parent 11fdeb4 commit dbf5e28
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ non-functioning `cider-test-jump` from test reports.
`user` namespace when using `cider-interactive-eval`.
* [#954](https://github.com/clojure-emacs/cider/issues/954): Detect properly a project's root
when in buffer that's not visiting a file (e.g. a REPL buffer).
* [#977](https://github.com/clojure-emacs/cider/issues/977): `cider-format-region` now respects indentation of the region start position

## 0.8.2 / 2014-12-21

Expand Down
11 changes: 9 additions & 2 deletions cider-interaction.el
Original file line number Diff line number Diff line change
Expand Up @@ -1794,17 +1794,24 @@ of the buffer into a formatted string."
(cider--format-buffer (lambda (edn)
(cider-sync-request:format-edn edn fill-column))))

(defun cider--format-reindent (formatted start)
"Reindent FORMATTED to align with buffer position START."
(let* ((start-column (save-excursion (goto-char start) (current-column)))
(indent-line (concat "\n" (make-string start-column ? ))))
(replace-regexp-in-string "\n" indent-line formatted)))

(defun cider--format-region (start end formatter)
"Format the contents of the given region.
START and END are the character positions of the start and end of the
region. FORMATTER is a function of one argument which is used to convert
the string contents of the region into a formatted string."
(let* ((original (buffer-substring-no-properties start end))
(formatted (funcall formatter original)))
(formatted (funcall formatter original))
(indented (cider--format-reindent formatted start)))
(unless (equal original indented)
(delete-region start end)
(insert formatted))))
(insert indented))))

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

0 comments on commit dbf5e28

Please sign in to comment.