-
Notifications
You must be signed in to change notification settings - Fork 3
/
rustfmt.el
87 lines (70 loc) · 2.79 KB
/
rustfmt.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
;;; rustfmt.el --- Format rust code using rustfmt -*- lexical-binding: t; -*-
;; Copyright (C) 2015 Fredrik Bergroth
;; Author: Fredrik Bergroth <[email protected]>
;; URL: https://github.com/fbergroth/emacs-rustfmt
;; Keywords: convenience
;; Version: 0
;; Package-Requires: ((emacs "24"))
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; *** OBSOLETE PACKAGE - This has been merged into rust-mode ***
;; Call `rustfmt-format-buffer' to format the current buffer using rustfmt. It is
;; convenient to bind it to a key, such as:
;;
;; (define-key rust-mode-map (kbd "C-c C-f") #'rustfmt-format-buffer)
;;
;; Alternatively, run rustfmt before saving rust buffers:
;;
;; (add-hook 'rust-mode-hook #'rustfmt-enable-on-save)
;;
;; Errors and warnings will be visible in the `*rustfmt*' buffer.
;;; Code:
(defgroup rustfmt nil
"Format rust buffers using rustfmt."
:group 'convenience
:prefix "rustfmt-")
(defcustom rustfmt-bin "rustfmt"
"Path to rustfmt executable."
:type 'string)
(defcustom rustfmt-popup-errors nil
"Display error buffer when rustfmt fails."
:type 'boolean)
(defun rustfmt--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) rustfmt-bin t t nil))
(progn (copy-to-buffer buf (point-min) (point-max))
(kill-buffer))
(when rustfmt-popup-errors
(display-buffer (current-buffer)))
(error "Rustfmt failed, see *rustfmt* buffer for details"))))
;;;###autoload
(defun rustfmt-format-buffer ()
"Format the current buffer using rustfmt."
(interactive)
(unless (executable-find rustfmt-bin)
(error "Could not locate executable \"%s\"" rustfmt-bin))
(let ((cur-point (point))
(cur-win-start (window-start)))
(rustfmt--call (current-buffer))
(goto-char cur-point)
(set-window-start (selected-window) cur-win-start))
(message "Formatted buffer with rustfmt."))
;;;###autoload
(defun rustfmt-enable-on-save ()
"Run rustfmt when saving buffer."
(interactive)
(add-hook 'before-save-hook #'rustfmt-format-buffer nil t))
(provide 'rustfmt)
;;; rustfmt.el ends here