|
7 | 7 | ;;; Code:
|
8 | 8 | ;;; Options
|
9 | 9 |
|
| 10 | +(require 'rust-compile) |
| 11 | +(require 'compile) |
| 12 | + |
10 | 13 | (defcustom rust-format-on-save nil
|
11 | 14 | "Format future rust buffers before saving using rustfmt."
|
12 | 15 | :type 'boolean
|
|
39 | 42 |
|
40 | 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 | 73 | (defun rust--format-call (buf)
|
43 | 74 | "Format BUF using rustfmt."
|
44 | 75 | (let ((path exec-path))
|
|
69 | 100 | (with-current-buffer buf
|
70 | 101 | (replace-buffer-contents rust-rustfmt-buffername))
|
71 | 102 | (copy-to-buffer buf (point-min) (point-max))))
|
72 |
| - (let ((win (get-buffer-window rust-rustfmt-buffername))) |
73 |
| - (if win |
74 |
| - (quit-window t win) |
75 |
| - (kill-buffer rust-rustfmt-buffername)))) |
| 103 | + (let ((win (get-buffer-window rust-rustfmt-buffername))) |
| 104 | + (if win |
| 105 | + (quit-window t win) |
| 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 | 117 | ((= ret 3)
|
77 | 118 | (if (not (string= (buffer-string)
|
78 | 119 | (with-current-buffer buf (buffer-string))))
|
|
0 commit comments