1
1
use crate :: core:: compiler:: { BuildConfig , MessageFormat , TimingOutput } ;
2
2
use crate :: core:: resolver:: CliFeatures ;
3
- use crate :: core:: { Edition , Workspace } ;
3
+ use crate :: core:: { shell , Edition , Target , TargetKind , Workspace } ;
4
4
use crate :: ops:: lockfile:: LOCKFILE_NAME ;
5
5
use crate :: ops:: registry:: RegistryOrIndex ;
6
6
use crate :: ops:: { CompileFilter , CompileOptions , NewOptions , Packages , VersionControl } ;
@@ -19,6 +19,7 @@ use cargo_util_schemas::manifest::ProfileName;
19
19
use cargo_util_schemas:: manifest:: RegistryName ;
20
20
use cargo_util_schemas:: manifest:: StringOrVec ;
21
21
use clap:: builder:: UnknownArgumentValueParser ;
22
+ use home:: cargo_home_with_cwd;
22
23
use std:: ffi:: { OsStr , OsString } ;
23
24
use std:: path:: Path ;
24
25
use std:: path:: PathBuf ;
@@ -177,7 +178,10 @@ pub trait CommandExt: Sized {
177
178
. _arg ( flag ( "examples" , examples) . help_heading ( heading:: TARGET_SELECTION ) )
178
179
. _arg (
179
180
optional_multi_opt ( "example" , "NAME" , example)
180
- . help_heading ( heading:: TARGET_SELECTION ) ,
181
+ . help_heading ( heading:: TARGET_SELECTION )
182
+ . add ( clap_complete:: ArgValueCandidates :: new (
183
+ get_example_candidates,
184
+ ) ) ,
181
185
)
182
186
}
183
187
@@ -192,7 +196,10 @@ pub trait CommandExt: Sized {
192
196
. _arg ( flag ( "bins" , bins) . help_heading ( heading:: TARGET_SELECTION ) )
193
197
. _arg (
194
198
optional_multi_opt ( "example" , "NAME" , example)
195
- . help_heading ( heading:: TARGET_SELECTION ) ,
199
+ . help_heading ( heading:: TARGET_SELECTION )
200
+ . add ( clap_complete:: ArgValueCandidates :: new (
201
+ get_example_candidates,
202
+ ) ) ,
196
203
)
197
204
. _arg ( flag ( "examples" , examples) . help_heading ( heading:: TARGET_SELECTION ) )
198
205
}
@@ -201,7 +208,10 @@ pub trait CommandExt: Sized {
201
208
self . _arg ( optional_multi_opt ( "bin" , "NAME" , bin) . help_heading ( heading:: TARGET_SELECTION ) )
202
209
. _arg (
203
210
optional_multi_opt ( "example" , "NAME" , example)
204
- . help_heading ( heading:: TARGET_SELECTION ) ,
211
+ . help_heading ( heading:: TARGET_SELECTION )
212
+ . add ( clap_complete:: ArgValueCandidates :: new (
213
+ get_example_candidates,
214
+ ) ) ,
205
215
)
206
216
}
207
217
@@ -1027,6 +1037,32 @@ pub fn lockfile_path(
1027
1037
return Ok ( Some ( path) ) ;
1028
1038
}
1029
1039
1040
+ fn get_example_candidates ( ) -> Vec < clap_complete:: CompletionCandidate > {
1041
+ get_targets_from_metadata ( )
1042
+ . unwrap_or_default ( )
1043
+ . into_iter ( )
1044
+ . filter_map ( |target| match target. kind ( ) {
1045
+ TargetKind :: ExampleBin => Some ( clap_complete:: CompletionCandidate :: new ( target. name ( ) ) ) ,
1046
+ _ => None ,
1047
+ } )
1048
+ . collect :: < Vec < _ > > ( )
1049
+ }
1050
+
1051
+ fn get_targets_from_metadata ( ) -> CargoResult < Vec < Target > > {
1052
+ let cwd = std:: env:: current_dir ( ) ?;
1053
+ let gctx = GlobalContext :: new ( shell:: Shell :: new ( ) , cwd. clone ( ) , cargo_home_with_cwd ( & cwd) ?) ;
1054
+ let ws = Workspace :: new ( & find_root_manifest_for_wd ( & cwd) ?, & gctx) ?;
1055
+
1056
+ let packages = ws. members ( ) . collect :: < Vec < _ > > ( ) ;
1057
+
1058
+ let targets = packages
1059
+ . into_iter ( )
1060
+ . flat_map ( |pkg| pkg. targets ( ) . into_iter ( ) . cloned ( ) )
1061
+ . collect :: < Vec < _ > > ( ) ;
1062
+
1063
+ Ok ( targets)
1064
+ }
1065
+
1030
1066
#[ track_caller]
1031
1067
pub fn ignore_unknown < T : Default > ( r : Result < T , clap:: parser:: MatchesError > ) -> T {
1032
1068
match r {
0 commit comments