Skip to content

Commit 063df78

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

File tree

3 files changed

+162
-1
lines changed

3 files changed

+162
-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

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

3135
fn complete(input: &str, shell: &str) -> String {
4136
let shell = shell.into();
@@ -53,4 +185,3 @@ source <(CARGO_COMPLETE=zsh cargo)",
53185

54186
runtime
55187
}
56-

0 commit comments

Comments
 (0)