Skip to content

Commit 2540d7e

Browse files
committed
Go back to the current line and column after formatting.
The previous approach simply moved to the same character offset. This is unlikely to preserve the position of point, as rustfmt often changes whitespace, changing the number of characters before point. Instead, we go back to the line number and column number we were on before. Provided that rustfmt has not radically changed the number of lines, this will typically put point back to its previous position, or at least close. Improves, but doesn't completely solve, issue #162.
1 parent e452995 commit 2540d7e

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

rust-mode.el

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1270,10 +1270,16 @@ This is written mainly to be used as `end-of-defun-function' for Rust."
12701270
(unless (executable-find rust-rustfmt-bin)
12711271
(error "Could not locate executable \"%s\"" rust-rustfmt-bin))
12721272

1273-
(let ((cur-point (point))
1273+
(let ((cur-line (line-number-at-pos))
1274+
(cur-column (current-column))
12741275
(cur-win-start (window-start)))
12751276
(rust--format-call (current-buffer))
1276-
(goto-char cur-point)
1277+
;; Move to the same line and column as before. This is best
1278+
;; effort: if rustfmt inserted lines before point, we end up in
1279+
;; the wrong place. See issue #162.
1280+
(goto-char (point-min))
1281+
(forward-line (1- cur-line))
1282+
(forward-char cur-column)
12771283
(set-window-start (selected-window) cur-win-start))
12781284

12791285
;; Issue #127: Running this on a buffer acts like a revert, and could cause

0 commit comments

Comments
 (0)