@@ -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
@@ -253,8 +263,7 @@ pub trait CommandExt: Sized {
253
263
. short ( 'F' )
254
264
. help_heading ( heading:: FEATURE_SELECTION )
255
265
. add ( clap_complete:: ArgValueCandidates :: new ( || {
256
- let candidates = get_feature_candidates ( ) ;
257
- candidates. unwrap_or_default ( )
266
+ get_feature_candidates ( ) . unwrap_or_default ( )
258
267
} ) ) ,
259
268
)
260
269
. _arg (
@@ -414,10 +423,7 @@ pub trait CommandExt: Sized {
414
423
. value_name ( "VCS" )
415
424
. value_parser ( [ "git" , "hg" , "pijul" , "fossil" , "none" ] ) ,
416
425
)
417
- . _arg (
418
- flag ( "bin" , "Use a binary (application) template [default]" )
419
- . add ( clap_complete:: ArgValueCandidates :: new ( get_bin_candidates) ) ,
420
- )
426
+ . _arg ( flag ( "bin" , "Use a binary (application) template [default]" ) )
421
427
. _arg ( flag ( "lib" , "Use a library template" ) )
422
428
. _arg (
423
429
opt ( "edition" , "Edition to set for the crate generated" )
@@ -1194,8 +1200,8 @@ fn default_profile_candidates() -> Vec<clap_complete::CompletionCandidate> {
1194
1200
1195
1201
fn get_feature_candidates ( ) -> CargoResult < Vec < clap_complete:: CompletionCandidate > > {
1196
1202
let gctx = new_gctx_for_completions ( ) ?;
1197
- let manifest_path = find_root_manifest_for_wd ( gctx . cwd ( ) ) ? ;
1198
- let ws = Workspace :: new ( & manifest_path , & gctx) ?;
1203
+
1204
+ let ws = Workspace :: new ( & find_root_manifest_for_wd ( gctx . cwd ( ) ) ? , & gctx) ?;
1199
1205
let mut feature_candidates = Vec :: new ( ) ;
1200
1206
1201
1207
// Process all packages in the workspace
@@ -1204,8 +1210,14 @@ fn get_feature_candidates() -> CargoResult<Vec<clap_complete::CompletionCandidat
1204
1210
1205
1211
// Add direct features with package info
1206
1212
for feature_name in package. summary ( ) . features ( ) . keys ( ) {
1213
+ let order = if ws. current_opt ( ) . map ( |p| p. name ( ) ) == Some ( package_name) {
1214
+ 0
1215
+ } else {
1216
+ 1
1217
+ } ;
1207
1218
feature_candidates. push (
1208
1219
clap_complete:: CompletionCandidate :: new ( feature_name)
1220
+ . display_order ( Some ( order) )
1209
1221
. help ( Some ( format ! ( "(from {})" , package_name) . into ( ) ) ) ,
1210
1222
) ;
1211
1223
}
@@ -1214,60 +1226,25 @@ fn get_feature_candidates() -> CargoResult<Vec<clap_complete::CompletionCandidat
1214
1226
Ok ( feature_candidates)
1215
1227
}
1216
1228
1217
- fn get_example_candidates ( ) -> Vec < clap_complete:: CompletionCandidate > {
1218
- get_targets_from_metadata ( )
1219
- . unwrap_or_default ( )
1220
- . into_iter ( )
1221
- . filter_map ( |target| match target. kind ( ) {
1222
- TargetKind :: ExampleBin => Some ( clap_complete:: CompletionCandidate :: new ( target. name ( ) ) ) ,
1223
- _ => None ,
1224
- } )
1225
- . collect :: < Vec < _ > > ( )
1226
- }
1227
-
1228
- fn get_bench_candidates ( ) -> Vec < clap_complete:: CompletionCandidate > {
1229
- get_targets_from_metadata ( )
1230
- . unwrap_or_default ( )
1231
- . into_iter ( )
1232
- . filter_map ( |target| match target. kind ( ) {
1233
- TargetKind :: Bench => Some ( clap_complete:: CompletionCandidate :: new ( target. name ( ) ) ) ,
1234
- _ => None ,
1235
- } )
1236
- . collect :: < Vec < _ > > ( )
1237
- }
1229
+ fn get_crate_candidates ( kind : TargetKind ) -> CargoResult < Vec < clap_complete:: CompletionCandidate > > {
1230
+ let gctx = new_gctx_for_completions ( ) ?;
1238
1231
1239
- fn get_test_candidates ( ) -> Vec < clap_complete:: CompletionCandidate > {
1240
- get_targets_from_metadata ( )
1241
- . unwrap_or_default ( )
1242
- . into_iter ( )
1243
- . filter_map ( |target| match target. kind ( ) {
1244
- TargetKind :: Test => Some ( clap_complete:: CompletionCandidate :: new ( target. name ( ) ) ) ,
1245
- _ => None ,
1246
- } )
1247
- . collect :: < Vec < _ > > ( )
1248
- }
1232
+ let ws = Workspace :: new ( & find_root_manifest_for_wd ( gctx. cwd ( ) ) ?, & gctx) ?;
1249
1233
1250
- fn get_bin_candidates ( ) -> Vec < clap_complete:: CompletionCandidate > {
1251
- get_targets_from_metadata ( )
1252
- . unwrap_or_default ( )
1253
- . into_iter ( )
1254
- . filter_map ( |target| match target. kind ( ) {
1255
- TargetKind :: Bin => Some ( clap_complete:: CompletionCandidate :: new ( target. name ( ) ) ) ,
1256
- _ => None ,
1234
+ let targets = ws
1235
+ . members ( )
1236
+ . flat_map ( |pkg| pkg. targets ( ) . into_iter ( ) . cloned ( ) . map ( |t| ( pkg. name ( ) , t) ) )
1237
+ . filter ( |( _, target) | * target. kind ( ) == kind)
1238
+ . map ( |( pkg_name, target) | {
1239
+ let order = if ws. current_opt ( ) . map ( |p| p. name ( ) ) == Some ( pkg_name) {
1240
+ 0
1241
+ } else {
1242
+ 1
1243
+ } ;
1244
+ clap_complete:: CompletionCandidate :: new ( target. name ( ) )
1245
+ . display_order ( Some ( order) )
1246
+ . help ( Some ( format ! ( "(from {})" , pkg_name) . into ( ) ) )
1257
1247
} )
1258
- . collect :: < Vec < _ > > ( )
1259
- }
1260
-
1261
- fn get_targets_from_metadata ( ) -> CargoResult < Vec < Target > > {
1262
- let cwd = std:: env:: current_dir ( ) ?;
1263
- let gctx = GlobalContext :: new ( shell:: Shell :: new ( ) , cwd. clone ( ) , cargo_home_with_cwd ( & cwd) ?) ;
1264
- let ws = Workspace :: new ( & find_root_manifest_for_wd ( & cwd) ?, & gctx) ?;
1265
-
1266
- let packages = ws. members ( ) . collect :: < Vec < _ > > ( ) ;
1267
-
1268
- let targets = packages
1269
- . into_iter ( )
1270
- . flat_map ( |pkg| pkg. targets ( ) . into_iter ( ) . cloned ( ) )
1271
1248
. collect :: < Vec < _ > > ( ) ;
1272
1249
1273
1250
Ok ( targets)
@@ -1319,9 +1296,9 @@ fn get_target_triples_from_rustup() -> CargoResult<Vec<clap_complete::Completion
1319
1296
}
1320
1297
1321
1298
fn get_target_triples_from_rustc ( ) -> CargoResult < Vec < clap_complete:: CompletionCandidate > > {
1322
- let cwd = std :: env :: current_dir ( ) ?;
1323
- let gctx = GlobalContext :: new ( shell :: Shell :: new ( ) , cwd . clone ( ) , cargo_home_with_cwd ( & cwd ) ? ) ;
1324
- let ws = Workspace :: new ( & find_root_manifest_for_wd ( & PathBuf :: from ( & cwd) ) ?, & gctx) ;
1299
+ let gctx = new_gctx_for_completions ( ) ?;
1300
+
1301
+ let ws = Workspace :: new ( & find_root_manifest_for_wd ( gctx . cwd ( ) ) ?, & gctx) ;
1325
1302
1326
1303
let rustc = gctx. load_global_rustc ( ws. as_ref ( ) . ok ( ) ) ?;
1327
1304
0 commit comments