summaryrefslogtreecommitdiff
path: root/rust-rustfmt.el
diff options
context:
space:
mode:
authoramesgen <amesgen@amesgen.de>2022-11-19 15:07:17 +0100
committeramesgen <amesgen@amesgen.de>2022-12-19 17:37:18 +0100
commit081e62d08c08ed5281474a44beb12dc75688cb4d (patch)
treee4008f879fd3574ee2295d7e5bb07fe55a8964f5 /rust-rustfmt.el
parente2bb3b090eb247fdf711e2fb97cf78a2d29095eb (diff)
downloadrust-mode-081e62d08c08ed5281474a44beb12dc75688cb4d.tar.gz
Support envrc for rustfmt
Diffstat (limited to 'rust-rustfmt.el')
-rw-r--r--rust-rustfmt.el88
1 files changed, 45 insertions, 43 deletions
diff --git a/rust-rustfmt.el b/rust-rustfmt.el
index 9e53fe5..ff5ba25 100644
--- a/rust-rustfmt.el
+++ b/rust-rustfmt.el
@@ -41,49 +41,51 @@
(defun rust--format-call (buf)
"Format BUF using rustfmt."
- (with-current-buffer (get-buffer-create rust-rustfmt-buffername)
- (view-mode +1)
- (let ((inhibit-read-only t))
- (erase-buffer)
- (insert-buffer-substring buf)
- (let* ((tmpf (make-temp-file "rustfmt"))
- (ret (apply #'call-process-region
- (point-min)
- (point-max)
- rust-rustfmt-bin
- t
- `(t ,tmpf)
- nil
- rust-rustfmt-switches)))
- (unwind-protect
- (cond
- ((zerop ret)
- (if (not (string= (buffer-string)
- (with-current-buffer buf (buffer-string))))
- ;; replace-buffer-contents was in emacs 26.1, but it
- ;; was broken for non-ASCII strings, so we need 26.2.
- (if (and (fboundp 'replace-buffer-contents)
- (version<= "26.2" emacs-version))
- (with-current-buffer buf
- (replace-buffer-contents rust-rustfmt-buffername))
- (copy-to-buffer buf (point-min) (point-max))))
- (kill-buffer-and-window))
- ((= ret 3)
- (if (not (string= (buffer-string)
- (with-current-buffer buf (buffer-string))))
- (copy-to-buffer buf (point-min) (point-max)))
- (erase-buffer)
- (insert-file-contents tmpf)
- (rust--format-fix-rustfmt-buffer (buffer-name buf))
- (error "Rustfmt could not format some lines, see %s buffer for details"
- rust-rustfmt-buffername))
- (t
- (erase-buffer)
- (insert-file-contents tmpf)
- (rust--format-fix-rustfmt-buffer (buffer-name buf))
- (error "Rustfmt failed, see %s buffer for details"
- rust-rustfmt-buffername))))
- (delete-file tmpf)))))
+ (let ((path exec-path))
+ (with-current-buffer (get-buffer-create rust-rustfmt-buffername)
+ (setq-local exec-path path)
+ (view-mode +1)
+ (let ((inhibit-read-only t))
+ (erase-buffer)
+ (insert-buffer-substring buf)
+ (let* ((tmpf (make-temp-file "rustfmt"))
+ (ret (apply #'call-process-region
+ (point-min)
+ (point-max)
+ rust-rustfmt-bin
+ t
+ `(t ,tmpf)
+ nil
+ rust-rustfmt-switches)))
+ (unwind-protect
+ (cond
+ ((zerop ret)
+ (if (not (string= (buffer-string)
+ (with-current-buffer buf (buffer-string))))
+ ;; replace-buffer-contents was in emacs 26.1, but it
+ ;; was broken for non-ASCII strings, so we need 26.2.
+ (if (and (fboundp 'replace-buffer-contents)
+ (version<= "26.2" emacs-version))
+ (with-current-buffer buf
+ (replace-buffer-contents rust-rustfmt-buffername))
+ (copy-to-buffer buf (point-min) (point-max))))
+ (kill-buffer-and-window))
+ ((= ret 3)
+ (if (not (string= (buffer-string)
+ (with-current-buffer buf (buffer-string))))
+ (copy-to-buffer buf (point-min) (point-max)))
+ (erase-buffer)
+ (insert-file-contents tmpf)
+ (rust--format-fix-rustfmt-buffer (buffer-name buf))
+ (error "Rustfmt could not format some lines, see %s buffer for details"
+ rust-rustfmt-buffername))
+ (t
+ (erase-buffer)
+ (insert-file-contents tmpf)
+ (rust--format-fix-rustfmt-buffer (buffer-name buf))
+ (error "Rustfmt failed, see %s buffer for details"
+ rust-rustfmt-buffername))))
+ (delete-file tmpf))))))
;; Since we run rustfmt through stdin we get <stdin> markers in the
;; output. This replaces them with the buffer name instead.