Skip to content

Commit 84caa83

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

File tree

3 files changed

+163
-1
lines changed

3 files changed

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

+25
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-completions) --- Move cargo shell completions to native completions.
120121

121122
## allow-features
122123

@@ -1686,6 +1687,30 @@ 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+
1711+
- powershell:
1712+
Add `CARGO_COMPLETE=powershell cargo | Invoke-Expression` to `$PROFILE`.
1713+
16891714
# Stabilized and removed features
16901715

16911716
## Compile progress

tests/testsuite/shell_completions.rs

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

3133
fn complete(input: &str, shell: &str) -> String {
4134
let shell = shell.into();
@@ -53,4 +183,3 @@ source <(CARGO_COMPLETE=zsh cargo)",
53183

54184
runtime
55185
}
56-

0 commit comments

Comments
 (0)