@@ -144,6 +144,12 @@ function or trait. When nil, where will be aligned with fn or trait."
144
144
:type 'string
145
145
:group 'rust-mode )
146
146
147
+ (defcustom rust-always-locate-project-on-open nil
148
+ " Whether to run `cargo locate-project' every time `rust-mode'
149
+ is activated."
150
+ :type 'boolean
151
+ :group 'rust-mode )
152
+
147
153
(defface rust-unsafe-face
148
154
'((t :inherit font-lock-warning-face ))
149
155
" Face for the `unsafe' keyword."
@@ -1411,7 +1417,12 @@ This is written mainly to be used as `end-of-defun-function' for Rust."
1411
1417
1412
1418
(setq-local compile-command " cargo build" )
1413
1419
1414
- (add-hook 'before-save-hook 'rust--before-save-hook nil t ))
1420
+ (add-hook 'before-save-hook 'rust--before-save-hook nil t )
1421
+
1422
+ (setq-local rust-buffer-project nil )
1423
+
1424
+ (when rust-always-locate-project-on-open
1425
+ (rust-update-buffer-project)))
1415
1426
1416
1427
;;;### autoload
1417
1428
(add-to-list 'auto-mode-alist '(" \\ .rs\\ '" . rust-mode))
@@ -1548,6 +1559,30 @@ visit the new file."
1548
1559
(rename-file filename new-name 1 )
1549
1560
(set-visited-file-name new-name))))))
1550
1561
1562
+ (defun rust-run-clippy ()
1563
+ " Run `cargo clippy'."
1564
+ (interactive )
1565
+ (when (null rust-buffer-project)
1566
+ (rust-update-buffer-project))
1567
+ (let* ((args (list " cargo" " clippy" (concat " --manifest-path=" rust-buffer-project)))
1568
+ ; ; set `compile-command' temporarily so `compile' doesn't
1569
+ ; ; clobber the existing value
1570
+ (compile-command (mapconcat #'shell-quote-argument args " " )))
1571
+ (compile compile-command)))
1572
+
1573
+ (defun rust-update-buffer-project ()
1574
+ (setq-local rust-buffer-project (rust-buffer-project)))
1575
+
1576
+ (defun rust-buffer-project ()
1577
+ " Get project root if possible."
1578
+ (with-temp-buffer
1579
+ (let ((ret (call-process " cargo" nil t nil " locate-project" )))
1580
+ (when (/= ret 0 )
1581
+ (error " `cargo locate-project' returned %s status: %s " ret (buffer-string )))
1582
+ (goto-char 0 )
1583
+ (let ((output (json-read )))
1584
+ (cdr (assoc-string " root" output))))))
1585
+
1551
1586
(provide 'rust-mode )
1552
1587
1553
1588
; ;; rust-mode.el ends here
0 commit comments