Skip to content

Commit ca1340a

Browse files
committed
internal: extract default_fill_expr to utils
1 parent 76de1de commit ca1340a

5 files changed

Lines changed: 36 additions & 106 deletions

File tree

crates/ide-assists/src/handlers/add_missing_match_arms.rs

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use std::iter::{self, Peekable};
33
use either::Either;
44
use hir::{Adt, AsAssocItem, Crate, FindPathConfig, HasAttrs, ModuleDef, Semantics};
55
use ide_db::RootDatabase;
6-
use ide_db::assists::ExprFillDefaultMode;
76
use ide_db::syntax_helpers::suggest_name;
87
use ide_db::{famous_defs::FamousDefs, helpers::mod_path_to_ast};
98
use itertools::Itertools;
@@ -229,17 +228,7 @@ pub(crate) fn add_missing_match_arms(acc: &mut Assists, ctx: &AssistContext<'_>)
229228
// filter out hidden patterns because they're handled by the catch-all arm
230229
!hidden
231230
})
232-
.map(|(pat, _)| {
233-
make.match_arm(
234-
pat,
235-
None,
236-
match ctx.config.expr_fill_default {
237-
ExprFillDefaultMode::Todo => make::ext::expr_todo(),
238-
ExprFillDefaultMode::Underscore => make::ext::expr_underscore(),
239-
ExprFillDefaultMode::Default => make::ext::expr_todo(),
240-
},
241-
)
242-
});
231+
.map(|(pat, _)| make.match_arm(pat, None, utils::expr_fill_default(ctx.config)));
243232

244233
let mut arms: Vec<_> = match_arm_list
245234
.arms()
@@ -266,11 +255,7 @@ pub(crate) fn add_missing_match_arms(acc: &mut Assists, ctx: &AssistContext<'_>)
266255
let arm = make.match_arm(
267256
make.wildcard_pat().into(),
268257
None,
269-
match ctx.config.expr_fill_default {
270-
ExprFillDefaultMode::Todo => make::ext::expr_todo(),
271-
ExprFillDefaultMode::Underscore => make::ext::expr_underscore(),
272-
ExprFillDefaultMode::Default => make::ext::expr_todo(),
273-
},
258+
utils::expr_fill_default(ctx.config),
274259
);
275260
arms.push(arm);
276261
}

crates/ide-assists/src/handlers/desugar_try_expr.rs

Lines changed: 15 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
use std::iter;
22

3-
use ide_db::{
4-
assists::{AssistId, ExprFillDefaultMode},
5-
ty_filter::TryEnum,
6-
};
3+
use ide_db::{assists::AssistId, ty_filter::TryEnum};
74
use syntax::{
85
AstNode, T,
96
ast::{
@@ -80,16 +77,9 @@ pub(crate) fn desugar_try_expr(acc: &mut Assists, ctx: &AssistContext<'_>) -> Op
8077
)
8178
.into(),
8279
};
83-
let sad_expr = match try_enum {
84-
TryEnum::Option => make.expr_return(Some(make.expr_path(make.ident_path("None")))),
85-
TryEnum::Result => make.expr_return(Some(
86-
make.expr_call(
87-
make.expr_path(make.ident_path("Err")),
88-
make.arg_list(iter::once(make.expr_path(make.ident_path("err")))),
89-
)
90-
.into(),
91-
)),
92-
};
80+
let sad_expr = make.expr_return(Some(sad_expr(try_enum, &make, || {
81+
make.expr_path(make.ident_path("err"))
82+
})));
9383

9484
let happy_arm = make.match_arm(
9585
try_enum.happy_pattern(make.ident_pat(false, false, make.name("it")).into()),
@@ -123,48 +113,15 @@ pub(crate) fn desugar_try_expr(acc: &mut Assists, ctx: &AssistContext<'_>) -> Op
123113
let mut editor = builder.make_editor(let_stmt.syntax());
124114

125115
let indent_level = IndentLevel::from_node(let_stmt.syntax());
116+
let fill_expr = || crate::utils::expr_fill_default(ctx.config);
126117
let new_let_stmt = make.let_else_stmt(
127118
try_enum.happy_pattern(pat),
128119
let_stmt.ty(),
129120
expr,
130121
make.block_expr(
131122
iter::once(
132123
make.expr_stmt(
133-
make.expr_return(Some(match try_enum {
134-
TryEnum::Option => make.expr_path(make.ident_path("None")),
135-
TryEnum::Result => make
136-
.expr_call(
137-
make.expr_path(make.ident_path("Err")),
138-
make.arg_list(iter::once(
139-
match ctx.config.expr_fill_default {
140-
ExprFillDefaultMode::Todo => make
141-
.expr_macro(
142-
make.ident_path("todo"),
143-
make.token_tree(
144-
syntax::SyntaxKind::L_PAREN,
145-
[],
146-
),
147-
)
148-
.into(),
149-
ExprFillDefaultMode::Underscore => {
150-
make.expr_underscore().into()
151-
}
152-
ExprFillDefaultMode::Default => make
153-
.expr_macro(
154-
make.ident_path("todo"),
155-
make.token_tree(
156-
syntax::SyntaxKind::L_PAREN,
157-
[],
158-
),
159-
)
160-
.into(),
161-
},
162-
)),
163-
)
164-
.into(),
165-
}))
166-
.indent(indent_level + 1)
167-
.into(),
124+
make.expr_return(Some(sad_expr(try_enum, &make, fill_expr))).into(),
168125
)
169126
.into(),
170127
),
@@ -181,6 +138,15 @@ pub(crate) fn desugar_try_expr(acc: &mut Assists, ctx: &AssistContext<'_>) -> Op
181138
Some(())
182139
}
183140

141+
fn sad_expr(try_enum: TryEnum, make: &SyntaxFactory, err: impl Fn() -> ast::Expr) -> ast::Expr {
142+
match try_enum {
143+
TryEnum::Option => make.expr_path(make.ident_path("None")),
144+
TryEnum::Result => make
145+
.expr_call(make.expr_path(make.ident_path("Err")), make.arg_list(iter::once(err())))
146+
.into(),
147+
}
148+
}
149+
184150
#[cfg(test)]
185151
mod tests {
186152
use super::*;

crates/ide-assists/src/handlers/generate_blanket_trait_impl.rs

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@ use crate::{
55
use hir::{HasCrate, Semantics};
66
use ide_db::{
77
RootDatabase,
8-
assists::{AssistId, AssistKind, ExprFillDefaultMode},
8+
assists::{AssistId, AssistKind},
99
famous_defs::FamousDefs,
1010
syntax_helpers::suggest_name,
1111
};
1212
use syntax::{
1313
AstNode,
1414
ast::{
15-
self, AssocItem, BlockExpr, GenericParam, HasAttrs, HasGenericParams, HasName,
16-
HasTypeBounds, HasVisibility, edit::AstNodeEdit, make,
15+
self, AssocItem, GenericParam, HasAttrs, HasGenericParams, HasName, HasTypeBounds,
16+
HasVisibility, edit::AstNodeEdit, make,
1717
},
1818
syntax_editor::Position,
1919
};
@@ -269,7 +269,7 @@ fn todo_fn(f: &ast::Fn, config: &AssistConfig) -> ast::Fn {
269269
f.generic_param_list(),
270270
f.where_clause(),
271271
params,
272-
default_block(config),
272+
make::block_expr(None, Some(crate::utils::expr_fill_default(config))),
273273
f.ret_type(),
274274
f.async_token().is_some(),
275275
f.const_token().is_some(),
@@ -278,15 +278,6 @@ fn todo_fn(f: &ast::Fn, config: &AssistConfig) -> ast::Fn {
278278
)
279279
}
280280

281-
fn default_block(config: &AssistConfig) -> BlockExpr {
282-
let expr = match config.expr_fill_default {
283-
ExprFillDefaultMode::Todo => make::ext::expr_todo(),
284-
ExprFillDefaultMode::Underscore => make::ext::expr_underscore(),
285-
ExprFillDefaultMode::Default => make::ext::expr_todo(),
286-
};
287-
make::block_expr(None, Some(expr))
288-
}
289-
290281
fn cfg_attrs(node: &impl HasAttrs) -> impl Iterator<Item = ast::Attr> {
291282
node.attrs().filter(|attr| attr.as_simple_call().is_some_and(|(name, _arg)| name == "cfg"))
292283
}

crates/ide-assists/src/handlers/generate_function.rs

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use hir::{
44
};
55
use ide_db::{
66
FileId, FxHashMap, FxHashSet, RootDatabase, SnippetCap,
7-
assists::ExprFillDefaultMode,
87
defs::{Definition, NameRefClass},
98
famous_defs::FamousDefs,
109
helpers::is_editable_crate,
@@ -24,7 +23,7 @@ use syntax::{
2423

2524
use crate::{
2625
AssistContext, AssistId, Assists,
27-
utils::{convert_reference_type, find_struct_impl},
26+
utils::{convert_reference_type, expr_fill_default, find_struct_impl},
2827
};
2928

3029
// Assist: generate_function
@@ -286,11 +285,7 @@ impl FunctionBuilder {
286285
target_module,
287286
&mut necessary_generic_params,
288287
);
289-
let placeholder_expr = match ctx.config.expr_fill_default {
290-
ExprFillDefaultMode::Todo => make::ext::expr_todo(),
291-
ExprFillDefaultMode::Underscore => make::ext::expr_underscore(),
292-
ExprFillDefaultMode::Default => make::ext::expr_todo(),
293-
};
288+
let placeholder_expr = expr_fill_default(ctx.config);
294289
fn_body = make::block_expr(vec![], Some(placeholder_expr));
295290
};
296291

@@ -345,11 +340,7 @@ impl FunctionBuilder {
345340
let (generic_param_list, where_clause) =
346341
fn_generic_params(ctx, necessary_generic_params, &target)?;
347342

348-
let placeholder_expr = match ctx.config.expr_fill_default {
349-
ExprFillDefaultMode::Todo => make::ext::expr_todo(),
350-
ExprFillDefaultMode::Underscore => make::ext::expr_underscore(),
351-
ExprFillDefaultMode::Default => make::ext::expr_todo(),
352-
};
343+
let placeholder_expr = expr_fill_default(ctx.config);
353344
let fn_body = make::block_expr(vec![], Some(placeholder_expr));
354345

355346
Some(Self {
@@ -465,11 +456,7 @@ fn make_fn_body_as_new_function(
465456
let adt_info = adt_info.as_ref()?;
466457

467458
let path_self = make::ext::ident_path("Self");
468-
let placeholder_expr = match ctx.config.expr_fill_default {
469-
ExprFillDefaultMode::Todo => make::ext::expr_todo(),
470-
ExprFillDefaultMode::Underscore => make::ext::expr_underscore(),
471-
ExprFillDefaultMode::Default => make::ext::expr_todo(),
472-
};
459+
let placeholder_expr = expr_fill_default(ctx.config);
473460
let tail_expr = if let Some(strukt) = adt_info.adt.as_struct() {
474461
match strukt.kind(ctx.db()) {
475462
StructKind::Record => {

crates/ide-assists/src/utils.rs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -248,17 +248,9 @@ pub fn add_trait_assoc_items_to_impl(
248248
.filter_map(|item| match item {
249249
ast::AssocItem::Fn(fn_) if fn_.body().is_none() => {
250250
let fn_ = fn_.clone_subtree();
251-
let new_body = &make::block_expr(
252-
None,
253-
Some(match config.expr_fill_default {
254-
ExprFillDefaultMode::Todo => make::ext::expr_todo(),
255-
ExprFillDefaultMode::Underscore => make::ext::expr_underscore(),
256-
ExprFillDefaultMode::Default => make::ext::expr_todo(),
257-
}),
258-
);
259-
let new_body = AstNodeEdit::indent(new_body, IndentLevel::single());
251+
let new_body = make::block_expr(None, Some(expr_fill_default(config)));
260252
let mut fn_editor = SyntaxEditor::new(fn_.syntax().clone());
261-
fn_.replace_or_insert_body(&mut fn_editor, new_body);
253+
fn_.replace_or_insert_body(&mut fn_editor, new_body.clone_for_update());
262254
let new_fn_ = fn_editor.finish().new_root().clone();
263255
ast::AssocItem::cast(new_fn_)
264256
}
@@ -465,6 +457,15 @@ fn check_pat_variant_nested_or_literal_with_depth(
465457
}
466458
}
467459

460+
pub(crate) fn expr_fill_default(config: &AssistConfig) -> ast::Expr {
461+
let make = SyntaxFactory::without_mappings();
462+
match config.expr_fill_default {
463+
ExprFillDefaultMode::Todo => make.expr_todo(),
464+
ExprFillDefaultMode::Underscore => make.expr_underscore().into(),
465+
ExprFillDefaultMode::Default => make.expr_todo(),
466+
}
467+
}
468+
468469
// Uses a syntax-driven approach to find any impl blocks for the struct that
469470
// exist within the module/file
470471
//

0 commit comments

Comments
 (0)