forked from kwrooijen/cargo.el
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cargo-process.el
884 lines (766 loc) · 33.8 KB
/
cargo-process.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
;;; cargo-process.el --- Cargo Process Major Mode -*-lexical-binding: t-*-
;; Copyright (C) 2021 Kevin W. van Rooijen
;; Author: Kevin W. van Rooijen
;; Keywords: processes, tools
;; 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:
;; Cargo Process Major mode.
;; Used to run Cargo background processes.
;; Current supported Cargo functions:
;; * cargo-process-bench - Run the benchmarks.
;; * cargo-process-build - Compile the current project.
;; * cargo-process-clean - Remove the target directory.
;; * cargo-process-doc - Build this project's and its dependencies' documentation.
;; * cargo-process-doc-open - Open this project's documentation.
;; * cargo-process-new - Create a new cargo project.
;; * cargo-process-init - Create a new cargo project inside an existing directory.
;; * cargo-process-run - Build and execute src/main.rs.
;; * cargo-process-run-example - Build and execute with --example <name>.
;; * cargo-process-run-bin - Build and execute a specific binary.
;; * cargo-process-search - Search registry for crates.
;; * cargo-process-test - Run all unit tests.
;; * cargo-process-update - Update dependencies listed in Cargo.lock.
;; * cargo-process-repeat - Run the last cargo-process command.
;; * cargo-process-current-test - Run the current unit test.
;; * cargo-process-current-file-tests - Run the current file unit tests.
;; * cargo-process-fmt - Run the optional cargo command fmt.
;; * cargo-process-check - Run the optional cargo command check.
;; * cargo-process-clippy - Run the optional cargo command clippy.
;; * cargo-process-add - Run the optional cargo command add.
;; * cargo-process-rm - Run the optional cargo command rm.
;; * cargo-process-upgrade - Run the optional cargo command upgrade.
;; * cargo-process-outdated - Run the optional cargo command outdated.
;; * cargo-process-audit - Run the optional cargo command audit.
;; * cargo-process-script - Run the optional cargo command script.
;; * cargo-process-watch - Run the optional cargo command watch, build by default
;; * cargo-process-watch-run - Run the optional cargo command watch, run by default
;;
;;; Code:
(require 'compile)
(require 'button)
(require 'markdown-mode)
(require 'tramp)
(defgroup cargo-process nil
"Cargo Process group."
:prefix "cargo-process-"
:group 'cargo)
(defcustom cargo-process--custom-path-to-bin
(or (executable-find "cargo")
(expand-file-name "cargo" "~/.cargo/bin")
"/usr/local/bin/cargo")
"Custom path to the cargo executable"
:type 'file)
(defcustom cargo-process--rustc-cmd
(or (executable-find "rustc")
(expand-file-name "rustc" "~/.cargo/bin")
"/usr/local/bin/rustc")
"Custom path to the rustc executable"
:type 'file)
(defcustom cargo-process--enable-rust-backtrace nil
"Set RUST_BACKTRACE environment variable to 1 for tasks test and run"
:type 'boolean)
(defcustom cargo-process--command-flags ""
"Flags to be added to every cargo command when run."
:type 'string)
(defcustom cargo-process--open-file-after-new nil
"Open the created project file after generating a new project"
:type 'boolean)
(defvar cargo-process-mode-map
(nconc (make-sparse-keymap) compilation-mode-map)
"Keymap for Cargo major mode.")
(defvar cargo-process--no-manifest-commands
'("New" "Init" "Install" "Search" "Fmt" "Audit" "Watch")
"These commands should not specify a manifest file.")
(defvar cargo-process-last-command nil "Command used last for repeating.")
(make-variable-buffer-local 'cargo-process-last-command)
(defcustom cargo-process--command-bench "bench"
"Subcommand used by `cargo-process-bench'."
:type 'string)
(defcustom cargo-process--command-build "build"
"Subcommand used by `cargo-process-build'."
:type 'string)
(defcustom cargo-process--command-clean "clean"
"Subcommand used by `cargo-process-clean'."
:type 'string)
(defcustom cargo-process--command-doc "doc"
"Subcommand used by `cargo-process-doc'."
:type 'string)
(defcustom cargo-process--command-doc-open "doc --open"
"Subcommand used by `cargo-process-doc'."
:type 'string)
(defcustom cargo-process--command-new "new"
"Subcommand used by `cargo-process-new'."
:type 'string)
(defcustom cargo-process--command-init "init"
"Subcommand used by `cargo-process-init'."
:type 'string)
(defcustom cargo-process--command-run "run"
"Subcommand used by `cargo-process-run'."
:type 'string)
(defcustom cargo-process--command-run-bin "run --bin"
"Subcommand used by `cargo-process-run-bin'."
:type 'string)
(defcustom cargo-process--command-run-example "run --example"
"Subcommand used by `cargo-process-run-example'."
:type 'string)
(defcustom cargo-process--command-search "search"
"Subcommand used by `cargo-process-search'."
:type 'string)
(defcustom cargo-process--command-test "test"
"Subcommand used by `cargo-process-test'."
:type 'string)
(defcustom cargo-process--command-current-test "test"
"Subcommand used by `cargo-process-current-test'."
:type 'string)
(defcustom cargo-process--command-current-file-tests "test"
"Subcommand used by `cargo-process-current-file-tests'."
:type 'string)
(defcustom cargo-process--command-test--additional-args nil
"Subcommand used by `cargo-process--command-test', `cargo-process--command-current-test` and `cargo-process--command-current-file-tests`."
:type 'string)
(defcustom cargo-process--command-update "update"
"Subcommand used by `cargo-process-update'."
:type 'string)
(defcustom cargo-process--command-fmt "fmt"
"Subcommand used by `cargo-process-fmt'."
:type 'string)
(defcustom cargo-process--command-outdated "outdated -R"
"Subcommand used by `cargo-process-outdated'."
:type 'string)
(defcustom cargo-process--command-check "check"
"Subcommand used by `cargo-process-check'."
:type 'string)
(defcustom cargo-process--command-clippy "clippy"
"Subcommand used by `cargo-process-clippy'."
:type 'string)
(defcustom cargo-process--command-clippy--additional-args nil
"Subcommand used by `cargo-process-clippy'.
Changing `cargo-process--command-clippy' use this when trying to customize
the clippy command so the manifest path is in the correct position."
:type 'string)
(defcustom cargo-process--command-add "add"
"Subcommand used by `cargo-process-add'."
:type 'string)
(defcustom cargo-process--command-rm "rm"
"Subcommand used by `cargo-process-rm'."
:type 'string)
(defcustom cargo-process--command-upgrade "upgrade"
"Subcommand used by `cargo-process-upgrade'."
:type 'string)
(defcustom cargo-process--command-audit "audit -f"
"Subcommand used by `cargo-process-audit'."
:type 'string)
(defcustom cargo-process--command-script "script"
"Subcommand used by `cargo-process-script'."
:type 'string)
(defcustom cargo-process--command-watch "watch -x build"
"Subcommand used by `cargo-process-watch'."
:type 'string)
(defcustom cargo-process--command-watch-run "watch -x run"
"Subcommand used by `cargo-process-watch'."
:type 'string)
(defvar cargo-process-favorite-crates nil)
(defface cargo-process--ok-face
'((t (:inherit success)))
"Ok face")
(defface cargo-process--error-face
'((t (:inherit error)))
"Error face")
(defface cargo-process--warning-face
'((t (:inherit warning)))
"Warning face")
(defface cargo-process--pointer-face
'((t (:inherit font-lock-negation-char-face)))
"Pointer face")
(defface cargo-process--standard-face
'((t (:inherit font-lock-comment-face)))
"Standard face")
(defface cargo-process--errno-face
'((t (:inherit link)))
"Error number face")
(defconst cargo-process--rust-backtrace "RUST_BACKTRACE")
(defconst cargo-process-test-regexp "^[[:space:]]*fn[[:space:]]*"
"Regex to find Rust unit test function.")
(defconst cargo-process-test-mod-regexp "^[[:space:]]*mod[[:space:]]+[[:word:][:multibyte:]_][[:word:][:multibyte:]_[:digit:]]*[[:space:]]*{")
(defconst cargo-process-font-lock-keywords
'(("^error\\:?" . 'cargo-process--error-face)
("^warning\\:?" . 'cargo-process--warning-face)
("^\s*\\^\\~*\s*$" . 'cargo-process--pointer-face)
("^\s*Compiling.*" . 'cargo-process--standard-face)
("^\s*Running.*" . 'cargo-process--standard-face)
("^\s*Updating.*" . 'cargo-process--standard-face)
("test result: FAILED." . 'cargo-process--error-face)
("test result: ok." . 'cargo-process--ok-face)
("test\s.*\sFAILED" . 'cargo-process--error-face)
("test\s.*\sok" . 'cargo-process--ok-face))
"Minimal highlighting expressions for cargo-process mode.")
;; Bind `case-fold-search' to nil before using the regex.
(defconst cargo-process--errno-regex "\\bE[0-9]\\{4\\}\\b"
"A regular expression to match Rust error number.")
(define-button-type 'rustc-errno
'follow-link t
'face 'cargo-process--errno-face
'action #'cargo-process--explain-action)
(defun cargo-process--defun-at-point-p ()
(string-match cargo-process-test-regexp
(buffer-substring-no-properties (line-beginning-position)
(line-end-position))))
(defun cargo-process--project-root (&optional extra)
"Find the root of the current Cargo project."
(let ((root (locate-dominating-file (or buffer-file-name default-directory) "Cargo.toml")))
(when root
(file-truename (concat root extra)))))
(define-derived-mode cargo-process-mode compilation-mode "Cargo-Process."
"Major mode for the Cargo process buffer."
(use-local-map cargo-process-mode-map)
(setq major-mode 'cargo-process-mode)
(setq mode-name "Cargo-Process")
(setq-local truncate-lines t)
(run-hooks 'cargo-process-mode-hook)
(add-hook 'compilation-filter-hook #'cargo-process--add-errno-buttons)
(add-hook 'compilation-filter-hook #'cargo-process--fix-missing-subcommand)
(font-lock-add-keywords nil cargo-process-font-lock-keywords))
(defun cargo-process--finished-sentinel (process event)
"Execute after PROCESS return and EVENT is \\='finished\\='."
(compilation-sentinel process event)
(when (equal event "finished\n")
(message "Cargo Process finished.")))
(defun cargo-process--activate-mode (buffer)
"Execute commands BUFFER at process start."
(with-current-buffer buffer
(funcall 'cargo-process-mode)
(setq-local window-point-insertion-type t)))
(defun set-rust-backtrace (command)
"Set RUST_BACKTRACE variable depending on the COMMAND used.
Always set to nil if cargo-process--enable-rust-backtrace is nil"
(if (and cargo-process--enable-rust-backtrace
(string-match "\\(test\\|run\\)" command))
(setenv cargo-process--rust-backtrace "1")
(setenv cargo-process--rust-backtrace nil)))
(defun cargo-json-read-from-string (text)
"A wrapper for `json-read-from-string' with improved error reporting.
This assumes that TEXT is a human readable error, which is used
for error reporting."
(condition-case nil
(json-read-from-string text)
(error (user-error text))))
(defun cargo-process--tramp-file-name-prefix (file-name)
"Return the TRAMP prefix for FILE-NAME.
If FILE-NAME is not a TRAMP file, return an empty string."
(if (and (fboundp 'tramp-tramp-file-p)
(tramp-tramp-file-p file-name))
(let ((tramp-file-name (tramp-dissect-file-name file-name)))
(setf (nth 6 tramp-file-name) nil)
(tramp-make-tramp-file-name tramp-file-name))
""))
(defun cargo-process--tramp-file-local-name (file-name)
"Return the local component of FILE-NAME.
If FILE-NAME is not a TRAMP file, return it unmodified."
(if (and (fboundp 'tramp-tramp-file-p)
(tramp-tramp-file-p file-name))
(nth 6 (tramp-dissect-file-name file-name))
file-name))
(defun cargo-process--workspace-root ()
"Find the workspace root using `cargo metadata`."
(when (cargo-process--project-root)
(let* ((metadata-text (shell-command-to-string
(concat (shell-quote-argument cargo-process--custom-path-to-bin)
" metadata --format-version 1 --no-deps")))
(metadata-json (cargo-json-read-from-string metadata-text))
(tramp-prefix (cargo-process--tramp-file-name-prefix (cargo-process--project-root)))
(workspace-root (concat tramp-prefix
(cdr (assoc 'workspace_root metadata-json)))))
workspace-root)))
(defun cargo-process--get-metadata ()
"Return the metadata of the current package as an alist."
(when (cargo-process--project-root)
(let ((metadata-text
(shell-command-to-string
(concat (shell-quote-argument cargo-process--custom-path-to-bin)
" metadata --format-version 1"))))
(cargo-json-read-from-string metadata-text))))
(defun manifest-path-argument (name)
(let ((manifest-filename (cargo-process--tramp-file-local-name (cargo-process--project-root "Cargo.toml"))))
(when (and manifest-filename
(not (member name cargo-process--no-manifest-commands)))
(concat "--manifest-path " (shell-quote-argument manifest-filename)))))
(defun cargo-process--start (name command &optional last-cmd opens-external additional-args)
"Start the Cargo process NAME with the cargo command COMMAND.
OPENS-EXTERNAL is non-nil if the COMMAND is expected to open an external application.
Returns the created process."
(set-rust-backtrace command)
(let* ((buffer (concat "*Cargo " name "*"))
(project-root (cargo-process--project-root))
(cmd
(or last-cmd
(cargo-process--maybe-read-command
(cargo-process--augment-cmd-for-os opens-external
(mapconcat #'identity (list (shell-quote-argument cargo-process--custom-path-to-bin)
command
(manifest-path-argument name)
cargo-process--command-flags
additional-args)
" ")))))
(default-directory (or project-root default-directory)))
(save-some-buffers (not compilation-ask-about-save)
(lambda ()
(and project-root
buffer-file-name
(string-prefix-p project-root (file-truename buffer-file-name)))))
(setq cargo-process-last-command (list name command cmd))
(let ((default-directory (or (cargo-process--workspace-root)
default-directory)))
(compilation-start cmd 'cargo-process-mode (lambda(_) buffer)))
(end-of-buffer-other-window buffer)
(let ((process (get-buffer-process buffer)))
(set-process-sentinel process 'cargo-process--finished-sentinel)
process)))
(defun cargo-process--explain-action (button)
"Action called when the user activates Rust errno BUTTON."
(cargo-process--explain-help (button-label button)))
(defun cargo-process--explain-help (errno)
"Display a detailed explaination of ERRNO in a markdown buffer."
(pop-to-buffer
(let ((current-window (selected-window))
(inhibit-message t))
(with-current-buffer (get-buffer-create "*rust errno*")
(let ((buffer-read-only nil))
(erase-buffer)
(insert (shell-command-to-string
(concat cargo-process--rustc-cmd " --explain=" errno))))
(markdown-view-mode)
(setq-local markdown-fontify-code-blocks-natively t)
(setq-local markdown-fontify-code-block-default-mode 'rust-mode)
(setq-local kill-buffer-hook (lambda ()
(when (window-live-p current-window)
(select-window current-window))))
(setq
header-line-format
(concat (propertize " " 'display
`(space :align-to (- right-fringe ,(1+ (length errno)))))
(propertize errno 'face 'error)))
(markdown-toggle-markup-hiding 1)
(goto-char 1)
(current-buffer)))))
(defvar cargo-process--install-cmd-alist
'(("check" . "cargo-check")
("clippy" . "clippy")
("script" . "cargo-script")
("add" . "cargo-edit")
("rm" . "cargo-edit")
("upgrade" . "cargo-edit")
("audit" . "cargo-audit")
("watch" . "cargo-watch"))
"Alist of subcommand-component pairs.
\\='cargo install COMPONENT\\=' should provide SUBCOMMAND.")
(defun cargo-process--fix-missing-subcommand ()
"Search complication output for missing subcommand and offer solution.
Meant to be run as a `compilation-filter-hook'."
(save-excursion
(goto-char compilation-filter-start)
(beginning-of-line)
(when (looking-at "error: no such subcommand: `\\(.*\\)`")
(if-let* ((missing-cmd
(match-string 1))
(component
(alist-get missing-cmd cargo-process--install-cmd-alist
nil nil 'equal)))
(when (y-or-n-p (concat "Missing subcommand: '" missing-cmd
"', fix it by running '"
"cargo install " component "'? "))
(cargo-process--start "Install" (concat "install " component)))))
(when (looking-at
"^error: the '\\([^']*\\)' binary, \
normally provided by the '\\([^']*\\)' component")
(if-let* ((missing-binary (match-string 1))
(component (match-string 2)))
(when (y-or-n-p (concat "Missing binary: '" missing-binary
"', fix it by running '"
"cargo install " component "'? "))
(cargo-process--start "Install" (concat "install " component)))))))
(defun cargo-process--add-errno-buttons ()
"Turn error numbers into clickable links in Cargo process output.
Meant to be run as a `compilation-filter-hook'."
(save-excursion
(let ((start compilation-filter-start)
(end (point))
(case-fold-search nil))
(goto-char start)
(while (re-search-forward cargo-process--errno-regex end t)
(make-button (match-beginning 0)
(match-end 0)
:type 'rustc-errno)))))
(defun cargo-process--get-current-test ()
"Return the current test."
(save-excursion
(unless (cargo-process--defun-at-point-p)
(cond ((fboundp 'rust-beginning-of-defun)
(rust-beginning-of-defun))
((fboundp 'rustic-beginning-of-defun)
(rustic-beginning-of-defun))
(t (user-error "%s needs either rust-mode or rustic-mode"
this-command))))
(beginning-of-line)
(search-forward "fn ")
(let* ((line (buffer-substring-no-properties (point)
(line-end-position)))
(lines (split-string line "("))
(function-name (car lines)))
function-name)))
(defun cargo-process--get-current-mod ()
"Return the current mod."
(save-excursion
(when (search-backward-regexp cargo-process-test-mod-regexp nil t)
(let* ((line (buffer-substring-no-properties (line-beginning-position)
(line-end-position)))
(line (string-trim-left line))
(lines (split-string line " \\|{"))
(mod (cadr lines)))
mod))))
(defun cargo-process--get-current-test-fullname ()
(if (cargo-process--get-current-mod)
(concat (cargo-process--get-current-mod)
"::"
(cargo-process--get-current-test))
(cargo-process--get-current-test)))
(defun cargo-process--maybe-read-command (default)
"Prompt to modify the DEFAULT command when the prefix argument is present.
Without the prefix argument, return DEFAULT immediately."
(if current-prefix-arg
(read-shell-command "Cargo command: " default)
default))
(defun cargo-process--get-dependencies (&optional manifest)
"Extract the list of dependencies from the
MANIFEST (i.e. Cargo.toml)."
(with-current-buffer (find-file-noselect (or manifest
(cargo-process--project-root "Cargo.toml")))
(save-excursion
(let ((deps-list))
(widen)
(goto-char (point-min))
(search-forward "[dependencies]" nil t)
(while (re-search-forward "^[a-zA-Z\-\_]* *=" nil t)
(beginning-of-line)
(setq deps-list
(cons (thing-at-point 'sexp t) deps-list))
(end-of-line))
deps-list))))
(defun cargo-process--augment-cmd-for-os (opens-external cmd)
"Augment the cargo CMD according to OS. OPENS-EXTERNAL is non-nil
if the CMD is expected to open and external application."
(if (and opens-external
(not (member system-type '(windows-nt ms-dos)))
(executable-find "setsid"))
(concat "setsid -w " cmd)
cmd))
;;;###autoload
(defun cargo-process-bench ()
"Run the Cargo bench command.
With the prefix argument, modify the command's invocation.
Cargo: Run the benchmarks."
(interactive)
(cargo-process--start "Bench" cargo-process--command-bench))
;;;###autoload
(defun cargo-process-build ()
"Run the Cargo build command.
With the prefix argument, modify the command's invocation.
Cargo: Compile the current project."
(interactive)
(cargo-process--start "Build" cargo-process--command-build))
;;;###autoload
(defun cargo-process-clean ()
"Run the Cargo clean command.
With the prefix argument, modify the command's invocation.
Cargo: Remove the target directory."
(interactive)
(cargo-process--start "Clean" cargo-process--command-clean))
;;;###autoload
(defun cargo-process-doc ()
"Run the Cargo doc command.
With the prefix argument, modify the command's invocation.
Cargo: Build this project's and its dependencies' documentation."
(interactive)
(cargo-process--start "Doc" cargo-process--command-doc))
;;;###autoload
(defun cargo-process-doc-open ()
"Run the Cargo doc command with the --open switch.
With the prefix argument, modify the command's invocation.
Cargo: Open this project's documentation."
(interactive)
(cargo-process--start "Doc" cargo-process--command-doc-open nil t))
;;;###autoload
(defun cargo-process-new (name &optional bin)
"Run the Cargo new command.
With the prefix argument, modify the command's invocation.
NAME is the name of your application.
If BIN is t then create a binary application, otherwise a library.
Cargo: Create a new cargo project."
(interactive "sProject name: ")
(let* ((bin (if (or bin
(y-or-n-p "Create Bin Project? "))
" --bin"
" --lib"))
(command
(concat cargo-process--command-new " " name bin))
(process
(cargo-process--start "New" command)))
(set-process-sentinel
process
(lambda (_process _event)
(let* ((project-root (cargo-process--project-root))
(default-directory (or project-root default-directory)))
(cond
((and cargo-process--open-file-after-new
(string= " --bin" bin))
(find-file (format "%s/src/main.rs" name)))
((and cargo-process--open-file-after-new
(string= " --lib" bin))
(find-file (format "%s/src/lib.rs" name)))))))))
;;;###autoload
(defun cargo-process-init (directory &optional bin)
"Run the Cargo init command.
With the prefix argument, modify the command's invocation.
DIRECTORY is the directory you want to create a cargo project in.
If BIN is t then create a binary application, otherwise a library.
Cargo: Create a new cargo project in current directory.
DIRECTORY is created if necessary."
(interactive
(list (read-directory-name "Directory: " nil default-directory nil)))
(let* ((bin (if (or bin (y-or-n-p "Create Bin Project? "))
" --bin"
" --lib"))
(process
(cargo-process--start "Init" (concat cargo-process--command-init
" "
directory
bin))))
(set-process-sentinel
process
(lambda (process event)
(cargo-process--open-manifest process
event
(expand-file-name "Cargo.toml" directory))))))
(defun cargo-process--open-manifest (_process event manifest-path)
"Open the manifest file when process ends."
(when (equal event "finished\n")
(find-file manifest-path)))
;;;###autoload
(defun cargo-process-run ()
"Run the Cargo run command.
With the prefix argument, modify the command's invocation.
Cargo: Build and execute src/main.rs."
(interactive)
(cargo-process--start "Run" cargo-process--command-run))
;;;###autoload
(defun cargo-process-run-bin (command)
"Run the Cargo run command --bin <name>.
With the prefix argument, modify the command's invocation.
Cargo: Build and execute a specific binary"
(interactive "sBinary name: ")
(cargo-process--start (concat "Run " command)
(concat cargo-process--command-run-bin " " command)))
;;;###autoload
(defun cargo-process-run-example (command)
"Run the Cargo run command --example <name>.
With the prefix argument, modify the command's invocation.
Cargo: Build and execute with --example <name>."
(interactive "sExample name: ")
(cargo-process--start (concat "Example " command)
(concat cargo-process--command-run-example " " command)))
;;;###autoload
(defun cargo-process-search (search-term)
"Run the Cargo search command.
With the prefix argument, modify the command's invocation.
SEARCH-TERM is used as the search term for the Cargo registry.
Cargo: Search registry for crates."
(interactive
(list (cargo-process--read-search-term)))
(cargo-process--start "Search"
(concat cargo-process--command-search " " search-term)))
(defun cargo-process--read-search-term ()
"Prompt for a search term, using the crate name at point (if
any) as the default if none is entered."
(let* ((default-search-term (and (thing-at-point-looking-at "[[:alnum:]-_]+") (match-string-no-properties 0)))
(default-prompt-text (if default-search-term (format " (default %s)" default-search-term) ""))
(search-term (read-string (format "Search%s: " default-prompt-text)
nil nil default-search-term)))
search-term))
;;;###autoload
(defun cargo-process-test ()
"Run the Cargo test command.
With the prefix argument, modify the command's invocation.
Cargo: Run the tests."
(interactive)
(cargo-process--start "Test" cargo-process--command-test nil nil cargo-process--command-test--additional-args))
;;;###autoload
(defun cargo-process-current-test ()
"Run the Cargo test command for the current test.
With the prefix argument, modify the command's invocation.
Cargo: Run the tests."
(interactive)
(cargo-process--start "Test"
(concat cargo-process--command-current-test
" "
(cargo-process--get-current-test-fullname))
nil
nil
cargo-process--command-test--additional-args))
;;;###autoload
(defun cargo-process-current-file-tests ()
"Run the Cargo test command for the current file.
With the prefix argument, modify the command's invocation.
Cargo: Run the tests."
(interactive)
(cargo-process--start "Test"
(concat cargo-process--command-current-file-tests
" "
(cargo-process--get-current-mod))
nil
nil
cargo-process--command-test--additional-args))
;;;###autoload
(defun cargo-process-update ()
"Run the Cargo update command.
With the prefix argument, modify the command's invocation.
Cargo: Update dependencies listed in Cargo.lock."
(interactive)
(cargo-process--start "Update" cargo-process--command-update))
;;;###autoload
(defun cargo-process-fmt ()
"Run the Cargo fmt command.
With the prefix argument, modify the command's invocation.
Requires rustfmt to be installed."
(interactive)
(cargo-process--start "Fmt" cargo-process--command-fmt))
;;;###autoload
(defun cargo-process-outdated ()
"Run the Cargo outdated command.
With the prefix argument, modify the command's invocation.
Requires cargo-outdated to be installed."
(interactive)
(cargo-process--start "Outdated" cargo-process--command-outdated))
;;;###autoload
(defun cargo-process-check ()
"Run the Cargo check command.
With the prefix argument, modify the command's invocation.
Cargo: Check compile the current project.
Requires cargo-check to be installed."
(interactive)
(cargo-process--start "Check" cargo-process--command-check))
;;;###autoload
(defun cargo-process-clippy ()
"Run the Cargo clippy command.
With the prefix argument, modify the command's invocation.
Cargo: Clippy compile the current project.
Requires clippy to be installed."
(interactive)
(cargo-process--start "Clippy"
cargo-process--command-clippy
nil
nil
cargo-process--command-clippy--additional-args))
;;;###autoload
(defun cargo-process-add (crate)
"Run the Cargo add command.
With the prefix argument, modify the command's invocation.
CRATES is the name of the crate to add.
Cargo: This command allows you to add a dependency to a Cargo.toml manifest file."
(interactive (list
(completing-read "Crate(s) to add: " cargo-process-favorite-crates)))
(cargo-process--start "Add" (concat cargo-process--command-add
" "
crate)))
;;;###autoload
(defun cargo-process-audit ()
"Run the Cargo audit command.
With the prefix argument, modify the command's invocation.
Cargo: Audit checks the current project's Cargo.lock for security vulnerabilities.
Requires cargo-audit to be installed."
(interactive)
(cargo-process--start "Audit" (concat cargo-process--command-audit
" "
(cargo-process--project-root "Cargo.lock"))))
;;;###autoload
(defun cargo-process-script ()
"Run the Cargo script command.
With the prefix argument, modify the command's invocation.
Cargo: Script compiles and runs 'Cargoified Rust scripts'.
Requires cargo-script to be installed."
(interactive)
(cargo-process--start "Script" (concat cargo-process--command-script
" "
buffer-file-name)))
;;;###autoload
(defun cargo-process-watch ()
"Run the Cargo watch command.
With the prefix argument, modify the command's invocation.
Cargo: Watches over your Cargo project’s source.
Requires cargo-watch to be installed."
(interactive)
(cargo-process--start "Watch" (concat cargo-process--command-watch)))
;;;###autoload
(defun cargo-process-watch-run ()
"Run the Cargo watch command.
With the prefix argument, modify the command's invocation.
Cargo: Watches over your Cargo project’s source.
Requires cargo-watch to be installed."
(interactive)
(cargo-process--start "Watch" (concat cargo-process--command-watch-run)))
;;;###autoload
(defun cargo-process-rm (crate)
"Run the Cargo rm command.
With the prefix argument, modify the command's invocation.
CRATE is the name of the crate to remove.
Cargo: Remove a dependency from a Cargo.toml manifest file."
(interactive
(list
(let ((deplist (cargo-process--get-dependencies)))
(when deplist
(completing-read "Crate to remove: "
deplist nil t)))))
(if crate (cargo-process--start "Remove" (concat cargo-process--command-rm
" "
crate))
(message "No crates used in current project.")))
;;;###autoload
(defun cargo-process-upgrade (&optional all crates)
"Run the Cargo update command.
With the prefix argument, modify the command's invocation.
If ALL is t then update all crates, otherwise specify CRATES.
Cargo: Upgrade dependencies as specified in the local manifest file"
(interactive)
(let ((deplist (cargo-process--get-dependencies)))
(if deplist
(let* ((all (when (or all
(y-or-n-p "Upgrade all crates? "))
"--workspace"))
(crates (when (not all)
(or crates
(completing-read "Crate(s) to upgrade: "
deplist nil 'confirm)))))
(cargo-process--start "Upgrade" (concat cargo-process--command-upgrade
" "
all
" "
crates)))
(message "No crates used in current project."))))
;;;###autoload
(defun cargo-process-repeat ()
"Run the last cargo-process command."
(interactive)
(if cargo-process-last-command
(apply 'cargo-process--start cargo-process-last-command)
(message "No last Cargo command.")))
(define-key cargo-process-mode-map (kbd "n") 'forward-button)
(define-key cargo-process-mode-map (kbd "p") 'backward-button)
(provide 'cargo-process)
;;; cargo-process.el ends here