diff options
| author | Niko Matsakis <niko@alum.mit.edu> | 2016-08-18 15:26:41 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2016-08-18 15:26:41 -0400 |
| commit | f73f3217b24f0fb261d09c4e58a0416a42c82c77 (patch) | |
| tree | add24c219ac5d926a0bef17590157a511e9ba8dd /rust-mode.el | |
| parent | ba5ff9058546df593ff7d8737fb52b5e92ad1913 (diff) | |
| parent | 2540d7eff0faa7e966b6c4391e53617800275f85 (diff) | |
| download | rust-mode-f73f3217b24f0fb261d09c4e58a0416a42c82c77.tar.gz | |
Merge pull request #163 from Wilfred/preserve_point_rustfmt
Correctly restore point position after running rustfmt
Diffstat (limited to 'rust-mode.el')
| -rw-r--r-- | rust-mode.el | 17 |
1 files changed, 12 insertions, 5 deletions
diff --git a/rust-mode.el b/rust-mode.el index 6259b48..99e4bb4 100644 --- a/rust-mode.el +++ b/rust-mode.el @@ -1270,10 +1270,16 @@ This is written mainly to be used as `end-of-defun-function' for Rust." (unless (executable-find rust-rustfmt-bin) (error "Could not locate executable \"%s\"" rust-rustfmt-bin)) - (let ((cur-point (point)) + (let ((cur-line (line-number-at-pos)) + (cur-column (current-column)) (cur-win-start (window-start))) (rust--format-call (current-buffer)) - (goto-char cur-point) + ;; Move to the same line and column as before. This is best + ;; effort: if rustfmt inserted lines before point, we end up in + ;; the wrong place. See issue #162. + (goto-char (point-min)) + (forward-line (1- cur-line)) + (forward-char cur-column) (set-window-start (selected-window) cur-win-start)) ;; Issue #127: Running this on a buffer acts like a revert, and could cause @@ -1365,9 +1371,10 @@ This is written mainly to be used as `end-of-defun-function' for Rust." ;; to use `font-lock-ensure', which doesn't exist in Emacs 24 and earlier. ;; If it's not available, fall back to calling `font-lock-fontify-region' ;; on the whole buffer. - (if (fboundp 'font-lock-ensure) - (font-lock-ensure) - (font-lock-fontify-region (point-min) (point-max)))) + (save-excursion + (if (fboundp 'font-lock-ensure) + (font-lock-ensure) + (font-lock-fontify-region (point-min) (point-max))))) (defun rust--before-save-hook () (when rust-format-on-save (rust-format-buffer))) |
