[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[nongnu] elpa/rust-mode ae49380 221/486: Merge pull request #121 from fb
From: |
ELPA Syncer |
Subject: |
[nongnu] elpa/rust-mode ae49380 221/486: Merge pull request #121 from fbergroth/integrate-rustfmt |
Date: |
Sat, 7 Aug 2021 09:25:23 -0400 (EDT) |
branch: elpa/rust-mode
commit ae4938076ee20c2636089f285465263bcbd9fd16
Merge: fa5b38f 86c55b1
Author: Micah Chalmer <micah@micahchalmer.net>
Commit: Micah Chalmer <micah@micahchalmer.net>
Merge pull request #121 from fbergroth/integrate-rustfmt
Integrate rustfmt support
---
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 5ed4675 171/486: Merge pull request #73 from MicahChalmer/raw-string-multiline-edit-fix, (continued)
- [nongnu] elpa/rust-mode 5ed4675 171/486: Merge pull request #73 from MicahChalmer/raw-string-multiline-edit-fix, ELPA Syncer, 2021/08/07
- [nongnu] elpa/rust-mode 9d773b4 170/486: Fix multi-line raw strings when editing, ELPA Syncer, 2021/08/07
- [nongnu] elpa/rust-mode 6bc1540 186/486: Support nested block comments, ELPA Syncer, 2021/08/07
- [nongnu] elpa/rust-mode 7baae9c 180/486: Merge pull request #78 from tromey/fix-issue-33, ELPA Syncer, 2021/08/07
- [nongnu] elpa/rust-mode c0e3878 183/486: Merge pull request #81 from MicahChalmer/fix-issue-80, ELPA Syncer, 2021/08/07
- [nongnu] elpa/rust-mode 75da3b0 189/486: Merge pull request #89 from MicahChalmer/fix-slow-angle-bracket-matching, ELPA Syncer, 2021/08/07
- [nongnu] elpa/rust-mode a2e9f56 199/486: Merge pull request #84 from birkenfeld/compilation-note, ELPA Syncer, 2021/08/07
- [nongnu] elpa/rust-mode 92584c3 209/486: Fix the special case for the first line, ELPA Syncer, 2021/08/07
- [nongnu] elpa/rust-mode b76e803 208/486: Correctly indent where clauses, ELPA Syncer, 2021/08/07
- [nongnu] elpa/rust-mode 304ae4b 219/486: Change font-lock face for module names., ELPA Syncer, 2021/08/07
- [nongnu] elpa/rust-mode ae49380 221/486: Merge pull request #121 from fbergroth/integrate-rustfmt,
ELPA Syncer <=
- [nongnu] elpa/rust-mode 315cc59 233/486: Re-indent on }, ELPA Syncer, 2021/08/07
- [nongnu] elpa/rust-mode bc77e16 240/486: remove emacs 23 support, ELPA Syncer, 2021/08/07
- [nongnu] elpa/rust-mode 620d718 257/486: Fix #160, ELPA Syncer, 2021/08/07
- [nongnu] elpa/rust-mode cffb950f2 258/486: [master] Remove redundant progn., ELPA Syncer, 2021/08/07
- [nongnu] elpa/rust-mode b3b0f78 259/486: Merge pull request #161 from Fanael/master, ELPA Syncer, 2021/08/07
- [nongnu] elpa/rust-mode ba5ff90 260/486: Merge pull request #159 from hotpxl/master, ELPA Syncer, 2021/08/07
- [nongnu] elpa/rust-mode f73f321 264/486: Merge pull request #163 from Wilfred/preserve_point_rustfmt, ELPA Syncer, 2021/08/07
- [nongnu] elpa/rust-mode cc59c83 270/486: Recognize imenu items starting with "unsafe", ELPA Syncer, 2021/08/07
- [nongnu] elpa/rust-mode fba7714 271/486: Handle comments when indenting method chains, ELPA Syncer, 2021/08/07
- [nongnu] elpa/rust-mode 7b32066 032/486: rust-mode.el uses the 'cl macros, so it should actually require them, ELPA Syncer, 2021/08/07