summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Changelog.md1
-rw-r--r--rust-cargo-tests.el19
-rw-r--r--rust-rustfmt.el49
3 files changed, 65 insertions, 4 deletions
diff --git a/Changelog.md b/Changelog.md
index ea4dcb1..cadbbf5 100644
--- a/Changelog.md
+++ b/Changelog.md
@@ -1,6 +1,7 @@
# Unreleased
- Update rustfmt's defaults to use 2021 edition ([#554](https://github.com/rust-lang/rust-mode/issues/509)).
+- Introduce `rust-format-mode` for `rust-format-buffer` ([#556](https://github.com/rust-lang/rust-mode/pull/556)).
# v1.0.6
diff --git a/rust-cargo-tests.el b/rust-cargo-tests.el
index f49cba0..fe78ac7 100644
--- a/rust-cargo-tests.el
+++ b/rust-cargo-tests.el
@@ -1,5 +1,6 @@
;;; rust-cargo-tests.el --- ERT tests for rust-cargo.el -*- lexical-binding: t; -*-
(require 'rust-cargo)
+(require 'rust-rustfmt)
(require 'ert)
(defun rust-test--wait-process-exit ()
@@ -51,3 +52,21 @@
(rust-test--send-process-string "1234\n")
(rust-test--wait-process-exit)
(should (rust-test--find-string "***run interactive: 1234")))))
+
+(ert-deftest rust-test-rustfmt ()
+ (skip-unless (executable-find "rustfmt"))
+ (rust-test--with-main-file-buffer
+ (let ((old-content (buffer-string))
+ (ret (rust-format-buffer)))
+ (should (string= ret "Formatted buffer with rustfmt."))
+ (should (string= old-content (buffer-string))))))
+
+(ert-deftest rust-test-rustfmt-parsing-errors ()
+ (skip-unless (executable-find "rustfmt"))
+ (with-temp-buffer
+ (insert "fn main() {")
+ (rust-format-buffer)
+ (with-current-buffer "*rustfmt*"
+ (should (eq major-mode 'rust-format-mode))
+ (should (rust-test--find-string "error:")))
+ (kill-buffer "*rustfmt*")))
diff --git a/rust-rustfmt.el b/rust-rustfmt.el
index ba3dbe2..04dce45 100644
--- a/rust-rustfmt.el
+++ b/rust-rustfmt.el
@@ -7,6 +7,9 @@
;;; Code:
;;; Options
+(require 'rust-compile)
+(require 'compile)
+
(defcustom rust-format-on-save nil
"Format future rust buffers before saving using rustfmt."
:type 'boolean
@@ -39,6 +42,34 @@
(defconst rust-rustfmt-buffername "*rustfmt*")
+(define-compilation-mode rust-format-mode "rust-format"
+ "Major mode for Rust compilation output."
+
+ (setq-local compilation-error-regexp-alist-alist nil)
+ (add-to-list 'compilation-error-regexp-alist-alist
+ (cons 'rustc-refs rustc-refs-compilation-regexps))
+ (add-to-list 'compilation-error-regexp-alist-alist
+ (cons 'rustc rustc-compilation-regexps))
+ (add-to-list 'compilation-error-regexp-alist-alist
+ (cons 'rustc-colon rustc-colon-compilation-regexps))
+ (add-to-list 'compilation-error-regexp-alist-alist
+ (cons 'cargo cargo-compilation-regexps))
+ (add-to-list 'compilation-error-regexp-alist-alist
+ (cons 'rustc-panics rustc-panics-compilation-regexps))
+
+ (setq-local compilation-error-regexp-alist nil)
+ (add-to-list 'compilation-error-regexp-alist 'rustc-refs)
+ (add-to-list 'compilation-error-regexp-alist 'rustc)
+ (add-to-list 'compilation-error-regexp-alist 'rustc-colon)
+ (add-to-list 'compilation-error-regexp-alist 'cargo)
+ (add-to-list 'compilation-error-regexp-alist 'rustc-panics)
+
+ (add-hook 'next-error-hook #'rustc-scroll-down-after-next-error)
+
+ (if (or compilation-auto-jump-to-first-error
+ (eq compilation-scroll-output 'first-error))
+ (set (make-local-variable 'compilation-auto-jump-to-next) t)))
+
(defun rust--format-call (buf)
"Format BUF using rustfmt."
(let ((path exec-path))
@@ -69,10 +100,20 @@
(with-current-buffer buf
(replace-buffer-contents rust-rustfmt-buffername))
(copy-to-buffer buf (point-min) (point-max))))
- (let ((win (get-buffer-window rust-rustfmt-buffername)))
- (if win
- (quit-window t win)
- (kill-buffer rust-rustfmt-buffername))))
+ (let ((win (get-buffer-window rust-rustfmt-buffername)))
+ (if win
+ (quit-window t win)
+ (kill-buffer rust-rustfmt-buffername))))
+ ((= ret 1)
+ (erase-buffer)
+ (insert-file-contents tmpf)
+
+ (rust-format-mode) ;; render compilation errors in compilation-mode
+ (setq-local compile-command (format "%s %s" rust-rustfmt-bin (buffer-file-name buf)))
+
+ (rust--format-fix-rustfmt-buffer (buffer-name buf))
+ (error "Rustfmt failed because of parsing errors, see %s buffer for details"
+ rust-rustfmt-buffername))
((= ret 3)
(if (not (string= (buffer-string)
(with-current-buffer buf (buffer-string))))