diff options
Diffstat (limited to '.emacs.d/lisp/rust-rustfmt.el')
| -rw-r--r-- | .emacs.d/lisp/rust-rustfmt.el | 51 |
1 files changed, 46 insertions, 5 deletions
diff --git a/.emacs.d/lisp/rust-rustfmt.el b/.emacs.d/lisp/rust-rustfmt.el index cab183c..04dce45 100644 --- a/.emacs.d/lisp/rust-rustfmt.el +++ b/.emacs.d/lisp/rust-rustfmt.el | |||
| @@ -7,6 +7,9 @@ | |||
| 7 | ;;; Code: | 7 | ;;; Code: |
| 8 | ;;; Options | 8 | ;;; Options |
| 9 | 9 | ||
| 10 | (require 'rust-compile) | ||
| 11 | (require 'compile) | ||
| 12 | |||
| 10 | (defcustom rust-format-on-save nil | 13 | (defcustom rust-format-on-save nil |
| 11 | "Format future rust buffers before saving using rustfmt." | 14 | "Format future rust buffers before saving using rustfmt." |
| 12 | :type 'boolean | 15 | :type 'boolean |
| @@ -30,7 +33,7 @@ | |||
| 30 | :type 'string | 33 | :type 'string |
| 31 | :group 'rust-mode) | 34 | :group 'rust-mode) |
| 32 | 35 | ||
| 33 | (defcustom rust-rustfmt-switches '("--edition" "2018") | 36 | (defcustom rust-rustfmt-switches '("--edition" "2021") |
| 34 | "Arguments to pass when invoking the `rustfmt' executable." | 37 | "Arguments to pass when invoking the `rustfmt' executable." |
| 35 | :type '(repeat string) | 38 | :type '(repeat string) |
| 36 | :group 'rust-mode) | 39 | :group 'rust-mode) |
| @@ -39,6 +42,34 @@ | |||
| 39 | 42 | ||
| 40 | (defconst rust-rustfmt-buffername "*rustfmt*") | 43 | (defconst rust-rustfmt-buffername "*rustfmt*") |
| 41 | 44 | ||
| 45 | (define-compilation-mode rust-format-mode "rust-format" | ||
| 46 | "Major mode for Rust compilation output." | ||
| 47 | |||
| 48 | (setq-local compilation-error-regexp-alist-alist nil) | ||
| 49 | (add-to-list 'compilation-error-regexp-alist-alist | ||
| 50 | (cons 'rustc-refs rustc-refs-compilation-regexps)) | ||
| 51 | (add-to-list 'compilation-error-regexp-alist-alist | ||
| 52 | (cons 'rustc rustc-compilation-regexps)) | ||
| 53 | (add-to-list 'compilation-error-regexp-alist-alist | ||
| 54 | (cons 'rustc-colon rustc-colon-compilation-regexps)) | ||
| 55 | (add-to-list 'compilation-error-regexp-alist-alist | ||
| 56 | (cons 'cargo cargo-compilation-regexps)) | ||
| 57 | (add-to-list 'compilation-error-regexp-alist-alist | ||
| 58 | (cons 'rustc-panics rustc-panics-compilation-regexps)) | ||
| 59 | |||
| 60 | (setq-local compilation-error-regexp-alist nil) | ||
| 61 | (add-to-list 'compilation-error-regexp-alist 'rustc-refs) | ||
| 62 | (add-to-list 'compilation-error-regexp-alist 'rustc) | ||
| 63 | (add-to-list 'compilation-error-regexp-alist 'rustc-colon) | ||
| 64 | (add-to-list 'compilation-error-regexp-alist 'cargo) | ||
| 65 | (add-to-list 'compilation-error-regexp-alist 'rustc-panics) | ||
| 66 | |||
| 67 | (add-hook 'next-error-hook #'rustc-scroll-down-after-next-error) | ||
| 68 | |||
| 69 | (if (or compilation-auto-jump-to-first-error | ||
| 70 | (eq compilation-scroll-output 'first-error)) | ||
| 71 | (set (make-local-variable 'compilation-auto-jump-to-next) t))) | ||
| 72 | |||
| 42 | (defun rust--format-call (buf) | 73 | (defun rust--format-call (buf) |
| 43 | "Format BUF using rustfmt." | 74 | "Format BUF using rustfmt." |
| 44 | (let ((path exec-path)) | 75 | (let ((path exec-path)) |
| @@ -69,10 +100,20 @@ | |||
| 69 | (with-current-buffer buf | 100 | (with-current-buffer buf |
| 70 | (replace-buffer-contents rust-rustfmt-buffername)) | 101 | (replace-buffer-contents rust-rustfmt-buffername)) |
| 71 | (copy-to-buffer buf (point-min) (point-max)))) | 102 | (copy-to-buffer buf (point-min) (point-max)))) |
| 72 | (let ((win (get-buffer-window rust-rustfmt-buffername))) | 103 | (let ((win (get-buffer-window rust-rustfmt-buffername))) |
| 73 | (if win | 104 | (if win |
| 74 | (quit-window t win) | 105 | (quit-window t win) |
| 75 | (kill-buffer rust-rustfmt-buffername)))) | 106 | (kill-buffer rust-rustfmt-buffername)))) |
| 107 | ((= ret 1) | ||
| 108 | (erase-buffer) | ||
| 109 | (insert-file-contents tmpf) | ||
| 110 | |||
| 111 | (rust-format-mode) ;; render compilation errors in compilation-mode | ||
| 112 | (setq-local compile-command (format "%s %s" rust-rustfmt-bin (buffer-file-name buf))) | ||
| 113 | |||
| 114 | (rust--format-fix-rustfmt-buffer (buffer-name buf)) | ||
| 115 | (error "Rustfmt failed because of parsing errors, see %s buffer for details" | ||
| 116 | rust-rustfmt-buffername)) | ||
| 76 | ((= ret 3) | 117 | ((= ret 3) |
| 77 | (if (not (string= (buffer-string) | 118 | (if (not (string= (buffer-string) |
| 78 | (with-current-buffer buf (buffer-string)))) | 119 | (with-current-buffer buf (buffer-string)))) |
