Skip to content

Commit c87f6f8

Browse files
authored
Merge pull request #556 from roife/rustfmt-compilation-mode
introduce a new compilation-mode for rust-format
2 parents 4686c6e + 19199f6 commit c87f6f8

File tree

3 files changed

+65
-4
lines changed

3 files changed

+65
-4
lines changed

Changelog.md

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Unreleased
22

33
- Update rustfmt's defaults to use 2021 edition ([#554](https://github.com/rust-lang/rust-mode/issues/509)).
4+
- Introduce `rust-format-mode` for `rust-format-buffer` ([#556](https://github.com/rust-lang/rust-mode/pull/556)).
45

56
# v1.0.6
67

rust-cargo-tests.el

+19
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
;;; rust-cargo-tests.el --- ERT tests for rust-cargo.el -*- lexical-binding: t; -*-
22
(require 'rust-cargo)
3+
(require 'rust-rustfmt)
34
(require 'ert)
45

56
(defun rust-test--wait-process-exit ()
@@ -51,3 +52,21 @@
5152
(rust-test--send-process-string "1234\n")
5253
(rust-test--wait-process-exit)
5354
(should (rust-test--find-string "***run interactive: 1234")))))
55+
56+
(ert-deftest rust-test-rustfmt ()
57+
(skip-unless (executable-find "rustfmt"))
58+
(rust-test--with-main-file-buffer
59+
(let ((old-content (buffer-string))
60+
(ret (rust-format-buffer)))
61+
(should (string= ret "Formatted buffer with rustfmt."))
62+
(should (string= old-content (buffer-string))))))
63+
64+
(ert-deftest rust-test-rustfmt-parsing-errors ()
65+
(skip-unless (executable-find "rustfmt"))
66+
(with-temp-buffer
67+
(insert "fn main() {")
68+
(rust-format-buffer)
69+
(with-current-buffer "*rustfmt*"
70+
(should (eq major-mode 'rust-format-mode))
71+
(should (rust-test--find-string "error:")))
72+
(kill-buffer "*rustfmt*")))

rust-rustfmt.el

+45-4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
;;; Code:
88
;;; Options
99

10+
(require 'rust-compile)
11+
(require 'compile)
12+
1013
(defcustom rust-format-on-save nil
1114
"Format future rust buffers before saving using rustfmt."
1215
:type 'boolean
@@ -39,6 +42,34 @@
3942

4043
(defconst rust-rustfmt-buffername "*rustfmt*")
4144

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+
4273
(defun rust--format-call (buf)
4374
"Format BUF using rustfmt."
4475
(let ((path exec-path))
@@ -69,10 +100,20 @@
69100
(with-current-buffer buf
70101
(replace-buffer-contents rust-rustfmt-buffername))
71102
(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))
76117
((= ret 3)
77118
(if (not (string= (buffer-string)
78119
(with-current-buffer buf (buffer-string))))

0 commit comments

Comments
 (0)