|
18 | 18 | (require 'compile)
|
19 | 19 | (require 'url-vars))
|
20 | 20 |
|
| 21 | +(require 'json) |
| 22 | + |
21 | 23 | (defvar electric-pair-inhibit-predicate)
|
22 | 24 | (defvar electric-indent-chars)
|
23 | 25 |
|
| 26 | +(defvar rust-buffer-project) |
| 27 | +(make-variable-buffer-local 'rust-buffer-project) |
| 28 | + |
24 | 29 | ;; for GNU Emacs < 24.3
|
25 | 30 | (eval-when-compile
|
26 | 31 | (unless (fboundp 'setq-local)
|
@@ -144,6 +149,17 @@ function or trait. When nil, where will be aligned with fn or trait."
|
144 | 149 | :type 'string
|
145 | 150 | :group 'rust-mode)
|
146 | 151 |
|
| 152 | +(defcustom rust-cargo-bin "cargo" |
| 153 | + "Path to cargo executable." |
| 154 | + :type 'string |
| 155 | + :group 'rust-mode) |
| 156 | + |
| 157 | +(defcustom rust-always-locate-project-on-open nil |
| 158 | + "Whether to run `cargo locate-project' every time `rust-mode' |
| 159 | + is activated." |
| 160 | + :type 'boolean |
| 161 | + :group 'rust-mode) |
| 162 | + |
147 | 163 | (defface rust-unsafe-face
|
148 | 164 | '((t :inherit font-lock-warning-face))
|
149 | 165 | "Face for the `unsafe' keyword."
|
@@ -1411,7 +1427,12 @@ This is written mainly to be used as `end-of-defun-function' for Rust."
|
1411 | 1427 |
|
1412 | 1428 | (setq-local compile-command "cargo build")
|
1413 | 1429 |
|
1414 |
| - (add-hook 'before-save-hook 'rust--before-save-hook nil t)) |
| 1430 | + (add-hook 'before-save-hook 'rust--before-save-hook nil t) |
| 1431 | + |
| 1432 | + (setq-local rust-buffer-project nil) |
| 1433 | + |
| 1434 | + (when rust-always-locate-project-on-open |
| 1435 | + (rust-update-buffer-project))) |
1415 | 1436 |
|
1416 | 1437 | ;;;###autoload
|
1417 | 1438 | (add-to-list 'auto-mode-alist '("\\.rs\\'" . rust-mode))
|
@@ -1548,6 +1569,30 @@ visit the new file."
|
1548 | 1569 | (rename-file filename new-name 1)
|
1549 | 1570 | (set-visited-file-name new-name))))))
|
1550 | 1571 |
|
| 1572 | +(defun rust-run-clippy () |
| 1573 | + "Run `cargo clippy'." |
| 1574 | + (interactive) |
| 1575 | + (when (null rust-buffer-project) |
| 1576 | + (rust-update-buffer-project)) |
| 1577 | + (let* ((args (list rust-cargo-bin "clippy" (concat "--manifest-path=" rust-buffer-project))) |
| 1578 | + ;; set `compile-command' temporarily so `compile' doesn't |
| 1579 | + ;; clobber the existing value |
| 1580 | + (compile-command (mapconcat #'shell-quote-argument args " "))) |
| 1581 | + (compile compile-command))) |
| 1582 | + |
| 1583 | +(defun rust-update-buffer-project () |
| 1584 | + (setq-local rust-buffer-project (rust-buffer-project))) |
| 1585 | + |
| 1586 | +(defun rust-buffer-project () |
| 1587 | + "Get project root if possible." |
| 1588 | + (with-temp-buffer |
| 1589 | + (let ((ret (call-process rust-cargo-bin nil t nil "locate-project"))) |
| 1590 | + (when (/= ret 0) |
| 1591 | + (error "`cargo locate-project' returned %s status: %s" ret (buffer-string))) |
| 1592 | + (goto-char 0) |
| 1593 | + (let ((output (json-read))) |
| 1594 | + (cdr (assoc-string "root" output)))))) |
| 1595 | + |
1551 | 1596 | (provide 'rust-mode)
|
1552 | 1597 |
|
1553 | 1598 | ;;; rust-mode.el ends here
|
0 commit comments