Skip to content

Commit 936a187

Browse files
committed
handle indirect buffers and multiple windows in rust-format-buffer
1 parent e327658 commit 936a187

File tree

1 file changed

+54
-11
lines changed

1 file changed

+54
-11
lines changed

rust-mode.el

Lines changed: 54 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1287,17 +1287,60 @@ This is written mainly to be used as `end-of-defun-function' for Rust."
12871287
(unless (executable-find rust-rustfmt-bin)
12881288
(error "Could not locate executable \"%s\"" rust-rustfmt-bin))
12891289

1290-
(let ((cur-line (line-number-at-pos))
1291-
(cur-column (current-column))
1292-
(cur-win-start (window-start)))
1293-
(rust--format-call (current-buffer))
1294-
;; Move to the same line and column as before. This is best
1295-
;; effort: if rustfmt inserted lines before point, we end up in
1296-
;; the wrong place. See issue #162.
1297-
(goto-char (point-min))
1298-
(forward-line (1- cur-line))
1299-
(forward-char cur-column)
1300-
(set-window-start (selected-window) cur-win-start))
1290+
(let* ((current (current-buffer))
1291+
(base (or (buffer-base-buffer current) current))
1292+
buffer-pos
1293+
window-pos)
1294+
(dolist (buffer (buffer-list))
1295+
(when (or (eq buffer base)
1296+
(eq (buffer-base-buffer buffer) base))
1297+
(with-current-buffer buffer
1298+
(push (list buffer
1299+
(line-number-at-pos)
1300+
(current-column))
1301+
buffer-pos))))
1302+
(dolist (window (window-list))
1303+
(let ((buffer (window-buffer window)))
1304+
(when (or (eq buffer base)
1305+
(eq (buffer-base-buffer buffer) base))
1306+
(let ((start (window-start window))
1307+
(point (window-point window)))
1308+
(with-current-buffer buffer
1309+
(push (list window
1310+
(line-number-at-pos start)
1311+
(save-excursion (goto-char start) (current-column))
1312+
(line-number-at-pos point)
1313+
(save-excursion (goto-char point) (current-column)))
1314+
window-pos))))))
1315+
(rust--format-call current)
1316+
(dolist (pos buffer-pos)
1317+
(let ((buffer (pop pos))
1318+
(line (pop pos))
1319+
(column (pop pos)))
1320+
(with-current-buffer buffer
1321+
;; Move to the same line and column as before. This is best
1322+
;; effort: if rustfmt inserted lines before point, we end up in
1323+
;; the wrong place. See issue #162.
1324+
(goto-char (point-min))
1325+
(forward-line (1- line))
1326+
(forward-char column))))
1327+
(dolist (pos window-pos)
1328+
(let ((window (pop pos))
1329+
(start-line (pop pos))
1330+
(start-column (pop pos))
1331+
(point-line (pop pos))
1332+
(point-column (pop pos)))
1333+
(with-current-buffer (window-buffer window)
1334+
(let ((start (save-excursion (goto-char (point-min))
1335+
(forward-line (1- start-line))
1336+
(forward-char start-column)
1337+
(point)))
1338+
(point (save-excursion (goto-char (point-min))
1339+
(forward-line (1- point-line))
1340+
(forward-char point-column)
1341+
(point))))
1342+
(set-window-start window start)
1343+
(set-window-point window point))))))
13011344

13021345
;; Issue #127: Running this on a buffer acts like a revert, and could cause
13031346
;; the fontification to get out of sync. Call the same hook to ensure it is

0 commit comments

Comments
 (0)