Skip to content

Commit 384051e

Browse files
authored
Merge pull request #470 from amesgen/envrc-rustfmt
Support envrc for rustfmt
2 parents 07c4a3a + 081e62d commit 384051e

File tree

1 file changed

+45
-43
lines changed

1 file changed

+45
-43
lines changed

rust-rustfmt.el

Lines changed: 45 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -41,49 +41,51 @@
4141

4242
(defun rust--format-call (buf)
4343
"Format BUF using rustfmt."
44-
(with-current-buffer (get-buffer-create rust-rustfmt-buffername)
45-
(view-mode +1)
46-
(let ((inhibit-read-only t))
47-
(erase-buffer)
48-
(insert-buffer-substring buf)
49-
(let* ((tmpf (make-temp-file "rustfmt"))
50-
(ret (apply #'call-process-region
51-
(point-min)
52-
(point-max)
53-
rust-rustfmt-bin
54-
t
55-
`(t ,tmpf)
56-
nil
57-
rust-rustfmt-switches)))
58-
(unwind-protect
59-
(cond
60-
((zerop ret)
61-
(if (not (string= (buffer-string)
62-
(with-current-buffer buf (buffer-string))))
63-
;; replace-buffer-contents was in emacs 26.1, but it
64-
;; was broken for non-ASCII strings, so we need 26.2.
65-
(if (and (fboundp 'replace-buffer-contents)
66-
(version<= "26.2" emacs-version))
67-
(with-current-buffer buf
68-
(replace-buffer-contents rust-rustfmt-buffername))
69-
(copy-to-buffer buf (point-min) (point-max))))
70-
(kill-buffer-and-window))
71-
((= ret 3)
72-
(if (not (string= (buffer-string)
73-
(with-current-buffer buf (buffer-string))))
74-
(copy-to-buffer buf (point-min) (point-max)))
75-
(erase-buffer)
76-
(insert-file-contents tmpf)
77-
(rust--format-fix-rustfmt-buffer (buffer-name buf))
78-
(error "Rustfmt could not format some lines, see %s buffer for details"
79-
rust-rustfmt-buffername))
80-
(t
81-
(erase-buffer)
82-
(insert-file-contents tmpf)
83-
(rust--format-fix-rustfmt-buffer (buffer-name buf))
84-
(error "Rustfmt failed, see %s buffer for details"
85-
rust-rustfmt-buffername))))
86-
(delete-file tmpf)))))
44+
(let ((path exec-path))
45+
(with-current-buffer (get-buffer-create rust-rustfmt-buffername)
46+
(setq-local exec-path path)
47+
(view-mode +1)
48+
(let ((inhibit-read-only t))
49+
(erase-buffer)
50+
(insert-buffer-substring buf)
51+
(let* ((tmpf (make-temp-file "rustfmt"))
52+
(ret (apply #'call-process-region
53+
(point-min)
54+
(point-max)
55+
rust-rustfmt-bin
56+
t
57+
`(t ,tmpf)
58+
nil
59+
rust-rustfmt-switches)))
60+
(unwind-protect
61+
(cond
62+
((zerop ret)
63+
(if (not (string= (buffer-string)
64+
(with-current-buffer buf (buffer-string))))
65+
;; replace-buffer-contents was in emacs 26.1, but it
66+
;; was broken for non-ASCII strings, so we need 26.2.
67+
(if (and (fboundp 'replace-buffer-contents)
68+
(version<= "26.2" emacs-version))
69+
(with-current-buffer buf
70+
(replace-buffer-contents rust-rustfmt-buffername))
71+
(copy-to-buffer buf (point-min) (point-max))))
72+
(kill-buffer-and-window))
73+
((= ret 3)
74+
(if (not (string= (buffer-string)
75+
(with-current-buffer buf (buffer-string))))
76+
(copy-to-buffer buf (point-min) (point-max)))
77+
(erase-buffer)
78+
(insert-file-contents tmpf)
79+
(rust--format-fix-rustfmt-buffer (buffer-name buf))
80+
(error "Rustfmt could not format some lines, see %s buffer for details"
81+
rust-rustfmt-buffername))
82+
(t
83+
(erase-buffer)
84+
(insert-file-contents tmpf)
85+
(rust--format-fix-rustfmt-buffer (buffer-name buf))
86+
(error "Rustfmt failed, see %s buffer for details"
87+
rust-rustfmt-buffername))))
88+
(delete-file tmpf))))))
8789

8890
;; Since we run rustfmt through stdin we get <stdin> markers in the
8991
;; output. This replaces them with the buffer name instead.

0 commit comments

Comments
 (0)