Skip to content

Commit 75a5c0a

Browse files
bors[bot]matklad
andauthored
Merge #8783
8783: internal: introduce `ast::make::ext` module with common shortcuts r=matklad a=matklad bors r+ 🤖 Co-authored-by: Aleksey Kladov <[email protected]>
2 parents 2fe329d + 4f3c0ad commit 75a5c0a

File tree

14 files changed

+104
-117
lines changed

14 files changed

+104
-117
lines changed

crates/ide_assists/src/handlers/early_return.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,7 @@ pub(crate) fn convert_to_guarded_return(acc: &mut Assists, ctx: &AssistContext)
130130
once(make::ident_pat(make::name("it")).into()),
131131
);
132132
let expr = {
133-
let name_ref = make::name_ref("it");
134-
let segment = make::path_segment(name_ref);
135-
let path = make::path_unqualified(segment);
133+
let path = make::ext::ident_path("it");
136134
make::expr_path(path)
137135
};
138136
make::match_arm(once(pat.into()), expr)

crates/ide_assists/src/handlers/expand_glob_import.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,7 @@ pub(crate) fn expand_glob_import(acc: &mut Assists, ctx: &AssistContext) -> Opti
6565

6666
let names_to_import = find_names_to_import(ctx, refs_in_target, imported_defs);
6767
let expanded = make::use_tree_list(names_to_import.iter().map(|n| {
68-
let path =
69-
make::path_unqualified(make::path_segment(make::name_ref(&n.to_string())));
68+
let path = make::ext::ident_path(&n.to_string());
7069
make::use_tree(path, None, None, false)
7170
}))
7271
.clone_for_update();

crates/ide_assists/src/handlers/extract_function.rs

Lines changed: 25 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -956,10 +956,10 @@ fn format_replacement(ctx: &AssistContext, fun: &Function, indent: IndentLevel)
956956
let args = fun.params.iter().map(|param| param.to_arg(ctx));
957957
let args = make::arg_list(args);
958958
let call_expr = if fun.self_param.is_some() {
959-
let self_arg = make::expr_path(make_path_from_text("self"));
959+
let self_arg = make::expr_path(make::ext::ident_path("self"));
960960
make::expr_method_call(self_arg, &fun.name, args)
961961
} else {
962-
let func = make::expr_path(make_path_from_text(&fun.name));
962+
let func = make::expr_path(make::ext::ident_path(&fun.name));
963963
make::expr_call(func, args)
964964
};
965965

@@ -1054,11 +1054,11 @@ impl FlowHandler {
10541054
make::expr_if(condition, block, None)
10551055
}
10561056
FlowHandler::IfOption { action } => {
1057-
let path = make_path_from_text("Some");
1057+
let path = make::ext::ident_path("Some");
10581058
let value_pat = make::ident_pat(make::name("value"));
10591059
let pattern = make::tuple_struct_pat(path, iter::once(value_pat.into()));
10601060
let cond = make::condition(call_expr, Some(pattern.into()));
1061-
let value = make::expr_path(make_path_from_text("value"));
1061+
let value = make::expr_path(make::ext::ident_path("value"));
10621062
let action_expr = action.make_result_handler(Some(value));
10631063
let action_stmt = make::expr_stmt(action_expr);
10641064
let then = make::block_expr(iter::once(action_stmt.into()), None);
@@ -1068,14 +1068,14 @@ impl FlowHandler {
10681068
let some_name = "value";
10691069

10701070
let some_arm = {
1071-
let path = make_path_from_text("Some");
1071+
let path = make::ext::ident_path("Some");
10721072
let value_pat = make::ident_pat(make::name(some_name));
10731073
let pat = make::tuple_struct_pat(path, iter::once(value_pat.into()));
1074-
let value = make::expr_path(make_path_from_text(some_name));
1074+
let value = make::expr_path(make::ext::ident_path(some_name));
10751075
make::match_arm(iter::once(pat.into()), value)
10761076
};
10771077
let none_arm = {
1078-
let path = make_path_from_text("None");
1078+
let path = make::ext::ident_path("None");
10791079
let pat = make::path_pat(path);
10801080
make::match_arm(iter::once(pat), none.make_result_handler(None))
10811081
};
@@ -1087,17 +1087,17 @@ impl FlowHandler {
10871087
let err_name = "value";
10881088

10891089
let ok_arm = {
1090-
let path = make_path_from_text("Ok");
1090+
let path = make::ext::ident_path("Ok");
10911091
let value_pat = make::ident_pat(make::name(ok_name));
10921092
let pat = make::tuple_struct_pat(path, iter::once(value_pat.into()));
1093-
let value = make::expr_path(make_path_from_text(ok_name));
1093+
let value = make::expr_path(make::ext::ident_path(ok_name));
10941094
make::match_arm(iter::once(pat.into()), value)
10951095
};
10961096
let err_arm = {
1097-
let path = make_path_from_text("Err");
1097+
let path = make::ext::ident_path("Err");
10981098
let value_pat = make::ident_pat(make::name(err_name));
10991099
let pat = make::tuple_struct_pat(path, iter::once(value_pat.into()));
1100-
let value = make::expr_path(make_path_from_text(err_name));
1100+
let value = make::expr_path(make::ext::ident_path(err_name));
11011101
make::match_arm(iter::once(pat.into()), err.make_result_handler(Some(value)))
11021102
};
11031103
let arms = make::match_arm_list(vec![ok_arm, err_arm]);
@@ -1107,13 +1107,9 @@ impl FlowHandler {
11071107
}
11081108
}
11091109

1110-
fn make_path_from_text(text: &str) -> ast::Path {
1111-
make::path_unqualified(make::path_segment(make::name_ref(text)))
1112-
}
1113-
11141110
fn path_expr_from_local(ctx: &AssistContext, var: Local) -> ast::Expr {
11151111
let name = var.name(ctx.db()).unwrap().to_string();
1116-
make::expr_path(make_path_from_text(&name))
1112+
make::expr_path(make::ext::ident_path(&name))
11171113
}
11181114

11191115
fn format_function(
@@ -1179,37 +1175,29 @@ fn make_ret_ty(ctx: &AssistContext, module: hir::Module, fun: &Function) -> Opti
11791175
fun_ty.make_ty(ctx, module)
11801176
}
11811177
FlowHandler::Try { kind: TryKind::Option } => {
1182-
make::ty_generic(make::name_ref("Option"), iter::once(fun_ty.make_ty(ctx, module)))
1178+
make::ext::ty_option(fun_ty.make_ty(ctx, module))
11831179
}
11841180
FlowHandler::Try { kind: TryKind::Result { ty: parent_ret_ty } } => {
11851181
let handler_ty = parent_ret_ty
11861182
.type_arguments()
11871183
.nth(1)
11881184
.map(|ty| make_ty(&ty, ctx, module))
11891185
.unwrap_or_else(make::ty_unit);
1190-
make::ty_generic(
1191-
make::name_ref("Result"),
1192-
vec![fun_ty.make_ty(ctx, module), handler_ty],
1193-
)
1186+
make::ext::ty_result(fun_ty.make_ty(ctx, module), handler_ty)
11941187
}
1195-
FlowHandler::If { .. } => make::ty_bool(),
1188+
FlowHandler::If { .. } => make::ext::ty_bool(),
11961189
FlowHandler::IfOption { action } => {
11971190
let handler_ty = action
11981191
.expr_ty(ctx)
11991192
.map(|ty| make_ty(&ty, ctx, module))
12001193
.unwrap_or_else(make::ty_unit);
1201-
make::ty_generic(make::name_ref("Option"), iter::once(handler_ty))
1202-
}
1203-
FlowHandler::MatchOption { .. } => {
1204-
make::ty_generic(make::name_ref("Option"), iter::once(fun_ty.make_ty(ctx, module)))
1194+
make::ext::ty_option(handler_ty)
12051195
}
1196+
FlowHandler::MatchOption { .. } => make::ext::ty_option(fun_ty.make_ty(ctx, module)),
12061197
FlowHandler::MatchResult { err } => {
12071198
let handler_ty =
12081199
err.expr_ty(ctx).map(|ty| make_ty(&ty, ctx, module)).unwrap_or_else(make::ty_unit);
1209-
make::ty_generic(
1210-
make::name_ref("Result"),
1211-
vec![fun_ty.make_ty(ctx, module), handler_ty],
1212-
)
1200+
make::ext::ty_result(fun_ty.make_ty(ctx, module), handler_ty)
12131201
}
12141202
};
12151203
Some(make::ret_type(ret_ty))
@@ -1296,7 +1284,7 @@ fn make_body(
12961284
TryKind::Option => "Some",
12971285
TryKind::Result { .. } => "Ok",
12981286
};
1299-
let func = make::expr_path(make_path_from_text(constructor));
1287+
let func = make::expr_path(make::ext::ident_path(constructor));
13001288
let args = make::arg_list(iter::once(tail_expr));
13011289
make::expr_call(func, args)
13021290
})
@@ -1306,16 +1294,16 @@ fn make_body(
13061294
with_tail_expr(block, lit_false.into())
13071295
}
13081296
FlowHandler::IfOption { .. } => {
1309-
let none = make::expr_path(make_path_from_text("None"));
1297+
let none = make::expr_path(make::ext::ident_path("None"));
13101298
with_tail_expr(block, none)
13111299
}
13121300
FlowHandler::MatchOption { .. } => map_tail_expr(block, |tail_expr| {
1313-
let some = make::expr_path(make_path_from_text("Some"));
1301+
let some = make::expr_path(make::ext::ident_path("Some"));
13141302
let args = make::arg_list(iter::once(tail_expr));
13151303
make::expr_call(some, args)
13161304
}),
13171305
FlowHandler::MatchResult { .. } => map_tail_expr(block, |tail_expr| {
1318-
let ok = make::expr_path(make_path_from_text("Ok"));
1306+
let ok = make::expr_path(make::ext::ident_path("Ok"));
13191307
let args = make::arg_list(iter::once(tail_expr));
13201308
make::expr_call(ok, args)
13211309
}),
@@ -1483,13 +1471,13 @@ fn make_rewritten_flow(handler: &FlowHandler, arg_expr: Option<ast::Expr>) -> Op
14831471
FlowHandler::IfOption { .. } => {
14841472
let expr = arg_expr.unwrap_or_else(|| make::expr_tuple(Vec::new()));
14851473
let args = make::arg_list(iter::once(expr));
1486-
make::expr_call(make::expr_path(make_path_from_text("Some")), args)
1474+
make::expr_call(make::expr_path(make::ext::ident_path("Some")), args)
14871475
}
1488-
FlowHandler::MatchOption { .. } => make::expr_path(make_path_from_text("None")),
1476+
FlowHandler::MatchOption { .. } => make::expr_path(make::ext::ident_path("None")),
14891477
FlowHandler::MatchResult { .. } => {
14901478
let expr = arg_expr.unwrap_or_else(|| make::expr_tuple(Vec::new()));
14911479
let args = make::arg_list(iter::once(expr));
1492-
make::expr_call(make::expr_path(make_path_from_text("Err")), args)
1480+
make::expr_call(make::expr_path(make::ext::ident_path("Err")), args)
14931481
}
14941482
};
14951483
Some(make::expr_return(Some(value)).clone_for_update())

crates/ide_assists/src/handlers/generate_function.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ impl FunctionBuilder {
175175
}
176176

177177
fn render(self) -> FunctionTemplate {
178-
let placeholder_expr = make::expr_todo();
178+
let placeholder_expr = make::ext::expr_todo();
179179
let fn_body = make::block_expr(vec![], Some(placeholder_expr));
180180
let visibility = if self.needs_pub { Some(make::visibility_pub_crate()) } else { None };
181181
let mut fn_def = make::fn_(

crates/ide_assists/src/handlers/move_bounds.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,7 @@ pub(crate) fn move_bounds_to_where_clause(acc: &mut Assists, ctx: &AssistContext
6363
}
6464

6565
fn build_predicate(param: ast::TypeParam) -> Option<ast::WherePred> {
66-
let path = {
67-
let name_ref = make::name_ref(&param.name()?.syntax().to_string());
68-
let segment = make::path_segment(name_ref);
69-
make::path_unqualified(segment)
70-
};
66+
let path = make::ext::ident_path(&param.name()?.syntax().to_string());
7167
let predicate = make::where_pred(path, param.type_bound_list()?.bounds());
7268
Some(predicate.clone_for_update())
7369
}

crates/ide_assists/src/handlers/replace_derive_with_manual_impl.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ pub(crate) fn replace_derive_with_manual_impl(
8484
add_assist(acc, ctx, &attr, &args, &trait_path, Some(trait_), &adt)?;
8585
}
8686
if no_traits_found {
87-
let trait_path = make::path_unqualified(make::path_segment(make::name_ref(trait_name)));
87+
let trait_path = make::ext::ident_path(trait_name);
8888
add_assist(acc, ctx, &attr, &args, &trait_path, None, &adt)?;
8989
}
9090
Some(())
@@ -159,10 +159,8 @@ fn impl_def_from_trait(
159159
if trait_items.is_empty() {
160160
return None;
161161
}
162-
let impl_def = make::impl_trait(
163-
trait_path.clone(),
164-
make::path_unqualified(make::path_segment(make::name_ref(&annotated_name.text()))),
165-
);
162+
let impl_def =
163+
make::impl_trait(trait_path.clone(), make::ext::ident_path(&annotated_name.text()));
166164
let (impl_def, first_assoc_item) =
167165
add_trait_assoc_items_to_impl(sema, trait_items, trait_, impl_def, target_scope);
168166
Some((impl_def, first_assoc_item))

crates/ide_assists/src/handlers/replace_let_with_if_let.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ pub(crate) fn replace_let_with_if_let(acc: &mut Assists, ctx: &AssistContext) ->
5353
let with_placeholder: ast::Pat = match happy_variant {
5454
None => make::wildcard_pat().into(),
5555
Some(var_name) => make::tuple_struct_pat(
56-
make::path_unqualified(make::path_segment(make::name_ref(var_name))),
56+
make::ext::ident_path(var_name),
5757
once(make::wildcard_pat().into()),
5858
)
5959
.into(),

crates/ide_assists/src/handlers/replace_unwrap_with_match.rs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ use ide_db::ty_filter::TryEnum;
3232
// fn main() {
3333
// let x: Result<i32, i32> = Result::Ok(92);
3434
// let y = match x {
35-
// Ok(a) => a,
35+
// Ok(it) => it,
3636
// $0_ => unreachable!(),
3737
// };
3838
// }
@@ -52,16 +52,17 @@ pub(crate) fn replace_unwrap_with_match(acc: &mut Assists, ctx: &AssistContext)
5252
"Replace unwrap with match",
5353
target,
5454
|builder| {
55-
let ok_path = make::path_unqualified(make::path_segment(make::name_ref(happy_variant)));
56-
let it = make::ident_pat(make::name("a")).into();
55+
let ok_path = make::ext::ident_path(happy_variant);
56+
let it = make::ident_pat(make::name("it")).into();
5757
let ok_tuple = make::tuple_struct_pat(ok_path, iter::once(it)).into();
5858

59-
let bind_path = make::path_unqualified(make::path_segment(make::name_ref("a")));
59+
let bind_path = make::ext::ident_path("it");
6060
let ok_arm = make::match_arm(iter::once(ok_tuple), make::expr_path(bind_path));
6161

62-
let unreachable_call = make::expr_unreachable();
63-
let err_arm =
64-
make::match_arm(iter::once(make::wildcard_pat().into()), unreachable_call);
62+
let err_arm = make::match_arm(
63+
iter::once(make::wildcard_pat().into()),
64+
make::ext::expr_unreachable(),
65+
);
6566

6667
let match_arm_list = make::match_arm_list(vec![ok_arm, err_arm]);
6768
let match_expr = make::expr_match(caller.clone(), match_arm_list)
@@ -110,7 +111,7 @@ fn i<T>(a: T) -> T { a }
110111
fn main() {
111112
let x: Result<i32, i32> = Result::Ok(92);
112113
let y = match i(x) {
113-
Ok(a) => a,
114+
Ok(it) => it,
114115
$0_ => unreachable!(),
115116
};
116117
}
@@ -136,7 +137,7 @@ fn i<T>(a: T) -> T { a }
136137
fn main() {
137138
let x = Option::Some(92);
138139
let y = match i(x) {
139-
Some(a) => a,
140+
Some(it) => it,
140141
$0_ => unreachable!(),
141142
};
142143
}
@@ -162,7 +163,7 @@ fn i<T>(a: T) -> T { a }
162163
fn main() {
163164
let x: Result<i32, i32> = Result::Ok(92);
164165
let y = match i(x) {
165-
Ok(a) => a,
166+
Ok(it) => it,
166167
$0_ => unreachable!(),
167168
}.count_zeroes();
168169
}

crates/ide_assists/src/handlers/wrap_return_type_in_result.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,7 @@ pub(crate) fn wrap_return_type_in_result(acc: &mut Assists, ctx: &AssistContext)
5454

5555
for ret_expr_arg in tail_return_expr_collector.exprs_to_wrap {
5656
let ok_wrapped = make::expr_call(
57-
make::expr_path(make::path_unqualified(make::path_segment(make::name_ref(
58-
"Ok",
59-
)))),
57+
make::expr_path(make::ext::ident_path("Ok")),
6058
make::arg_list(iter::once(ret_expr_arg.clone())),
6159
);
6260
builder.replace_ast(ret_expr_arg, ok_wrapped);

crates/ide_assists/src/tests/generated.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1531,7 +1531,7 @@ enum Result<T, E> { Ok(T), Err(E) }
15311531
fn main() {
15321532
let x: Result<i32, i32> = Result::Ok(92);
15331533
let y = match x {
1534-
Ok(a) => a,
1534+
Ok(it) => it,
15351535
$0_ => unreachable!(),
15361536
};
15371537
}

0 commit comments

Comments
 (0)