diff options
author | Bozhidar Batsov <bozhidar@batsov.com> | 2017-12-11 00:09:18 +0200 |
---|---|---|
committer | Bozhidar Batsov <bozhidar@batsov.com> | 2017-12-11 00:09:18 +0200 |
commit | c1a500d7da606f48cbde8546da922ca6ddfea9ef (patch) | |
tree | 76a29577dbf31ec0b0b75d49f4cd7de4eed59c2d | |
parent | 254f17d022353ae5dc95d8a5148a59c8ec9206ee (diff) |
[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.
-rw-r--r-- | CHANGELOG.md | 1 | ||||
-rw-r--r-- | cider-interaction.el | 11 |
2 files changed, 10 insertions, 2 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index 3146ef9f..5dae4288 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ * [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. * [#2078](https://github.com/clojure-emacs/cider/pull/2078): Improve startup time by bundling together sync requests during startup. * `cider-rotate-default-connection` will warn if you use it with only a single active connection. +* `cider-format-buffer` preserves the point position. ### Bugs Fixed diff --git a/cider-interaction.el b/cider-interaction.el index af5195aa..0c0a1f15 100644 --- a/cider-interaction.el +++ b/cider-interaction.el @@ -1771,8 +1771,15 @@ of the buffer into a formatted string." (let* ((original (substring-no-properties (buffer-string))) (formatted (funcall formatter original))) (unless (equal original formatted) - (erase-buffer) - (insert formatted)))) + (let ((current-line (line-number-at-pos)) + (current-column (current-column))) + (erase-buffer) + (insert formatted) + ;; we have to preserve our point location in the buffer, + ;; but save-excursion doesn't work, because of erase-buffer + (goto-char (point-min)) + (forward-line (1- current-line)) + (forward-char current-column))))) (defun cider-format-buffer () "Format the Clojure code in the current buffer." |