Skip to content

Commit c1a500d

Browse files
committed
[Fix #2126] Preserve the point position in cider-format-buffer
That's pretty important for people who want to apply the formatting automatically on buffer save. Unfortunately we can't use save-excursion here, because we're deleting the location we want to preserve, so we have to get a bit more creative.
1 parent 254f17d commit c1a500d

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
@@ -13,6 +13,7 @@
1313
* [cider-nrepl#438](https://github.com/clojure-emacs/cider-nrepl/pull/438): Improve startup time by deferring loading CIDER's middleware until the first usage.
1414
* [#2078](https://github.com/clojure-emacs/cider/pull/2078): Improve startup time by bundling together sync requests during startup.
1515
* `cider-rotate-default-connection` will warn if you use it with only a single active connection.
16+
* `cider-format-buffer` preserves the point position.
1617

1718
### Bugs Fixed
1819

cider-interaction.el

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1771,8 +1771,15 @@ of the buffer into a formatted string."
17711771
(let* ((original (substring-no-properties (buffer-string)))
17721772
(formatted (funcall formatter original)))
17731773
(unless (equal original formatted)
1774-
(erase-buffer)
1775-
(insert formatted))))
1774+
(let ((current-line (line-number-at-pos))
1775+
(current-column (current-column)))
1776+
(erase-buffer)
1777+
(insert formatted)
1778+
;; we have to preserve our point location in the buffer,
1779+
;; but save-excursion doesn't work, because of erase-buffer
1780+
(goto-char (point-min))
1781+
(forward-line (1- current-line))
1782+
(forward-char current-column)))))
17761783

17771784
(defun cider-format-buffer ()
17781785
"Format the Clojure code in the current buffer."

0 commit comments

Comments
 (0)