Skip to content

introduce a new compilation-mode for rust-format #556

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
19 changes: 19 additions & 0 deletions rust-cargo-tests.el
Original file line number Diff line number Diff line change
@@ -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 ()
Expand Down Expand Up @@ -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*")))
49 changes: 45 additions & 4 deletions rust-rustfmt.el
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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))))
Expand Down