Skip to content

Commit d36acc1

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

File tree

1 file changed

+18
-17
lines changed

1 file changed

+18
-17
lines changed

rust-mode.el

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1332,29 +1332,30 @@ 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)))
1347-
(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"))
1348+
(kill-buffer)
1349+
(message "Formatted buffer with rustfmt."))
13551350
(t
1356-
(error "Rustfmt failed, see *rustfmt* buffer for details"))))
1357-
(delete-file tmpf))))
1351+
(let ((path (buffer-file-name buf)))
1352+
(goto-char (point-min))
1353+
(save-excursion
1354+
(while (re-search-forward "stdin" nil t)
1355+
(replace-match path))
1356+
(compilation-mode)
1357+
(pop-to-buffer fmt-buffer))
1358+
(message "Rustfmt could not format some lines.")))))))))
13581359

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

0 commit comments

Comments
 (0)