Skip to content

Commit 0985f5f

Browse files
authored
Merge pull request #217 from Aankhen/add-clippy-command
Add `rust-run-clippy' and `rust-buffer-project' with testing paraphernalia
2 parents 60a1f36 + 128601b commit 0985f5f

File tree

3 files changed

+54
-1
lines changed

3 files changed

+54
-1
lines changed

rust-mode-tests.el

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2650,6 +2650,13 @@ extern \"rust-intrinsic\" fn five() {
26502650
"four"
26512651
"five"))))
26522652

2653+
(when (executable-find rust-cargo-bin)
2654+
(ert-deftest rust-test-project-located ()
2655+
(lexical-let* ((test-dir (expand-file-name "test-project" default-directory))
2656+
(manifest-file (expand-file-name "Cargo.toml" test-dir)))
2657+
(let ((default-directory test-dir))
2658+
(should (equal (expand-file-name (rust-buffer-project)) manifest-file))))))
2659+
26532660
;; If electric-pair-mode is available, load it and run the tests that use it. If not,
26542661
;; no error--the tests will be skipped.
26552662
(require 'elec-pair nil t)

rust-mode.el

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,14 @@
1818
(require 'compile)
1919
(require 'url-vars))
2020

21+
(require 'json)
22+
2123
(defvar electric-pair-inhibit-predicate)
2224
(defvar electric-indent-chars)
2325

26+
(defvar rust-buffer-project)
27+
(make-variable-buffer-local 'rust-buffer-project)
28+
2429
;; for GNU Emacs < 24.3
2530
(eval-when-compile
2631
(unless (fboundp 'setq-local)
@@ -144,6 +149,17 @@ function or trait. When nil, where will be aligned with fn or trait."
144149
:type 'string
145150
:group 'rust-mode)
146151

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+
147163
(defface rust-unsafe-face
148164
'((t :inherit font-lock-warning-face))
149165
"Face for the `unsafe' keyword."
@@ -1411,7 +1427,12 @@ This is written mainly to be used as `end-of-defun-function' for Rust."
14111427

14121428
(setq-local compile-command "cargo build")
14131429

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)))
14151436

14161437
;;;###autoload
14171438
(add-to-list 'auto-mode-alist '("\\.rs\\'" . rust-mode))
@@ -1548,6 +1569,30 @@ visit the new file."
15481569
(rename-file filename new-name 1)
15491570
(set-visited-file-name new-name))))))
15501571

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+
15511596
(provide 'rust-mode)
15521597

15531598
;;; rust-mode.el ends here

test-project/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Dummy file needed for test

0 commit comments

Comments
 (0)