Skip to content

Commit 4355af7

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

File tree

1 file changed

+36
-15
lines changed

1 file changed

+36
-15
lines changed

rust-mode.el

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1332,29 +1332,50 @@ 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+
;; (line-error "replace"))
1337+
(with-current-buffer fmt-buffer
1338+
(let ((buffer-read-only nil))
1339+
(erase-buffer)
1340+
(insert-buffer-substring buf)
1341+
(let ((ret (shell-command-on-region (point-min) (point-max)
1342+
rust-rustfmt-bin fmt-buffer
1343+
nil nil t)))
13421344
(cond
13431345
((zerop ret)
13441346
(if (not (string= (buffer-string)
13451347
(with-current-buffer buf (buffer-string))))
13461348
(copy-to-buffer buf (point-min) (point-max)))
13471349
(kill-buffer))
13481350
((= 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"))
1351+
(let ((path (buffer-file-name buf))
1352+
(line-error "error: line exceeded maximum width[[:ascii:]]+")
1353+
buf-string)
1354+
(copy-to-buffer buf (point-min) (point-max))
1355+
(with-current-buffer buf
1356+
(while (re-search-forward line-error nil t)
1357+
(replace-match ""))
1358+
(setq buf-string (buffer-string)))
1359+
(goto-char (point-min))
1360+
(save-excursion
1361+
(while (re-search-forward buf-string nil t)
1362+
(replace-match ""))
1363+
(while (re-search-forward "stdin" nil t)
1364+
(replace-match path)
1365+
(goto-char (line-end-position))
1366+
(insert ":1")))
1367+
(compilation-mode)
1368+
(next-error)
1369+
(pop-to-buffer fmt-buffer)))
13551370
(t
1356-
(error "Rustfmt failed, see *rustfmt* buffer for details"))))
1357-
(delete-file tmpf))))
1371+
(let ((path (buffer-file-name buf)))
1372+
(goto-char (point-min))
1373+
(save-excursion
1374+
(while (re-search-forward "stdin" nil t)
1375+
(replace-match path)))
1376+
(compilation-mode)
1377+
(next-error)
1378+
(pop-to-buffer fmt-buffer)))))))))
13581379

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

0 commit comments

Comments
 (0)