@@ -4,7 +4,7 @@ use crate::core::compiler::{
4
4
BuildConfig , CompileKind , MessageFormat , RustcTargetData , TimingOutput ,
5
5
} ;
6
6
use crate :: core:: resolver:: { CliFeatures , ForceAllTargets , HasDevUnits } ;
7
- use crate :: core:: { Edition , Package , Target , TargetKind , Workspace , profiles:: Profiles , shell} ;
7
+ use crate :: core:: { Edition , Package , TargetKind , Workspace , profiles:: Profiles , shell} ;
8
8
use crate :: ops:: lockfile:: LOCKFILE_NAME ;
9
9
use crate :: ops:: registry:: RegistryOrIndex ;
10
10
use crate :: ops:: { self , CompileFilter , CompileOptions , NewOptions , Packages , VersionControl } ;
@@ -169,13 +169,17 @@ pub trait CommandExt: Sized {
169
169
. _arg (
170
170
optional_multi_opt ( "test" , "NAME" , test)
171
171
. help_heading ( heading:: TARGET_SELECTION )
172
- . add ( clap_complete:: ArgValueCandidates :: new ( get_test_candidates) ) ,
172
+ . add ( clap_complete:: ArgValueCandidates :: new ( || {
173
+ get_crate_candidates ( TargetKind :: Test ) . unwrap_or_default ( )
174
+ } ) ) ,
173
175
)
174
176
. _arg ( flag ( "benches" , benches) . help_heading ( heading:: TARGET_SELECTION ) )
175
177
. _arg (
176
178
optional_multi_opt ( "bench" , "NAME" , bench)
177
179
. help_heading ( heading:: TARGET_SELECTION )
178
- . add ( clap_complete:: ArgValueCandidates :: new ( get_bench_candidates) ) ,
180
+ . add ( clap_complete:: ArgValueCandidates :: new ( || {
181
+ get_crate_candidates ( TargetKind :: Bench ) . unwrap_or_default ( )
182
+ } ) ) ,
179
183
)
180
184
. _arg ( flag ( "all-targets" , all) . help_heading ( heading:: TARGET_SELECTION ) )
181
185
}
@@ -193,15 +197,17 @@ pub trait CommandExt: Sized {
193
197
. _arg (
194
198
optional_multi_opt ( "bin" , "NAME" , bin)
195
199
. help_heading ( heading:: TARGET_SELECTION )
196
- . add ( clap_complete:: ArgValueCandidates :: new ( get_bin_candidates) ) ,
200
+ . add ( clap_complete:: ArgValueCandidates :: new ( || {
201
+ get_crate_candidates ( TargetKind :: Bin ) . unwrap_or_default ( )
202
+ } ) ) ,
197
203
)
198
204
. _arg ( flag ( "examples" , examples) . help_heading ( heading:: TARGET_SELECTION ) )
199
205
. _arg (
200
206
optional_multi_opt ( "example" , "NAME" , example)
201
207
. help_heading ( heading:: TARGET_SELECTION )
202
- . add ( clap_complete:: ArgValueCandidates :: new (
203
- get_example_candidates ,
204
- ) ) ,
208
+ . add ( clap_complete:: ArgValueCandidates :: new ( || {
209
+ get_crate_candidates ( TargetKind :: ExampleBin ) . unwrap_or_default ( )
210
+ } ) ) ,
205
211
)
206
212
}
207
213
@@ -215,15 +221,17 @@ pub trait CommandExt: Sized {
215
221
self . _arg (
216
222
optional_multi_opt ( "bin" , "NAME" , bin)
217
223
. help_heading ( heading:: TARGET_SELECTION )
218
- . add ( clap_complete:: ArgValueCandidates :: new ( get_bin_candidates) ) ,
224
+ . add ( clap_complete:: ArgValueCandidates :: new ( || {
225
+ get_crate_candidates ( TargetKind :: Bin ) . unwrap_or_default ( )
226
+ } ) ) ,
219
227
)
220
228
. _arg ( flag ( "bins" , bins) . help_heading ( heading:: TARGET_SELECTION ) )
221
229
. _arg (
222
230
optional_multi_opt ( "example" , "NAME" , example)
223
231
. help_heading ( heading:: TARGET_SELECTION )
224
- . add ( clap_complete:: ArgValueCandidates :: new (
225
- get_example_candidates ,
226
- ) ) ,
232
+ . add ( clap_complete:: ArgValueCandidates :: new ( || {
233
+ get_crate_candidates ( TargetKind :: ExampleBin ) . unwrap_or_default ( )
234
+ } ) ) ,
227
235
)
228
236
. _arg ( flag ( "examples" , examples) . help_heading ( heading:: TARGET_SELECTION ) )
229
237
}
@@ -232,14 +240,16 @@ pub trait CommandExt: Sized {
232
240
self . _arg (
233
241
optional_multi_opt ( "bin" , "NAME" , bin)
234
242
. help_heading ( heading:: TARGET_SELECTION )
235
- . add ( clap_complete:: ArgValueCandidates :: new ( get_bin_candidates) ) ,
243
+ . add ( clap_complete:: ArgValueCandidates :: new ( || {
244
+ get_crate_candidates ( TargetKind :: Bin ) . unwrap_or_default ( )
245
+ } ) ) ,
236
246
)
237
247
. _arg (
238
248
optional_multi_opt ( "example" , "NAME" , example)
239
249
. help_heading ( heading:: TARGET_SELECTION )
240
- . add ( clap_complete:: ArgValueCandidates :: new (
241
- get_example_candidates ,
242
- ) ) ,
250
+ . add ( clap_complete:: ArgValueCandidates :: new ( || {
251
+ get_crate_candidates ( TargetKind :: ExampleBin ) . unwrap_or_default ( )
252
+ } ) ) ,
243
253
)
244
254
}
245
255
@@ -1210,70 +1220,19 @@ fn get_feature_candidates() -> CargoResult<Vec<clap_complete::CompletionCandidat
1210
1220
Ok ( feature_candidates)
1211
1221
}
1212
1222
1213
- fn get_example_candidates ( ) -> Vec < clap_complete:: CompletionCandidate > {
1214
- get_targets_from_metadata ( )
1215
- . unwrap_or_default ( )
1216
- . into_iter ( )
1217
- . filter_map ( |( pkg_name, target) | match target. kind ( ) {
1218
- TargetKind :: ExampleBin => Some (
1219
- clap_complete:: CompletionCandidate :: new ( target. name ( ) )
1220
- . help ( Some ( format ! ( "(from {})" , pkg_name) . into ( ) ) ) ,
1221
- ) ,
1222
- _ => None ,
1223
- } )
1224
- . collect :: < Vec < _ > > ( )
1225
- }
1226
-
1227
- fn get_bench_candidates ( ) -> Vec < clap_complete:: CompletionCandidate > {
1228
- get_targets_from_metadata ( )
1229
- . unwrap_or_default ( )
1230
- . into_iter ( )
1231
- . filter_map ( |( pkg_name, target) | match target. kind ( ) {
1232
- TargetKind :: Bench => Some (
1233
- clap_complete:: CompletionCandidate :: new ( target. name ( ) )
1234
- . help ( Some ( format ! ( "(from {})" , pkg_name) . into ( ) ) ) ,
1235
- ) ,
1236
- _ => None ,
1237
- } )
1238
- . collect :: < Vec < _ > > ( )
1239
- }
1240
-
1241
- fn get_test_candidates ( ) -> Vec < clap_complete:: CompletionCandidate > {
1242
- get_targets_from_metadata ( )
1243
- . unwrap_or_default ( )
1244
- . into_iter ( )
1245
- . filter_map ( |( pkg_name, target) | match target. kind ( ) {
1246
- TargetKind :: Test => Some (
1247
- clap_complete:: CompletionCandidate :: new ( target. name ( ) )
1248
- . help ( Some ( format ! ( "(from {})" , pkg_name) . into ( ) ) ) ,
1249
- ) ,
1250
- _ => None ,
1251
- } )
1252
- . collect :: < Vec < _ > > ( )
1253
- }
1254
-
1255
- fn get_bin_candidates ( ) -> Vec < clap_complete:: CompletionCandidate > {
1256
- get_targets_from_metadata ( )
1257
- . unwrap_or_default ( )
1258
- . into_iter ( )
1259
- . filter_map ( |( pkg_name, target) | match target. kind ( ) {
1260
- TargetKind :: Bin => Some (
1261
- clap_complete:: CompletionCandidate :: new ( target. name ( ) )
1262
- . help ( Some ( format ! ( "(from {})" , pkg_name) . into ( ) ) ) ,
1263
- ) ,
1264
- _ => None ,
1265
- } )
1266
- . collect :: < Vec < _ > > ( )
1267
- }
1268
-
1269
- fn get_targets_from_metadata ( ) -> CargoResult < Vec < ( InternedString , Target ) > > {
1223
+ fn get_crate_candidates ( kind : TargetKind ) -> CargoResult < Vec < clap_complete:: CompletionCandidate > > {
1270
1224
let gctx = new_gctx_for_completions ( ) ?;
1271
1225
1272
1226
let ws = Workspace :: new ( & find_root_manifest_for_wd ( gctx. cwd ( ) ) ?, & gctx) ?;
1273
1227
1274
1228
let targets = ws
1275
1229
. members ( )
1276
1230
. flat_map ( |pkg| pkg. targets ( ) . into_iter ( ) . cloned ( ) . map ( |t| ( pkg. name ( ) , t) ) )
1231
+ . filter ( |( _, target) | * target. kind ( ) == kind)
1232
+ . map ( |( pkg_name, target) | {
1233
+ clap_complete:: CompletionCandidate :: new ( target. name ( ) )
1234
+ . help ( Some ( format ! ( "(from {})" , pkg_name) . into ( ) ) )
1235
+ } )
1277
1236
. collect :: < Vec < _ > > ( ) ;
1278
1237
1279
1238
Ok ( targets)
0 commit comments