From 4f3c0adc5aafea465c71c85f36484da970df1ba2 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 9 May 2021 19:51:06 +0300 Subject: internal: introduce `ast::make::ext` module with common shortcuts There's a tension between keeping a well-architectured minimal orthogonal set of constructs, and providing convenience functions. Relieve this pressure by introducing an dedicated module for non-orthogonal shortcuts. This is inspired by the django.shortcuts module which serves a similar purpose architecturally. --- crates/ide_assists/src/handlers/early_return.rs | 4 +- .../ide_assists/src/handlers/expand_glob_import.rs | 3 +- .../ide_assists/src/handlers/extract_function.rs | 62 +++++++++------------- .../ide_assists/src/handlers/generate_function.rs | 2 +- crates/ide_assists/src/handlers/move_bounds.rs | 6 +-- .../handlers/replace_derive_with_manual_impl.rs | 8 ++- .../src/handlers/replace_let_with_if_let.rs | 2 +- .../src/handlers/replace_unwrap_with_match.rs | 21 ++++---- .../src/handlers/wrap_return_type_in_result.rs | 4 +- crates/ide_assists/src/tests/generated.rs | 2 +- crates/ide_assists/src/utils.rs | 4 +- 11 files changed, 48 insertions(+), 70 deletions(-) (limited to 'crates/ide_assists') diff --git a/crates/ide_assists/src/handlers/early_return.rs b/crates/ide_assists/src/handlers/early_return.rs index c66f8c05d..5eb6a57f0 100644 --- a/crates/ide_assists/src/handlers/early_return.rs +++ b/crates/ide_assists/src/handlers/early_return.rs @@ -130,9 +130,7 @@ pub(crate) fn convert_to_guarded_return(acc: &mut Assists, ctx: &AssistContext) once(make::ident_pat(make::name("it")).into()), ); let expr = { - let name_ref = make::name_ref("it"); - let segment = make::path_segment(name_ref); - let path = make::path_unqualified(segment); + let path = make::ext::ident_path("it"); make::expr_path(path) }; make::match_arm(once(pat.into()), expr) diff --git a/crates/ide_assists/src/handlers/expand_glob_import.rs b/crates/ide_assists/src/handlers/expand_glob_import.rs index e3095f26b..da8d245e5 100644 --- a/crates/ide_assists/src/handlers/expand_glob_import.rs +++ b/crates/ide_assists/src/handlers/expand_glob_import.rs @@ -65,8 +65,7 @@ pub(crate) fn expand_glob_import(acc: &mut Assists, ctx: &AssistContext) -> Opti let names_to_import = find_names_to_import(ctx, refs_in_target, imported_defs); let expanded = make::use_tree_list(names_to_import.iter().map(|n| { - let path = - make::path_unqualified(make::path_segment(make::name_ref(&n.to_string()))); + let path = make::ext::ident_path(&n.to_string()); make::use_tree(path, None, None, false) })) .clone_for_update(); diff --git a/crates/ide_assists/src/handlers/extract_function.rs b/crates/ide_assists/src/handlers/extract_function.rs index 494ef4621..6311afc1f 100644 --- a/crates/ide_assists/src/handlers/extract_function.rs +++ b/crates/ide_assists/src/handlers/extract_function.rs @@ -956,10 +956,10 @@ fn format_replacement(ctx: &AssistContext, fun: &Function, indent: IndentLevel) let args = fun.params.iter().map(|param| param.to_arg(ctx)); let args = make::arg_list(args); let call_expr = if fun.self_param.is_some() { - let self_arg = make::expr_path(make_path_from_text("self")); + let self_arg = make::expr_path(make::ext::ident_path("self")); make::expr_method_call(self_arg, &fun.name, args) } else { - let func = make::expr_path(make_path_from_text(&fun.name)); + let func = make::expr_path(make::ext::ident_path(&fun.name)); make::expr_call(func, args) }; @@ -1054,11 +1054,11 @@ impl FlowHandler { make::expr_if(condition, block, None) } FlowHandler::IfOption { action } => { - let path = make_path_from_text("Some"); + let path = make::ext::ident_path("Some"); let value_pat = make::ident_pat(make::name("value")); let pattern = make::tuple_struct_pat(path, iter::once(value_pat.into())); let cond = make::condition(call_expr, Some(pattern.into())); - let value = make::expr_path(make_path_from_text("value")); + let value = make::expr_path(make::ext::ident_path("value")); let action_expr = action.make_result_handler(Some(value)); let action_stmt = make::expr_stmt(action_expr); let then = make::block_expr(iter::once(action_stmt.into()), None); @@ -1068,14 +1068,14 @@ impl FlowHandler { let some_name = "value"; let some_arm = { - let path = make_path_from_text("Some"); + let path = make::ext::ident_path("Some"); let value_pat = make::ident_pat(make::name(some_name)); let pat = make::tuple_struct_pat(path, iter::once(value_pat.into())); - let value = make::expr_path(make_path_from_text(some_name)); + let value = make::expr_path(make::ext::ident_path(some_name)); make::match_arm(iter::once(pat.into()), value) }; let none_arm = { - let path = make_path_from_text("None"); + let path = make::ext::ident_path("None"); let pat = make::path_pat(path); make::match_arm(iter::once(pat), none.make_result_handler(None)) }; @@ -1087,17 +1087,17 @@ impl FlowHandler { let err_name = "value"; let ok_arm = { - let path = make_path_from_text("Ok"); + let path = make::ext::ident_path("Ok"); let value_pat = make::ident_pat(make::name(ok_name)); let pat = make::tuple_struct_pat(path, iter::once(value_pat.into())); - let value = make::expr_path(make_path_from_text(ok_name)); + let value = make::expr_path(make::ext::ident_path(ok_name)); make::match_arm(iter::once(pat.into()), value) }; let err_arm = { - let path = make_path_from_text("Err"); + let path = make::ext::ident_path("Err"); let value_pat = make::ident_pat(make::name(err_name)); let pat = make::tuple_struct_pat(path, iter::once(value_pat.into())); - let value = make::expr_path(make_path_from_text(err_name)); + let value = make::expr_path(make::ext::ident_path(err_name)); make::match_arm(iter::once(pat.into()), err.make_result_handler(Some(value))) }; let arms = make::match_arm_list(vec![ok_arm, err_arm]); @@ -1107,13 +1107,9 @@ impl FlowHandler { } } -fn make_path_from_text(text: &str) -> ast::Path { - make::path_unqualified(make::path_segment(make::name_ref(text))) -} - fn path_expr_from_local(ctx: &AssistContext, var: Local) -> ast::Expr { let name = var.name(ctx.db()).unwrap().to_string(); - make::expr_path(make_path_from_text(&name)) + make::expr_path(make::ext::ident_path(&name)) } fn format_function( @@ -1179,7 +1175,7 @@ fn make_ret_ty(ctx: &AssistContext, module: hir::Module, fun: &Function) -> Opti fun_ty.make_ty(ctx, module) } FlowHandler::Try { kind: TryKind::Option } => { - make::ty_generic(make::name_ref("Option"), iter::once(fun_ty.make_ty(ctx, module))) + make::ext::ty_option(fun_ty.make_ty(ctx, module)) } FlowHandler::Try { kind: TryKind::Result { ty: parent_ret_ty } } => { let handler_ty = parent_ret_ty @@ -1187,29 +1183,21 @@ fn make_ret_ty(ctx: &AssistContext, module: hir::Module, fun: &Function) -> Opti .nth(1) .map(|ty| make_ty(&ty, ctx, module)) .unwrap_or_else(make::ty_unit); - make::ty_generic( - make::name_ref("Result"), - vec![fun_ty.make_ty(ctx, module), handler_ty], - ) + make::ext::ty_result(fun_ty.make_ty(ctx, module), handler_ty) } - FlowHandler::If { .. } => make::ty_bool(), + FlowHandler::If { .. } => make::ext::ty_bool(), FlowHandler::IfOption { action } => { let handler_ty = action .expr_ty(ctx) .map(|ty| make_ty(&ty, ctx, module)) .unwrap_or_else(make::ty_unit); - make::ty_generic(make::name_ref("Option"), iter::once(handler_ty)) - } - FlowHandler::MatchOption { .. } => { - make::ty_generic(make::name_ref("Option"), iter::once(fun_ty.make_ty(ctx, module))) + make::ext::ty_option(handler_ty) } + FlowHandler::MatchOption { .. } => make::ext::ty_option(fun_ty.make_ty(ctx, module)), FlowHandler::MatchResult { err } => { let handler_ty = err.expr_ty(ctx).map(|ty| make_ty(&ty, ctx, module)).unwrap_or_else(make::ty_unit); - make::ty_generic( - make::name_ref("Result"), - vec![fun_ty.make_ty(ctx, module), handler_ty], - ) + make::ext::ty_result(fun_ty.make_ty(ctx, module), handler_ty) } }; Some(make::ret_type(ret_ty)) @@ -1296,7 +1284,7 @@ fn make_body( TryKind::Option => "Some", TryKind::Result { .. } => "Ok", }; - let func = make::expr_path(make_path_from_text(constructor)); + let func = make::expr_path(make::ext::ident_path(constructor)); let args = make::arg_list(iter::once(tail_expr)); make::expr_call(func, args) }) @@ -1306,16 +1294,16 @@ fn make_body( with_tail_expr(block, lit_false.into()) } FlowHandler::IfOption { .. } => { - let none = make::expr_path(make_path_from_text("None")); + let none = make::expr_path(make::ext::ident_path("None")); with_tail_expr(block, none) } FlowHandler::MatchOption { .. } => map_tail_expr(block, |tail_expr| { - let some = make::expr_path(make_path_from_text("Some")); + let some = make::expr_path(make::ext::ident_path("Some")); let args = make::arg_list(iter::once(tail_expr)); make::expr_call(some, args) }), FlowHandler::MatchResult { .. } => map_tail_expr(block, |tail_expr| { - let ok = make::expr_path(make_path_from_text("Ok")); + let ok = make::expr_path(make::ext::ident_path("Ok")); let args = make::arg_list(iter::once(tail_expr)); make::expr_call(ok, args) }), @@ -1483,13 +1471,13 @@ fn make_rewritten_flow(handler: &FlowHandler, arg_expr: Option) -> Op FlowHandler::IfOption { .. } => { let expr = arg_expr.unwrap_or_else(|| make::expr_tuple(Vec::new())); let args = make::arg_list(iter::once(expr)); - make::expr_call(make::expr_path(make_path_from_text("Some")), args) + make::expr_call(make::expr_path(make::ext::ident_path("Some")), args) } - FlowHandler::MatchOption { .. } => make::expr_path(make_path_from_text("None")), + FlowHandler::MatchOption { .. } => make::expr_path(make::ext::ident_path("None")), FlowHandler::MatchResult { .. } => { let expr = arg_expr.unwrap_or_else(|| make::expr_tuple(Vec::new())); let args = make::arg_list(iter::once(expr)); - make::expr_call(make::expr_path(make_path_from_text("Err")), args) + make::expr_call(make::expr_path(make::ext::ident_path("Err")), args) } }; Some(make::expr_return(Some(value)).clone_for_update()) diff --git a/crates/ide_assists/src/handlers/generate_function.rs b/crates/ide_assists/src/handlers/generate_function.rs index 6f95b1a07..bc9fc524b 100644 --- a/crates/ide_assists/src/handlers/generate_function.rs +++ b/crates/ide_assists/src/handlers/generate_function.rs @@ -175,7 +175,7 @@ impl FunctionBuilder { } fn render(self) -> FunctionTemplate { - let placeholder_expr = make::expr_todo(); + let placeholder_expr = make::ext::expr_todo(); let fn_body = make::block_expr(vec![], Some(placeholder_expr)); let visibility = if self.needs_pub { Some(make::visibility_pub_crate()) } else { None }; let mut fn_def = make::fn_( diff --git a/crates/ide_assists/src/handlers/move_bounds.rs b/crates/ide_assists/src/handlers/move_bounds.rs index 011a28d44..fa3f76609 100644 --- a/crates/ide_assists/src/handlers/move_bounds.rs +++ b/crates/ide_assists/src/handlers/move_bounds.rs @@ -63,11 +63,7 @@ pub(crate) fn move_bounds_to_where_clause(acc: &mut Assists, ctx: &AssistContext } fn build_predicate(param: ast::TypeParam) -> Option { - let path = { - let name_ref = make::name_ref(¶m.name()?.syntax().to_string()); - let segment = make::path_segment(name_ref); - make::path_unqualified(segment) - }; + let path = make::ext::ident_path(¶m.name()?.syntax().to_string()); let predicate = make::where_pred(path, param.type_bound_list()?.bounds()); Some(predicate.clone_for_update()) } diff --git a/crates/ide_assists/src/handlers/replace_derive_with_manual_impl.rs b/crates/ide_assists/src/handlers/replace_derive_with_manual_impl.rs index 694d897d1..10d9cec31 100644 --- a/crates/ide_assists/src/handlers/replace_derive_with_manual_impl.rs +++ b/crates/ide_assists/src/handlers/replace_derive_with_manual_impl.rs @@ -84,7 +84,7 @@ pub(crate) fn replace_derive_with_manual_impl( add_assist(acc, ctx, &attr, &args, &trait_path, Some(trait_), &adt)?; } if no_traits_found { - let trait_path = make::path_unqualified(make::path_segment(make::name_ref(trait_name))); + let trait_path = make::ext::ident_path(trait_name); add_assist(acc, ctx, &attr, &args, &trait_path, None, &adt)?; } Some(()) @@ -159,10 +159,8 @@ fn impl_def_from_trait( if trait_items.is_empty() { return None; } - let impl_def = make::impl_trait( - trait_path.clone(), - make::path_unqualified(make::path_segment(make::name_ref(&annotated_name.text()))), - ); + let impl_def = + make::impl_trait(trait_path.clone(), make::ext::ident_path(&annotated_name.text())); let (impl_def, first_assoc_item) = add_trait_assoc_items_to_impl(sema, trait_items, trait_, impl_def, target_scope); Some((impl_def, first_assoc_item)) diff --git a/crates/ide_assists/src/handlers/replace_let_with_if_let.rs b/crates/ide_assists/src/handlers/replace_let_with_if_let.rs index be7e724b5..f811234c0 100644 --- a/crates/ide_assists/src/handlers/replace_let_with_if_let.rs +++ b/crates/ide_assists/src/handlers/replace_let_with_if_let.rs @@ -53,7 +53,7 @@ pub(crate) fn replace_let_with_if_let(acc: &mut Assists, ctx: &AssistContext) -> let with_placeholder: ast::Pat = match happy_variant { None => make::wildcard_pat().into(), Some(var_name) => make::tuple_struct_pat( - make::path_unqualified(make::path_segment(make::name_ref(var_name))), + make::ext::ident_path(var_name), once(make::wildcard_pat().into()), ) .into(), diff --git a/crates/ide_assists/src/handlers/replace_unwrap_with_match.rs b/crates/ide_assists/src/handlers/replace_unwrap_with_match.rs index a986a6ae8..0fec961b4 100644 --- a/crates/ide_assists/src/handlers/replace_unwrap_with_match.rs +++ b/crates/ide_assists/src/handlers/replace_unwrap_with_match.rs @@ -32,7 +32,7 @@ use ide_db::ty_filter::TryEnum; // fn main() { // let x: Result = Result::Ok(92); // let y = match x { -// Ok(a) => a, +// Ok(it) => it, // $0_ => unreachable!(), // }; // } @@ -52,16 +52,17 @@ pub(crate) fn replace_unwrap_with_match(acc: &mut Assists, ctx: &AssistContext) "Replace unwrap with match", target, |builder| { - let ok_path = make::path_unqualified(make::path_segment(make::name_ref(happy_variant))); - let it = make::ident_pat(make::name("a")).into(); + let ok_path = make::ext::ident_path(happy_variant); + let it = make::ident_pat(make::name("it")).into(); let ok_tuple = make::tuple_struct_pat(ok_path, iter::once(it)).into(); - let bind_path = make::path_unqualified(make::path_segment(make::name_ref("a"))); + let bind_path = make::ext::ident_path("it"); let ok_arm = make::match_arm(iter::once(ok_tuple), make::expr_path(bind_path)); - let unreachable_call = make::expr_unreachable(); - let err_arm = - make::match_arm(iter::once(make::wildcard_pat().into()), unreachable_call); + let err_arm = make::match_arm( + iter::once(make::wildcard_pat().into()), + make::ext::expr_unreachable(), + ); let match_arm_list = make::match_arm_list(vec![ok_arm, err_arm]); let match_expr = make::expr_match(caller.clone(), match_arm_list) @@ -110,7 +111,7 @@ fn i(a: T) -> T { a } fn main() { let x: Result = Result::Ok(92); let y = match i(x) { - Ok(a) => a, + Ok(it) => it, $0_ => unreachable!(), }; } @@ -136,7 +137,7 @@ fn i(a: T) -> T { a } fn main() { let x = Option::Some(92); let y = match i(x) { - Some(a) => a, + Some(it) => it, $0_ => unreachable!(), }; } @@ -162,7 +163,7 @@ fn i(a: T) -> T { a } fn main() { let x: Result = Result::Ok(92); let y = match i(x) { - Ok(a) => a, + Ok(it) => it, $0_ => unreachable!(), }.count_zeroes(); } diff --git a/crates/ide_assists/src/handlers/wrap_return_type_in_result.rs b/crates/ide_assists/src/handlers/wrap_return_type_in_result.rs index e838630ea..2f1da82c7 100644 --- a/crates/ide_assists/src/handlers/wrap_return_type_in_result.rs +++ b/crates/ide_assists/src/handlers/wrap_return_type_in_result.rs @@ -54,9 +54,7 @@ pub(crate) fn wrap_return_type_in_result(acc: &mut Assists, ctx: &AssistContext) for ret_expr_arg in tail_return_expr_collector.exprs_to_wrap { let ok_wrapped = make::expr_call( - make::expr_path(make::path_unqualified(make::path_segment(make::name_ref( - "Ok", - )))), + make::expr_path(make::ext::ident_path("Ok")), make::arg_list(iter::once(ret_expr_arg.clone())), ); builder.replace_ast(ret_expr_arg, ok_wrapped); diff --git a/crates/ide_assists/src/tests/generated.rs b/crates/ide_assists/src/tests/generated.rs index 59bcef8fb..49c1a9776 100644 --- a/crates/ide_assists/src/tests/generated.rs +++ b/crates/ide_assists/src/tests/generated.rs @@ -1531,7 +1531,7 @@ enum Result { Ok(T), Err(E) } fn main() { let x: Result = Result::Ok(92); let y = match x { - Ok(a) => a, + Ok(it) => it, $0_ => unreachable!(), }; } diff --git a/crates/ide_assists/src/utils.rs b/crates/ide_assists/src/utils.rs index 5a90ad715..0dcf20c61 100644 --- a/crates/ide_assists/src/utils.rs +++ b/crates/ide_assists/src/utils.rs @@ -159,8 +159,8 @@ pub fn add_trait_assoc_items_to_impl( match fn_def.body() { Some(_) => fn_def, None => { - let body = - make::block_expr(None, Some(make::expr_todo())).indent(edit::IndentLevel(1)); + let body = make::block_expr(None, Some(make::ext::expr_todo())) + .indent(edit::IndentLevel(1)); fn_def.with_body(body) } } -- cgit v1.2.3