Skip to content

Commit 7e526b6

Browse files
committed
Add expression fill mode variant for filling with underscore expressions
1 parent 8fb2dcc commit 7e526b6

File tree

31 files changed

+172
-125
lines changed

31 files changed

+172
-125
lines changed

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((

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
})

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,

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 {

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,

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

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
})

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 => {

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 {

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) {

crates/ide-assists/src/utils.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use hir::{
88
};
99
use ide_db::{
1010
RootDatabase,
11+
assists::ExprFillDefaultMode,
1112
famous_defs::FamousDefs,
1213
path_transform::PathTransform,
1314
syntax_helpers::{node_ext::preorder_expr, prettify_macro_expansion},
@@ -27,7 +28,10 @@ use syntax::{
2728
ted,
2829
};
2930

30-
use crate::assist_context::{AssistContext, SourceChangeBuilder};
31+
use crate::{
32+
AssistConfig,
33+
assist_context::{AssistContext, SourceChangeBuilder},
34+
};
3135

3236
mod gen_trait_fn_body;
3337
pub(crate) mod ref_field_expr;
@@ -174,6 +178,7 @@ pub fn filter_assoc_items(
174178
/// inserted.
175179
pub fn add_trait_assoc_items_to_impl(
176180
sema: &Semantics<'_, RootDatabase>,
181+
config: &AssistConfig,
177182
original_items: &[InFile<ast::AssocItem>],
178183
trait_: hir::Trait,
179184
impl_: &ast::Impl,
@@ -219,7 +224,14 @@ pub fn add_trait_assoc_items_to_impl(
219224
match &item {
220225
ast::AssocItem::Fn(fn_) if fn_.body().is_none() => {
221226
let body = AstNodeEdit::indent(
222-
&make::block_expr(None, Some(make::ext::expr_todo())),
227+
&make::block_expr(
228+
None,
229+
Some(match config.expr_fill_default {
230+
ExprFillDefaultMode::Todo => make::ext::expr_todo(),
231+
ExprFillDefaultMode::Underscore => make::ext::expr_underscore(),
232+
ExprFillDefaultMode::Default => make::ext::expr_todo(),
233+
}),
234+
),
223235
new_indent_level,
224236
);
225237
ted::replace(fn_.get_or_create_body().syntax(), body.clone_for_update().syntax())

crates/ide-completion/src/item.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ impl fmt::Debug for CompletionItem {
135135
},
136136
CompletionItemRefMode::Dereference => "*",
137137
};
138-
s.field("ref_match", &format!("{}@{offset:?}", prefix));
138+
s.field("ref_match", &format!("{prefix}@{offset:?}"));
139139
}
140140
if self.trigger_call_info {
141141
s.field("trigger_call_info", &true);

crates/ide-db/src/assists.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,3 +169,15 @@ impl AssistResolveStrategy {
169169

170170
#[derive(Clone, Debug)]
171171
pub struct GroupLabel(pub String);
172+
173+
#[derive(Clone, Debug, PartialEq, Eq)]
174+
pub enum ExprFillDefaultMode {
175+
Todo,
176+
Default,
177+
Underscore,
178+
}
179+
impl Default for ExprFillDefaultMode {
180+
fn default() -> Self {
181+
Self::Todo
182+
}
183+
}

crates/ide-diagnostics/src/handlers/missing_fields.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,13 @@ use hir::{
55
sym,
66
};
77
use ide_db::{
8-
FxHashMap, assists::Assist, famous_defs::FamousDefs,
9-
imports::import_assets::item_for_path_search, source_change::SourceChange,
10-
syntax_helpers::tree_diff::diff, text_edit::TextEdit,
8+
FxHashMap,
9+
assists::{Assist, ExprFillDefaultMode},
10+
famous_defs::FamousDefs,
11+
imports::import_assets::item_for_path_search,
12+
source_change::SourceChange,
13+
syntax_helpers::tree_diff::diff,
14+
text_edit::TextEdit,
1115
use_trivial_constructor::use_trivial_constructor,
1216
};
1317
use stdx::format_to;
@@ -102,8 +106,9 @@ fn fixes(ctx: &DiagnosticsContext<'_>, d: &hir::MissingFields) -> Option<Vec<Ass
102106
});
103107

104108
let generate_fill_expr = |ty: &Type| match ctx.config.expr_fill_default {
105-
crate::ExprFillDefaultMode::Todo => make::ext::expr_todo(),
106-
crate::ExprFillDefaultMode::Default => {
109+
ExprFillDefaultMode::Todo => make::ext::expr_todo(),
110+
ExprFillDefaultMode::Underscore => make::ext::expr_underscore(),
111+
ExprFillDefaultMode::Default => {
107112
get_default_constructor(ctx, d, ty).unwrap_or_else(make::ext::expr_todo)
108113
}
109114
};

0 commit comments

Comments
 (0)