@@ -1270,10 +1270,16 @@ This is written mainly to be used as `end-of-defun-function' for Rust."
1270
1270
(unless (executable-find rust-rustfmt-bin)
1271
1271
(error " Could not locate executable \" %s \" " rust-rustfmt-bin))
1272
1272
1273
- (let ((cur-point (point ))
1273
+ (let ((cur-line (line-number-at-pos ))
1274
+ (cur-column (current-column ))
1274
1275
(cur-win-start (window-start )))
1275
1276
(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)
1277
1283
(set-window-start (selected-window ) cur-win-start))
1278
1284
1279
1285
; ; 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."
1365
1371
; ; to use `font-lock-ensure' , which doesn't exist in Emacs 24 and earlier.
1366
1372
; ; If it's not available, fall back to calling `font-lock-fontify-region'
1367
1373
; ; on the whole buffer.
1368
- (if (fboundp 'font-lock-ensure )
1369
- (font-lock-ensure )
1370
- (font-lock-fontify-region (point-min ) (point-max ))))
1374
+ (save-excursion
1375
+ (if (fboundp 'font-lock-ensure )
1376
+ (font-lock-ensure )
1377
+ (font-lock-fontify-region (point-min ) (point-max )))))
1371
1378
1372
1379
(defun rust--before-save-hook ()
1373
1380
(when rust-format-on-save (rust-format-buffer)))
0 commit comments