Skip to content

Commit 649f492

Browse files
tarsiusbrotzeit
authored andcommitted
Create rust-cargo.el from existing code
1 parent d548425 commit 649f492

File tree

3 files changed

+105
-87
lines changed

3 files changed

+105
-87
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ EMACS ?= emacs
1010
EMACS_ARGS ?=
1111

1212
ELS = rust-mode.el
13+
ELS += rust-cargo.el
1314
ELS += rust-compile.el
1415
ELS += rust-playpen.el
1516
ELS += rust-rustfmt.el

rust-cargo.el

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
;;; rust-cargo.el --- Support for cargo -*-lexical-binding: t-*-
2+
;;; Commentary:
3+
4+
;; This library implements support for running `cargo'.
5+
6+
;;; Code:
7+
8+
(require 'json)
9+
10+
;;; Options
11+
12+
(defcustom rust-cargo-bin "cargo"
13+
"Path to cargo executable."
14+
:type 'string
15+
:group 'rust-mode)
16+
17+
(defcustom rust-always-locate-project-on-open nil
18+
"Whether to run `cargo locate-project' every time `rust-mode' is activated."
19+
:type 'boolean
20+
:group 'rust-mode)
21+
22+
;;; Buffer Project
23+
24+
(defvar-local rust-buffer-project nil)
25+
26+
(defun rust-buffer-project ()
27+
"Get project root if possible."
28+
(with-temp-buffer
29+
(let ((ret (call-process rust-cargo-bin nil t nil "locate-project")))
30+
(when (/= ret 0)
31+
(error "`cargo locate-project' returned %s status: %s" ret (buffer-string)))
32+
(goto-char 0)
33+
(let ((output (json-read)))
34+
(cdr (assoc-string "root" output))))))
35+
36+
(defun rust-update-buffer-project ()
37+
(setq-local rust-buffer-project (rust-buffer-project)))
38+
39+
(defun rust-maybe-initialize-buffer-project ()
40+
(setq-local rust-buffer-project nil)
41+
(when rust-always-locate-project-on-open
42+
(rust-update-buffer-project)))
43+
44+
(add-hook 'rust-mode-hook 'rust-maybe-initialize-buffer-project)
45+
46+
;;; Internal
47+
48+
(defun rust--compile (format-string &rest args)
49+
(when (null rust-buffer-project)
50+
(rust-update-buffer-project))
51+
(let ((default-directory
52+
(or (and rust-buffer-project
53+
(file-name-directory rust-buffer-project))
54+
default-directory)))
55+
(compile (apply #'format format-string args))))
56+
57+
;;; Commands
58+
59+
(defun rust-check ()
60+
"Compile using `cargo check`"
61+
(interactive)
62+
(rust--compile "%s check" rust-cargo-bin))
63+
64+
(defun rust-compile ()
65+
"Compile using `cargo build`"
66+
(interactive)
67+
(rust--compile "%s build" rust-cargo-bin))
68+
69+
(defun rust-compile-release ()
70+
"Compile using `cargo build --release`"
71+
(interactive)
72+
(rust--compile "%s build --release" rust-cargo-bin))
73+
74+
(defun rust-run ()
75+
"Run using `cargo run`"
76+
(interactive)
77+
(rust--compile "%s run" rust-cargo-bin))
78+
79+
(defun rust-run-release ()
80+
"Run using `cargo run --release`"
81+
(interactive)
82+
(rust--compile "%s run --release" rust-cargo-bin))
83+
84+
(defun rust-test ()
85+
"Test using `cargo test`"
86+
(interactive)
87+
(rust--compile "%s test" rust-cargo-bin))
88+
89+
(defun rust-run-clippy ()
90+
"Run `cargo clippy'."
91+
(interactive)
92+
(when (null rust-buffer-project)
93+
(rust-update-buffer-project))
94+
(let* ((args (list rust-cargo-bin "clippy"
95+
(concat "--manifest-path=" rust-buffer-project)))
96+
;; set `compile-command' temporarily so `compile' doesn't
97+
;; clobber the existing value
98+
(compile-command (mapconcat #'shell-quote-argument args " ")))
99+
(rust--compile compile-command)))
100+
101+
;;; _
102+
(provide 'rust-cargo)
103+
;;; rust-cargo.el ends here

rust-mode.el

Lines changed: 1 addition & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,12 @@
1717

1818
(eval-when-compile (require 'rx))
1919

20-
(require 'json)
2120
(require 'thingatpt)
2221

2322
(defvar electric-pair-inhibit-predicate)
2423
(defvar electric-pair-skip-self)
2524
(defvar electric-indent-chars)
2625

27-
(defvar rust-buffer-project nil)
28-
(make-variable-buffer-local 'rust-buffer-project)
29-
3026
;;; Customization
3127

3228
(defgroup rust-mode nil
@@ -59,16 +55,6 @@ When nil, `where' will be aligned with `fn' or `trait'."
5955
:safe #'booleanp
6056
:group 'rust-mode)
6157

62-
(defcustom rust-cargo-bin "cargo"
63-
"Path to cargo executable."
64-
:type 'string
65-
:group 'rust-mode)
66-
67-
(defcustom rust-always-locate-project-on-open nil
68-
"Whether to run `cargo locate-project' every time `rust-mode' is activated."
69-
:type 'boolean
70-
:group 'rust-mode)
71-
7258
(defcustom rust-indent-return-type-to-arguments t
7359
"Indent a line starting with the `->' (RArrow) following a function, aligning
7460
to the function arguments. When nil, `->' will be indented one level."
@@ -266,11 +252,7 @@ Use idomenu (imenu with `ido-mode') for best mileage.")
266252

267253
(add-hook 'before-save-hook 'rust-before-save-hook nil t)
268254
(add-hook 'after-save-hook 'rust-after-save-hook nil t)
269-
270-
(setq-local rust-buffer-project nil)
271-
272-
(when rust-always-locate-project-on-open
273-
(rust-update-buffer-project)))
255+
)
274256

275257
;;;###autoload
276258
(add-to-list 'auto-mode-alist '("\\.rs\\'" . rust-mode))
@@ -1569,45 +1551,6 @@ This is written mainly to be used as `end-of-defun-function' for Rust."
15691551
;; There is no opening brace, so consider the whole buffer to be one "defun"
15701552
(goto-char (point-max))))
15711553

1572-
(defun rust--compile (format-string &rest args)
1573-
(when (null rust-buffer-project)
1574-
(rust-update-buffer-project))
1575-
(let ((default-directory
1576-
(or (and rust-buffer-project
1577-
(file-name-directory rust-buffer-project))
1578-
default-directory)))
1579-
(compile (apply #'format format-string args))))
1580-
1581-
(defun rust-check ()
1582-
"Compile using `cargo check`"
1583-
(interactive)
1584-
(rust--compile "%s check" rust-cargo-bin))
1585-
1586-
(defun rust-compile ()
1587-
"Compile using `cargo build`"
1588-
(interactive)
1589-
(rust--compile "%s build" rust-cargo-bin))
1590-
1591-
(defun rust-compile-release ()
1592-
"Compile using `cargo build --release`"
1593-
(interactive)
1594-
(rust--compile "%s build --release" rust-cargo-bin))
1595-
1596-
(defun rust-run ()
1597-
"Run using `cargo run`"
1598-
(interactive)
1599-
(rust--compile "%s run" rust-cargo-bin))
1600-
1601-
(defun rust-run-release ()
1602-
"Run using `cargo run --release`"
1603-
(interactive)
1604-
(rust--compile "%s run --release" rust-cargo-bin))
1605-
1606-
(defun rust-test ()
1607-
"Test using `cargo test`"
1608-
(interactive)
1609-
(rust--compile "%s test" rust-cargo-bin))
1610-
16111554
;;; Secondary Commands
16121555

16131556
(defun rust-promote-module-into-dir ()
@@ -1632,35 +1575,6 @@ visit the new file."
16321575
(rename-file filename new-name 1)
16331576
(set-visited-file-name new-name))))))
16341577

1635-
(defun rust-run-clippy ()
1636-
"Run `cargo clippy'."
1637-
(interactive)
1638-
(when (null rust-buffer-project)
1639-
(rust-update-buffer-project))
1640-
(let* ((args (list rust-cargo-bin "clippy"
1641-
(concat "--manifest-path=" rust-buffer-project)))
1642-
;; set `compile-command' temporarily so `compile' doesn't
1643-
;; clobber the existing value
1644-
(compile-command (mapconcat #'shell-quote-argument args " ")))
1645-
(rust--compile compile-command)))
1646-
1647-
;;; Utilities
1648-
1649-
(defun rust-update-buffer-project ()
1650-
(setq-local rust-buffer-project (rust-buffer-project)))
1651-
1652-
(defun rust-buffer-project ()
1653-
"Get project root if possible."
1654-
(with-temp-buffer
1655-
(let ((ret (call-process rust-cargo-bin nil t nil "locate-project")))
1656-
(when (/= ret 0)
1657-
(error "`cargo locate-project' returned %s status: %s" ret (buffer-string)))
1658-
(goto-char 0)
1659-
(let ((output (json-read)))
1660-
(cdr (assoc-string "root" output))))))
1661-
1662-
;;; Secondary Commands
1663-
16641578
(defun rust-insert-dbg ()
16651579
"Insert the dbg! macro."
16661580
(cond ((region-active-p)

0 commit comments

Comments
 (0)