[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[nongnu] elpa/rust-mode 86c55b1 220/486: Integrate rustfmt support
From: |
ELPA Syncer |
Subject: |
[nongnu] elpa/rust-mode 86c55b1 220/486: Integrate rustfmt support |
Date: |
Sat, 7 Aug 2021 09:25:22 -0400 (EDT) |
branch: elpa/rust-mode
commit 86c55b1f34e820b16a812bd2145878cf5ed56ce4
Author: Fredrik Bergroth <fbergroth@gmail.com>
Commit: Fredrik Bergroth <fbergroth@gmail.com>
Integrate rustfmt support
Merges the rustfmt package (https://github.com/fbergroth/rust-mode)
Fixes #120.
---
rust-mode.el | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++--
1 file changed, 55 insertions(+), 2 deletions(-)
diff --git a/rust-mode.el b/rust-mode.el
index a1a3db4..759e586 100644
--- a/rust-mode.el
+++ b/rust-mode.el
@@ -181,6 +181,15 @@ function or trait. When nil, where will be aligned with
fn or trait."
:safe #'booleanp
:group 'rust-mode)
+(defcustom rust-format-on-save nil
+ "Format future rust buffers before saving using rustfmt."
+ :type 'boolean
+ :safe #'booleanp)
+
+(defcustom rust-rustfmt-bin "rustfmt"
+ "Path to rustfmt executable."
+ :type 'string)
+
(defface rust-unsafe-face
'((t :inherit font-lock-warning-face))
"Face for the `unsafe' keyword."
@@ -1211,13 +1220,55 @@ This is written mainly to be used as
`end-of-defun-function' for Rust."
;; There is no opening brace, so consider the whole buffer to be one
"defun"
(goto-char (point-max))))
+;; Formatting using rustfmt
+(defun rust--format-call (buf)
+ "Format BUF using rustfmt."
+ (with-current-buffer (get-buffer-create "*rustfmt*")
+ (erase-buffer)
+ (insert-buffer-substring buf)
+ (if (zerop (call-process-region (point-min) (point-max) rust-rustfmt-bin t
t nil))
+ (progn (copy-to-buffer buf (point-min) (point-max))
+ (kill-buffer))
+ (error "Rustfmt failed, see *rustfmt* buffer for details"))))
+
+(defun rust-format-buffer ()
+ "Format the current buffer using rustfmt."
+ (interactive)
+ (unless (executable-find rust-rustfmt-bin)
+ (error "Could not locate executable \"%s\"" rust-rustfmt-bin))
+
+ (let ((cur-point (point))
+ (cur-win-start (window-start)))
+ (rust--format-call (current-buffer))
+ (goto-char cur-point)
+ (set-window-start (selected-window) cur-win-start))
+ (message "Formatted buffer with rustfmt."))
+
+(defun rust-enable-format-on-save ()
+ "Enable formatting using rustfmt when saving buffer."
+ (interactive)
+ (add-hook 'before-save-hook #'rust-format-buffer nil t))
+
+(defun rust-disable-format-on-save ()
+ "Disable formatting using rustfmt when saving buffer."
+ (interactive)
+ (remove-hook 'before-save-hook #'rust-format-buffer t))
+
;; For compatibility with Emacs < 24, derive conditionally
(defalias 'rust-parent-mode
(if (fboundp 'prog-mode) 'prog-mode 'fundamental-mode))
+(defvar rust-mode-map
+ (let ((map (make-sparse-keymap)))
+ (define-key map (kbd "C-c C-f") 'rust-format-buffer)
+ map)
+ "Keymap for Rust major mode.")
+
;;;###autoload
(define-derived-mode rust-mode rust-parent-mode "Rust"
- "Major mode for Rust code."
+ "Major mode for Rust code.
+
+\\{rust-mode-map}"
:group 'rust-mode
:syntax-table rust-mode-syntax-table
@@ -1255,7 +1306,9 @@ This is written mainly to be used as
`end-of-defun-function' for Rust."
(setq-local parse-sexp-lookup-properties t)
(setq-local electric-pair-inhibit-predicate
'rust-electric-pair-inhibit-predicate-wrap)
(add-hook 'after-revert-hook 'rust--after-revert-hook 'LOCAL)
- )
+
+ (when rust-format-on-save
+ (rust-enable-format-on-save)))
;;;###autoload
(add-to-list 'auto-mode-alist '("\\.rs\\'" . rust-mode))
- [nongnu] elpa/rust-mode 99b128c 187/486: Fix slowness in angle bracket matching, (continued)
- [nongnu] elpa/rust-mode 99b128c 187/486: Fix slowness in angle bracket matching, ELPA Syncer, 2021/08/07
- [nongnu] elpa/rust-mode 5e77aed 191/486: Correcting highlighting of capitals in function names., ELPA Syncer, 2021/08/07
- [nongnu] elpa/rust-mode 5e51aaa 194/486: Merge pull request #69 from tromey/safe-local-variables, ELPA Syncer, 2021/08/07
- [nongnu] elpa/rust-mode 012537b 195/486: remove byte-compiler warnings and prevent future ones, ELPA Syncer, 2021/08/07
- [nongnu] elpa/rust-mode 866df37 196/486: make rust-mode use lexical binding, ELPA Syncer, 2021/08/07
- [nongnu] elpa/rust-mode 6252cf1 217/486: Fix indentation of closing delimiters, ELPA Syncer, 2021/08/07
- [nongnu] elpa/rust-mode dd6d417 215/486: Fix type annotations incorrectly highlighted as modules., ELPA Syncer, 2021/08/07
- [nongnu] elpa/rust-mode 86d636a 223/486: Remove duplicate keywords, ELPA Syncer, 2021/08/07
- [nongnu] elpa/rust-mode b1cca0f 222/486: Merge pull request #72 from birkenfeld/builtin-face, ELPA Syncer, 2021/08/07
- [nongnu] elpa/rust-mode 061e6d8 214/486: Merge pull request #109 from mrBliss/highlight-unsafe, ELPA Syncer, 2021/08/07
- [nongnu] elpa/rust-mode 86c55b1 220/486: Integrate rustfmt support,
ELPA Syncer <=
- [nongnu] elpa/rust-mode beec95a 231/486: Allow disabling rustfmt with a dir-local/file-local variable, ELPA Syncer, 2021/08/07
- [nongnu] elpa/rust-mode 6739dd9 226/486: Merge pull request #123 from birkenfeld/fix-unicode-char-escapes, ELPA Syncer, 2021/08/07
- [nongnu] elpa/rust-mode f408b06 227/486: Run the after-revert hook after rustfmt, ELPA Syncer, 2021/08/07
- [nongnu] elpa/rust-mode bc0df03 235/486: Merge pull request #135 from Stebalien/file-local, ELPA Syncer, 2021/08/07
- [nongnu] elpa/rust-mode 9812232 236/486: Document how to install on Debian, ELPA Syncer, 2021/08/07
- [nongnu] elpa/rust-mode bfe24d2 237/486: Merge pull request #132 from tomjakubowski/move-module, ELPA Syncer, 2021/08/07
- [nongnu] elpa/rust-mode 1c37249 238/486: Merge pull request #136 from Stebalien/indent-brace, ELPA Syncer, 2021/08/07
- [nongnu] elpa/rust-mode bd44088 239/486: Merge pull request #138 from kraai/upstream, ELPA Syncer, 2021/08/07
- [nongnu] elpa/rust-mode 18fad39 242/486: Merge pull request #142 from tromey/remove-emacs-23-support, ELPA Syncer, 2021/08/07
- [nongnu] elpa/rust-mode eafb7a0 248/486: Properly fix #151, ELPA Syncer, 2021/08/07