Skip to content

Commit 19eef54

Browse files
committed
feat: Add native completions with CompleteEnv and under the nightly features
1 parent f25806c commit 19eef54

File tree

3 files changed

+158
-1
lines changed

3 files changed

+158
-1
lines changed

src/bin/cargo/main.rs

+8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#![allow(clippy::self_named_module_files)] // false positive in `commands/build.rs`
22

3+
use cargo::core::features;
34
use cargo::core::shell::Shell;
45
use cargo::util::network::http::http_handle;
56
use cargo::util::network::http::needs_custom_http_transport;
@@ -28,6 +29,13 @@ fn main() {
2829
}
2930
};
3031

32+
let nightly_features_allowed = matches!(&*features::channel(), "nightly" | "dev");
33+
if nightly_features_allowed {
34+
clap_complete::CompleteEnv::with_factory(|| cli::cli(&mut gctx))
35+
.var("CARGO_COMPLETE")
36+
.complete();
37+
}
38+
3139
let result = if let Some(lock_addr) = cargo::ops::fix_get_proxy_lock_addr() {
3240
cargo::ops::fix_exec_rustc(&gctx, &lock_addr).map_err(|e| CliError::from(e))
3341
} else {

src/doc/src/reference/unstable.md

+22
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ Each new feature described below should explain how to use it.
117117
* [script](#script) --- Enable support for single-file `.rs` packages.
118118
* [lockfile-path](#lockfile-path) --- Allows to specify a path to lockfile other than the default path `<workspace_root>/Cargo.lock`.
119119
* [package-workspace](#package-workspace) --- Allows for packaging and publishing multiple crates in a workspace.
120+
* [native-completions](#native-completion) --- Move cargo shell completions to native completions.
120121

121122
## allow-features
122123

@@ -1686,6 +1687,27 @@ cargo +nightly -Zpackage-workspace --registry=my-registry package -p foo -p dep
16861687
cargo +nightly -Zpackage-workspace --index=https://example.com package -p foo -p dep
16871688
```
16881689

1690+
## native-completions
1691+
* Original Issue: [#6645](https://github.com/rust-lang/cargo/issues/6645)
1692+
* Tracking Issue: [#14520](https://github.com/rust-lang/cargo/issues/14520)
1693+
1694+
This feature moves the handwritten completion scripts to Rust native, making it
1695+
easier for us to add, extend and test new completions. This feature is enabled with the
1696+
nightly channel, without requiring additional `-Z` options.
1697+
1698+
### How to use native-completions feature:
1699+
- bash:
1700+
Add `source <(CARGO_COMPLETE=bash cargo)` to your .bashrc.
1701+
1702+
- zsh:
1703+
Add `source <(CARGO_COMPLETE=zsh cargo)` to your .zshrc.
1704+
1705+
- fish:
1706+
Add `source (CARGO_COMPLETE=fish cargo | psub)` to `$XDG_CONFIG_HOME/fish/completions/cargo.fish`
1707+
1708+
- elvish:
1709+
Add `eval (E:CARGO_COMPLETE=elvish cargo | slurp)` to `$XDG_CONFIG_HOME/elvish/rc.elv`
1710+
16891711
# Stabilized and removed features
16901712

16911713
## Compile progress

tests/testsuite/shell_completions.rs

+128-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,132 @@
1+
use cargo_test_support::cargo_test;
12
use completest_pty::Runtime;
3+
use snapbox::assert_data_eq;
4+
5+
#[cargo_test]
6+
fn bash() {
7+
let input = "cargo \t\t";
8+
let expected = snapbox::str![
9+
"%
10+
--version --help check install read-manifest update
11+
--list -V clean locate-project remove vendor
12+
--explain -v config login report verify-project
13+
--verbose -q doc logout run version
14+
--quiet -C fetch metadata rustc yank
15+
--color -Z fix new rustdoc
16+
--locked -h generate-lockfile owner search
17+
--offline add help package test
18+
--frozen bench info pkgid tree
19+
--config build init publish uninstall "
20+
];
21+
let actual = complete(input, "bash");
22+
assert_data_eq!(actual, expected);
23+
}
24+
25+
#[cargo_test]
26+
fn elvish() {
27+
let input = "cargo \t\t";
28+
let expected = snapbox::str![
29+
"% cargo
30+
COMPLETING argument
31+
--color --version check install read-manifest update
32+
--config -C clean locate-project remove vendor
33+
--explain -V config login report verify-project
34+
--frozen -Z doc logout run version
35+
--help -h fetch metadata rustc yank
36+
--list -q fix new rustdoc
37+
--locked -v generate-lockfile owner search
38+
--offline add help package test
39+
--quiet bench info pkgid tree
40+
--verbose build init publish uninstall "
41+
];
42+
let actual = complete(input, "elvish");
43+
assert_data_eq!(actual, expected);
44+
}
45+
46+
#[cargo_test]
47+
fn fish() {
48+
let input = "cargo \t\t";
49+
let expected = snapbox::str![
50+
"% cargo
51+
--version (Print version info and exit)
52+
--list (List installed commands)
53+
--explain (Provide a detailed explanation of a rustc error message)
54+
--verbose (Use verbose output (-vv very verbose/build.rs output))
55+
--quiet (Do not print cargo log messages)
56+
--color (Coloring: auto, always, never)
57+
--locked (Assert that `Cargo.lock` will remain unchanged)
58+
--offline (Run without accessing the network)
59+
--frozen (Equivalent to specifying both --locked and --offline)
60+
--config (Override a configuration value)
61+
--help (Print help)
62+
-V (Print version info and exit)
63+
-v (Use verbose output (-vv very verbose/build.rs output))
64+
-q (Do not print cargo log messages)
65+
-C (Change to DIRECTORY before doing anything (nightly-only))
66+
-Z (Unstable (nightly-only) flags to Cargo, see 'cargo -Z help' for details)
67+
-h (Print help)
68+
add (Add dependencies to a Cargo.toml manifest file)
69+
bench (Execute all benchmarks of a local package)
70+
build (Compile a local package and all of its dependencies)
71+
check (Check a local package and all of its dependencies for errors)
72+
clean (Remove artifacts that cargo has generated in the past)
73+
config (Inspect configuration values)
74+
doc (Build a package's documentation)
75+
fetch (Fetch dependencies of a package from the network)
76+
fix (Automatically fix lint warnings reported by rustc)
77+
generate-lockfile (Generate the lockfile for a package)
78+
help (Displays help for a cargo subcommand)
79+
info (Display information about a package in the registry)
80+
init (Create a new cargo package in an existing directory)
81+
install (Install a Rust binary)
82+
locate-project (Print a JSON representation of a Cargo.toml file's location)
83+
login (Log in to a registry.)
84+
logout (Remove an API token from the registry locally)
85+
metadata (Output the resolved dependencies of a package, the concrete used versions including overrides, in machine-r…)
86+
new (Create a new cargo package at <path>)
87+
owner (Manage the owners of a crate on the registry)
88+
package (Assemble the local package into a distributable tarball)
89+
pkgid (Print a fully qualified package specification)
90+
publish (Upload a package to the registry)
91+
read-manifest (Print a JSON representation of a Cargo.toml manifest.)
92+
remove (Remove dependencies from a Cargo.toml manifest file)
93+
report (Generate and display various kinds of reports)
94+
run (Run a binary or example of the local package)
95+
rustc (Compile a package, and pass extra options to the compiler)
96+
rustdoc (Build a package's documentation, using specified custom flags.)
97+
search (Search packages in the registry. Default registry is crates.io)
98+
test (Execute all unit and integration tests and build examples of a local package)
99+
tree (Display a tree visualization of a dependency graph)
100+
uninstall (Remove a Rust binary)
101+
update (Update dependencies as recorded in the local lock file)
102+
vendor (Vendor all dependencies for a project locally)
103+
verify-project (Check correctness of crate manifest)
104+
version (Show version information)
105+
yank (Remove a pushed crate from the index)"];
106+
107+
let actual = complete(input, "fish");
108+
assert_data_eq!(actual, expected);
109+
}
110+
111+
#[cargo_test]
112+
fn zsh() {
113+
let input = "cargo \t\t";
114+
let expected = snapbox::str![
115+
"% cargo
116+
--color --version check install read-manifest update
117+
--config -C clean locate-project remove vendor
118+
--explain -V config login report verify-project
119+
--frozen -Z doc logout run version
120+
--help -h fetch metadata rustc yank
121+
--list -q fix new rustdoc
122+
--locked -v generate-lockfile owner search
123+
--offline add help package test
124+
--quiet bench info pkgid tree
125+
--verbose build init publish uninstall "
126+
];
127+
let actual = complete(input, "zsh");
128+
assert_data_eq!(actual, expected);
129+
}
2130

3131
fn complete(input: &str, shell: &str) -> String {
4132
let shell = shell.into();
@@ -53,4 +181,3 @@ source <(CARGO_COMPLETE=zsh cargo)",
53181

54182
runtime
55183
}
56-

0 commit comments

Comments
 (0)