diff options
Diffstat (limited to '.emacs.d/lisp/rust-rustfmt.el')
| -rw-r--r-- | .emacs.d/lisp/rust-rustfmt.el | 94 |
1 files changed, 50 insertions, 44 deletions
diff --git a/.emacs.d/lisp/rust-rustfmt.el b/.emacs.d/lisp/rust-rustfmt.el index bb88647..f609980 100644 --- a/.emacs.d/lisp/rust-rustfmt.el +++ b/.emacs.d/lisp/rust-rustfmt.el | |||
| @@ -41,49 +41,54 @@ | |||
| 41 | 41 | ||
| 42 | (defun rust--format-call (buf) | 42 | (defun rust--format-call (buf) |
| 43 | "Format BUF using rustfmt." | 43 | "Format BUF using rustfmt." |
| 44 | (with-current-buffer (get-buffer-create rust-rustfmt-buffername) | 44 | (let ((path exec-path)) |
| 45 | (view-mode +1) | 45 | (with-current-buffer (get-buffer-create rust-rustfmt-buffername) |
| 46 | (let ((inhibit-read-only t)) | 46 | (setq-local exec-path path) |
| 47 | (erase-buffer) | 47 | (view-mode +1) |
| 48 | (insert-buffer-substring buf) | 48 | (let ((inhibit-read-only t)) |
| 49 | (let* ((tmpf (make-temp-file "rustfmt")) | 49 | (erase-buffer) |
| 50 | (ret (apply #'call-process-region | 50 | (insert-buffer-substring buf) |
| 51 | (point-min) | 51 | (let* ((tmpf (make-temp-file "rustfmt")) |
| 52 | (point-max) | 52 | (ret (apply #'call-process-region |
| 53 | rust-rustfmt-bin | 53 | (point-min) |
| 54 | t | 54 | (point-max) |
| 55 | `(t ,tmpf) | 55 | rust-rustfmt-bin |
| 56 | nil | 56 | t |
| 57 | rust-rustfmt-switches))) | 57 | `(t ,tmpf) |
| 58 | (unwind-protect | 58 | nil |
| 59 | (cond | 59 | rust-rustfmt-switches))) |
| 60 | ((zerop ret) | 60 | (unwind-protect |
| 61 | (if (not (string= (buffer-string) | 61 | (cond |
| 62 | (with-current-buffer buf (buffer-string)))) | 62 | ((zerop ret) |
| 63 | ;; replace-buffer-contents was in emacs 26.1, but it | 63 | (if (not (string= (buffer-string) |
| 64 | ;; was broken for non-ASCII strings, so we need 26.2. | 64 | (with-current-buffer buf (buffer-string)))) |
| 65 | (if (and (fboundp 'replace-buffer-contents) | 65 | ;; replace-buffer-contents was in emacs 26.1, but it |
| 66 | (version<= "26.2" emacs-version)) | 66 | ;; was broken for non-ASCII strings, so we need 26.2. |
| 67 | (with-current-buffer buf | 67 | (if (and (fboundp 'replace-buffer-contents) |
| 68 | (replace-buffer-contents rust-rustfmt-buffername)) | 68 | (version<= "26.2" emacs-version)) |
| 69 | (copy-to-buffer buf (point-min) (point-max)))) | 69 | (with-current-buffer buf |
| 70 | (kill-buffer)) | 70 | (replace-buffer-contents rust-rustfmt-buffername)) |
| 71 | ((= ret 3) | 71 | (copy-to-buffer buf (point-min) (point-max)))) |
| 72 | (if (not (string= (buffer-string) | 72 | (let ((win (get-buffer-window rust-rustfmt-buffername))) |
| 73 | (with-current-buffer buf (buffer-string)))) | 73 | (if win |
| 74 | (copy-to-buffer buf (point-min) (point-max))) | 74 | (quit-window t win) |
| 75 | (erase-buffer) | 75 | (kill-buffer rust-rustfmt-buffername)))) |
| 76 | (insert-file-contents tmpf) | 76 | ((= ret 3) |
| 77 | (rust--format-fix-rustfmt-buffer (buffer-name buf)) | 77 | (if (not (string= (buffer-string) |
| 78 | (error "Rustfmt could not format some lines, see %s buffer for details" | 78 | (with-current-buffer buf (buffer-string)))) |
| 79 | rust-rustfmt-buffername)) | 79 | (copy-to-buffer buf (point-min) (point-max))) |
| 80 | (t | 80 | (erase-buffer) |
| 81 | (erase-buffer) | 81 | (insert-file-contents tmpf) |
| 82 | (insert-file-contents tmpf) | 82 | (rust--format-fix-rustfmt-buffer (buffer-name buf)) |
| 83 | (rust--format-fix-rustfmt-buffer (buffer-name buf)) | 83 | (error "Rustfmt could not format some lines, see %s buffer for details" |
| 84 | (error "Rustfmt failed, see %s buffer for details" | 84 | rust-rustfmt-buffername)) |
| 85 | rust-rustfmt-buffername)))) | 85 | (t |
| 86 | (delete-file tmpf))))) | 86 | (erase-buffer) |
| 87 | (insert-file-contents tmpf) | ||
| 88 | (rust--format-fix-rustfmt-buffer (buffer-name buf)) | ||
| 89 | (error "Rustfmt failed, see %s buffer for details" | ||
| 90 | rust-rustfmt-buffername)))) | ||
| 91 | (delete-file tmpf)))))) | ||
| 87 | 92 | ||
| 88 | ;; Since we run rustfmt through stdin we get <stdin> markers in the | 93 | ;; Since we run rustfmt through stdin we get <stdin> markers in the |
| 89 | ;; output. This replaces them with the buffer name instead. | 94 | ;; output. This replaces them with the buffer name instead. |
| @@ -147,7 +152,8 @@ rustfmt complain in the echo area." | |||
| 147 | (goto-char (point-min)) | 152 | (goto-char (point-min)) |
| 148 | (forward-line (1- (car target-point))) | 153 | (forward-line (1- (car target-point))) |
| 149 | (forward-char (1- (cdr target-point)))) | 154 | (forward-char (1- (cdr target-point)))) |
| 150 | (message target-problem))))) | 155 | (unless rust-format-show-buffer |
| 156 | (message target-problem)))))) | ||
| 151 | 157 | ||
| 152 | (defconst rust--format-word "\ | 158 | (defconst rust--format-word "\ |
| 153 | \\b\\(else\\|enum\\|fn\\|for\\|if\\|let\\|loop\\|\ | 159 | \\b\\(else\\|enum\\|fn\\|for\\|if\\|let\\|loop\\|\ |
