Skip to content

Commit 174eb8b

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

File tree

1 file changed

+35
-15
lines changed

1 file changed

+35
-15
lines changed

rust-mode.el

+35-15
Original file line numberDiff line numberDiff line change
@@ -1332,29 +1332,49 @@ 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))
13481349
((= 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"))
1350+
(let ((path (buffer-file-name buf))
1351+
(line-error "error: line exceeded maximum width[[:ascii:]]+")
1352+
buf-string)
1353+
(copy-to-buffer buf (point-min) (point-max))
1354+
(with-current-buffer buf
1355+
(while (re-search-forward line-error nil t)
1356+
(replace-match ""))
1357+
(setq buf-string (buffer-string)))
1358+
(goto-char (point-min))
1359+
(save-excursion
1360+
(while (re-search-forward buf-string nil t)
1361+
(replace-match ""))
1362+
(while (re-search-forward "stdin" nil t)
1363+
(replace-match path)
1364+
(goto-char (line-end-position))
1365+
(insert ":1")))
1366+
(compilation-mode)
1367+
(next-error)
1368+
(pop-to-buffer fmt-buffer)))
13551369
(t
1356-
(error "Rustfmt failed, see *rustfmt* buffer for details"))))
1357-
(delete-file tmpf))))
1370+
(let ((path (buffer-file-name buf)))
1371+
(goto-char (point-min))
1372+
(save-excursion
1373+
(while (re-search-forward "stdin" nil t)
1374+
(replace-match path)))
1375+
(compilation-mode)
1376+
(next-error)
1377+
(pop-to-buffer fmt-buffer)))))))))
13581378

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

0 commit comments

Comments
 (0)