Skip to content

Commit bb906cc

Browse files
authored
Merge pull request #19704 from Veykril/push-wrvznvvpvtvp
Add expression fill mode variant for filling with underscore expressions
2 parents 36ed4aa + 7e064c3 commit bb906cc

File tree

31 files changed

+172
-125
lines changed

31 files changed

+172
-125
lines changed

src/tools/rust-analyzer/crates/hir-def/src/nameres/mod_resolution.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ impl ModDir {
8686
let dir_path = if root_dir_owner {
8787
DirPath::empty()
8888
} else {
89-
DirPath::new(format!("{}/", name))
89+
DirPath::new(format!("{name}/"))
9090
};
9191
if let Some(mod_dir) = self.child(dir_path, !root_dir_owner) {
9292
return Ok((

src/tools/rust-analyzer/crates/hir-expand/src/db.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -752,8 +752,7 @@ fn check_tt_count(tt: &tt::TopSubtree) -> Result<(), ExpandResult<()>> {
752752
err: Some(ExpandError::other(
753753
tt.delimiter.open,
754754
format!(
755-
"macro invocation exceeds token limit: produced {} tokens, limit is {}",
756-
count, TOKEN_LIMIT,
755+
"macro invocation exceeds token limit: produced {count} tokens, limit is {TOKEN_LIMIT}",
757756
),
758757
)),
759758
})

src/tools/rust-analyzer/crates/hir-expand/src/lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,8 +206,7 @@ impl ExpandErrorKind {
206206
},
207207
None => RenderedExpandError {
208208
message: format!(
209-
"internal error: proc-macro map is missing error entry for crate {:?}",
210-
def_crate
209+
"internal error: proc-macro map is missing error entry for crate {def_crate:?}"
211210
),
212211
error: true,
213212
kind: RenderedExpandError::GENERAL_KIND,

src/tools/rust-analyzer/crates/ide-assists/src/assist_config.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
//! assists if we are allowed to.
66
77
use hir::ImportPathConfig;
8-
use ide_db::{SnippetCap, imports::insert_use::InsertUseConfig};
8+
use ide_db::{SnippetCap, assists::ExprFillDefaultMode, imports::insert_use::InsertUseConfig};
99

1010
use crate::AssistKind;
1111

@@ -21,6 +21,7 @@ pub struct AssistConfig {
2121
pub term_search_fuel: u64,
2222
pub term_search_borrowck: bool,
2323
pub code_action_grouping: bool,
24+
pub expr_fill_default: ExprFillDefaultMode,
2425
}
2526

2627
impl AssistConfig {

src/tools/rust-analyzer/crates/ide-assists/src/handlers/add_missing_impl_members.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ fn add_missing_impl_members_inner(
150150
let new_impl_def = edit.make_mut(impl_def.clone());
151151
let first_new_item = add_trait_assoc_items_to_impl(
152152
&ctx.sema,
153+
ctx.config,
153154
&missing_items,
154155
trait_,
155156
&new_impl_def,

src/tools/rust-analyzer/crates/ide-assists/src/handlers/add_missing_match_arms.rs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use std::iter::{self, Peekable};
33
use either::Either;
44
use hir::{Adt, Crate, HasAttrs, ImportPathConfig, ModuleDef, Semantics, sym};
55
use ide_db::RootDatabase;
6+
use ide_db::assists::ExprFillDefaultMode;
67
use ide_db::syntax_helpers::suggest_name;
78
use ide_db::{famous_defs::FamousDefs, helpers::mod_path_to_ast};
89
use itertools::Itertools;
@@ -216,7 +217,17 @@ pub(crate) fn add_missing_match_arms(acc: &mut Assists, ctx: &AssistContext<'_>)
216217
// filter out hidden patterns because they're handled by the catch-all arm
217218
!hidden
218219
})
219-
.map(|(pat, _)| make.match_arm(pat, None, make::ext::expr_todo()));
220+
.map(|(pat, _)| {
221+
make.match_arm(
222+
pat,
223+
None,
224+
match ctx.config.expr_fill_default {
225+
ExprFillDefaultMode::Todo => make::ext::expr_todo(),
226+
ExprFillDefaultMode::Underscore => make::ext::expr_underscore(),
227+
ExprFillDefaultMode::Default => make::ext::expr_todo(),
228+
},
229+
)
230+
});
220231

221232
let mut arms: Vec<_> = match_arm_list
222233
.arms()
@@ -246,7 +257,15 @@ pub(crate) fn add_missing_match_arms(acc: &mut Assists, ctx: &AssistContext<'_>)
246257

247258
if needs_catch_all_arm && !has_catch_all_arm {
248259
cov_mark::hit!(added_wildcard_pattern);
249-
let arm = make.match_arm(make.wildcard_pat().into(), None, make::ext::expr_todo());
260+
let arm = make.match_arm(
261+
make.wildcard_pat().into(),
262+
None,
263+
match ctx.config.expr_fill_default {
264+
ExprFillDefaultMode::Todo => make::ext::expr_todo(),
265+
ExprFillDefaultMode::Underscore => make::ext::expr_underscore(),
266+
ExprFillDefaultMode::Default => make::ext::expr_todo(),
267+
},
268+
);
250269
arms.push(arm);
251270
}
252271

src/tools/rust-analyzer/crates/ide-assists/src/handlers/destructure_tuple_binding.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ fn collect_data(ident_pat: IdentPat, ctx: &AssistContext<'_>) -> Option<TupleDat
142142
.map(|(id, ty)| {
143143
match name_generator.for_type(&ty, ctx.db(), ctx.edition()) {
144144
Some(name) => name,
145-
None => name_generator.suggest_name(&format!("_{}", id)),
145+
None => name_generator.suggest_name(&format!("_{id}")),
146146
}
147147
.to_string()
148148
})

src/tools/rust-analyzer/crates/ide-assists/src/handlers/generate_function.rs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use hir::{
44
};
55
use ide_db::{
66
FileId, FxHashMap, FxHashSet, RootDatabase, SnippetCap,
7+
assists::ExprFillDefaultMode,
78
defs::{Definition, NameRefClass},
89
famous_defs::FamousDefs,
910
helpers::is_editable_crate,
@@ -276,7 +277,11 @@ impl FunctionBuilder {
276277
target_module,
277278
&mut necessary_generic_params,
278279
);
279-
let placeholder_expr = make::ext::expr_todo();
280+
let placeholder_expr = match ctx.config.expr_fill_default {
281+
ExprFillDefaultMode::Todo => make::ext::expr_todo(),
282+
ExprFillDefaultMode::Underscore => make::ext::expr_underscore(),
283+
ExprFillDefaultMode::Default => make::ext::expr_todo(),
284+
};
280285
fn_body = make::block_expr(vec![], Some(placeholder_expr));
281286
};
282287

@@ -331,7 +336,11 @@ impl FunctionBuilder {
331336
let (generic_param_list, where_clause) =
332337
fn_generic_params(ctx, necessary_generic_params, &target)?;
333338

334-
let placeholder_expr = make::ext::expr_todo();
339+
let placeholder_expr = match ctx.config.expr_fill_default {
340+
ExprFillDefaultMode::Todo => make::ext::expr_todo(),
341+
ExprFillDefaultMode::Underscore => make::ext::expr_underscore(),
342+
ExprFillDefaultMode::Default => make::ext::expr_todo(),
343+
};
335344
let fn_body = make::block_expr(vec![], Some(placeholder_expr));
336345

337346
Some(Self {
@@ -444,7 +453,11 @@ fn make_fn_body_as_new_function(
444453
let adt_info = adt_info.as_ref()?;
445454

446455
let path_self = make::ext::ident_path("Self");
447-
let placeholder_expr = make::ext::expr_todo();
456+
let placeholder_expr = match ctx.config.expr_fill_default {
457+
ExprFillDefaultMode::Todo => make::ext::expr_todo(),
458+
ExprFillDefaultMode::Underscore => make::ext::expr_underscore(),
459+
ExprFillDefaultMode::Default => make::ext::expr_todo(),
460+
};
448461
let tail_expr = if let Some(strukt) = adt_info.adt.as_struct() {
449462
match strukt.kind(ctx.db()) {
450463
StructKind::Record => {

src/tools/rust-analyzer/crates/ide-assists/src/handlers/replace_derive_with_manual_impl.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use syntax::{
99
};
1010

1111
use crate::{
12-
AssistId,
12+
AssistConfig, AssistId,
1313
assist_context::{AssistContext, Assists, SourceChangeBuilder},
1414
utils::{
1515
DefaultMethods, IgnoreAssocItems, add_trait_assoc_items_to_impl, filter_assoc_items,
@@ -128,8 +128,14 @@ fn add_assist(
128128
acc.add(AssistId::refactor("replace_derive_with_manual_impl"), label, target, |builder| {
129129
let insert_after = ted::Position::after(builder.make_mut(adt.clone()).syntax());
130130
let impl_is_unsafe = trait_.map(|s| s.is_unsafe(ctx.db())).unwrap_or(false);
131-
let impl_def_with_items =
132-
impl_def_from_trait(&ctx.sema, adt, &annotated_name, trait_, replace_trait_path);
131+
let impl_def_with_items = impl_def_from_trait(
132+
&ctx.sema,
133+
ctx.config,
134+
adt,
135+
&annotated_name,
136+
trait_,
137+
replace_trait_path,
138+
);
133139
update_attribute(builder, old_derives, old_tree, old_trait_path, attr);
134140

135141
let trait_path = make::ty_path(replace_trait_path.clone());
@@ -217,6 +223,7 @@ fn add_assist(
217223

218224
fn impl_def_from_trait(
219225
sema: &hir::Semantics<'_, ide_db::RootDatabase>,
226+
config: &AssistConfig,
220227
adt: &ast::Adt,
221228
annotated_name: &ast::Name,
222229
trait_: Option<hir::Trait>,
@@ -241,7 +248,7 @@ fn impl_def_from_trait(
241248
let impl_def = generate_trait_impl(adt, make::ty_path(trait_path.clone()));
242249

243250
let first_assoc_item =
244-
add_trait_assoc_items_to_impl(sema, &trait_items, trait_, &impl_def, &target_scope);
251+
add_trait_assoc_items_to_impl(sema, config, &trait_items, trait_, &impl_def, &target_scope);
245252

246253
// Generate a default `impl` function body for the derived trait.
247254
if let ast::AssocItem::Fn(ref func) = first_assoc_item {

src/tools/rust-analyzer/crates/ide-assists/src/tests.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use expect_test::expect;
44
use hir::Semantics;
55
use ide_db::{
66
EditionedFileId, FileRange, RootDatabase, SnippetCap,
7+
assists::ExprFillDefaultMode,
78
base_db::SourceDatabase,
89
imports::insert_use::{ImportGranularity, InsertUseConfig},
910
source_change::FileSystemEdit,
@@ -35,6 +36,7 @@ pub(crate) const TEST_CONFIG: AssistConfig = AssistConfig {
3536
term_search_fuel: 400,
3637
term_search_borrowck: true,
3738
code_action_grouping: true,
39+
expr_fill_default: ExprFillDefaultMode::Todo,
3840
};
3941

4042
pub(crate) const TEST_CONFIG_NO_GROUPING: AssistConfig = AssistConfig {
@@ -54,6 +56,7 @@ pub(crate) const TEST_CONFIG_NO_GROUPING: AssistConfig = AssistConfig {
5456
term_search_fuel: 400,
5557
term_search_borrowck: true,
5658
code_action_grouping: false,
59+
expr_fill_default: ExprFillDefaultMode::Todo,
5760
};
5861

5962
pub(crate) const TEST_CONFIG_NO_SNIPPET_CAP: AssistConfig = AssistConfig {
@@ -73,6 +76,7 @@ pub(crate) const TEST_CONFIG_NO_SNIPPET_CAP: AssistConfig = AssistConfig {
7376
term_search_fuel: 400,
7477
term_search_borrowck: true,
7578
code_action_grouping: true,
79+
expr_fill_default: ExprFillDefaultMode::Todo,
7680
};
7781

7882
pub(crate) const TEST_CONFIG_IMPORT_ONE: AssistConfig = AssistConfig {
@@ -92,6 +96,7 @@ pub(crate) const TEST_CONFIG_IMPORT_ONE: AssistConfig = AssistConfig {
9296
term_search_fuel: 400,
9397
term_search_borrowck: true,
9498
code_action_grouping: true,
99+
expr_fill_default: ExprFillDefaultMode::Todo,
95100
};
96101

97102
pub(crate) fn with_single_file(text: &str) -> (RootDatabase, EditionedFileId) {

0 commit comments

Comments
 (0)