Skip to content

Commit c9e810b

Browse files
committed
use compilation buffer for rustfmt errors
1 parent 04e3078 commit c9e810b

File tree

1 file changed

+15
-16
lines changed

1 file changed

+15
-16
lines changed

rust-mode.el

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1332,29 +1332,28 @@ This is written mainly to be used as `end-of-defun-function' for Rust."
13321332
;; Formatting using rustfmt
13331333
(defun rust--format-call (buf)
13341334
"Format BUF using rustfmt."
1335-
(with-current-buffer (get-buffer-create "*rustfmt*")
1336-
(erase-buffer)
1337-
(insert-buffer-substring buf)
1338-
(let* ((tmpf (make-temp-file "rustfmt"))
1339-
(ret (call-process-region (point-min) (point-max) rust-rustfmt-bin
1340-
t `(t ,tmpf) nil)))
1341-
(unwind-protect
1335+
(let ((fmt-buffer (get-buffer-create "*rustfmt*")))
1336+
(with-current-buffer fmt-buffer
1337+
(let ((buffer-read-only nil))
1338+
(erase-buffer)
1339+
(insert-buffer-substring buf)
1340+
(let ((ret (shell-command-on-region (point-min) (point-max)
1341+
rust-rustfmt-bin fmt-buffer
1342+
nil nil t)))
13421343
(cond
13431344
((zerop ret)
13441345
(if (not (string= (buffer-string)
13451346
(with-current-buffer buf (buffer-string))))
13461347
(copy-to-buffer buf (point-min) (point-max)))
13471348
(kill-buffer))
1348-
((= ret 3)
1349-
(if (not (string= (buffer-string)
1350-
(with-current-buffer buf (buffer-string))))
1351-
(copy-to-buffer buf (point-min) (point-max)))
1352-
(erase-buffer)
1353-
(insert-file-contents tmpf)
1354-
(error "Rustfmt could not format some lines, see *rustfmt* buffer for details"))
13551349
(t
1356-
(error "Rustfmt failed, see *rustfmt* buffer for details"))))
1357-
(delete-file tmpf))))
1350+
(let ((path (buffer-file-name buf)))
1351+
(goto-char (point-min))
1352+
(save-excursion
1353+
(while (re-search-forward "stdin" nil t)
1354+
(replace-match path))
1355+
(compilation-mode)
1356+
(pop-to-buffer fmt-buffer))))))))))
13581357

13591358
(defconst rust--format-word "\\b\\(else\\|enum\\|fn\\|for\\|if\\|let\\|loop\\|match\\|struct\\|union\\|unsafe\\|while\\)\\b")
13601359
(defconst rust--format-line "\\([\n]\\)")

0 commit comments

Comments
 (0)