align names in make

This commit is contained in:
Aleksey Kladov 2020-08-05 19:29:24 +02:00
parent 5ebf92cd0e
commit 09d3b7d7a2
12 changed files with 29 additions and 28 deletions

View file

@ -63,7 +63,7 @@ impl<'a> SubstituteTypeParams<'a> {
let default = k.default(source_scope.db)?; let default = k.default(source_scope.db)?;
Some(( Some((
k, k,
ast::make::type_ref( ast::make::ty(
&default &default
.display_source_code(source_scope.db, source_scope.module()?.into()) .display_source_code(source_scope.db, source_scope.module()?.into())
.ok()?, .ok()?,

View file

@ -123,7 +123,7 @@ pub(crate) fn convert_to_guarded_return(acc: &mut Assists, ctx: &AssistContext)
let happy_arm = { let happy_arm = {
let pat = make::tuple_struct_pat( let pat = make::tuple_struct_pat(
path, path,
once(make::bind_pat(make::name("it")).into()), once(make::ident_pat(make::name("it")).into()),
); );
let expr = { let expr = {
let name_ref = make::name_ref("it"); let name_ref = make::name_ref("it");
@ -136,7 +136,7 @@ pub(crate) fn convert_to_guarded_return(acc: &mut Assists, ctx: &AssistContext)
let sad_arm = make::match_arm( let sad_arm = make::match_arm(
// FIXME: would be cool to use `None` or `Err(_)` if appropriate // FIXME: would be cool to use `None` or `Err(_)` if appropriate
once(make::placeholder_pat().into()), once(make::wildcard_pat().into()),
early_expression, early_expression,
); );
@ -144,7 +144,7 @@ pub(crate) fn convert_to_guarded_return(acc: &mut Assists, ctx: &AssistContext)
}; };
let let_stmt = make::let_stmt( let let_stmt = make::let_stmt(
make::bind_pat(make::name(&bound_ident.syntax().to_string())).into(), make::ident_pat(make::name(&bound_ident.syntax().to_string())).into(),
Some(match_expr), Some(match_expr),
); );
let let_stmt = let_stmt.indent(if_indent_level); let let_stmt = let_stmt.indent(if_indent_level);

View file

@ -173,7 +173,7 @@ fn replace_ast(
replace_node(replacement); replace_node(replacement);
}, },
ast::Use(use_item) => { ast::Use(use_item) => {
builder.replace_ast(use_item, ast::make::use_item(replacement.left_or_else(|ut| ast::make::use_tree(path, Some(ut), None, false)))); builder.replace_ast(use_item, ast::make::use_(replacement.left_or_else(|ut| ast::make::use_tree(path, Some(ut), None, false))));
}, },
_ => {}, _ => {},
} }

View file

@ -197,12 +197,11 @@ fn build_pat(db: &RootDatabase, module: hir::Module, var: hir::EnumVariant) -> O
// FIXME: use HIR for this; it doesn't currently expose struct vs. tuple vs. unit variants though // FIXME: use HIR for this; it doesn't currently expose struct vs. tuple vs. unit variants though
let pat: ast::Pat = match var.source(db).value.kind() { let pat: ast::Pat = match var.source(db).value.kind() {
ast::StructKind::Tuple(field_list) => { ast::StructKind::Tuple(field_list) => {
let pats = let pats = iter::repeat(make::wildcard_pat().into()).take(field_list.fields().count());
iter::repeat(make::placeholder_pat().into()).take(field_list.fields().count());
make::tuple_struct_pat(path, pats).into() make::tuple_struct_pat(path, pats).into()
} }
ast::StructKind::Record(field_list) => { ast::StructKind::Record(field_list) => {
let pats = field_list.fields().map(|f| make::bind_pat(f.name().unwrap()).into()); let pats = field_list.fields().map(|f| make::ident_pat(f.name().unwrap()).into());
make::record_pat(path, pats).into() make::record_pat(path, pats).into()
} }
ast::StructKind::Unit => make::path_pat(path), ast::StructKind::Unit => make::path_pat(path),

View file

@ -142,7 +142,7 @@ impl FunctionBuilder {
let fn_body = make::block_expr(vec![], Some(placeholder_expr)); let fn_body = make::block_expr(vec![], Some(placeholder_expr));
let visibility = if self.needs_pub { Some(make::visibility_pub_crate()) } else { None }; let visibility = if self.needs_pub { Some(make::visibility_pub_crate()) } else { None };
let mut fn_def = let mut fn_def =
make::fn_def(visibility, self.fn_name, self.type_params, self.params, fn_body); make::fn_(visibility, self.fn_name, self.type_params, self.params, fn_body);
let leading_ws; let leading_ws;
let trailing_ws; let trailing_ws;

View file

@ -65,7 +65,7 @@ pub(crate) fn replace_if_let_with_match(acc: &mut Assists, ctx: &AssistContext)
.type_of_pat(&pat) .type_of_pat(&pat)
.and_then(|ty| TryEnum::from_ty(&ctx.sema, &ty)) .and_then(|ty| TryEnum::from_ty(&ctx.sema, &ty))
.map(|it| it.sad_pattern()) .map(|it| it.sad_pattern())
.unwrap_or_else(|| make::placeholder_pat().into()); .unwrap_or_else(|| make::wildcard_pat().into());
let else_expr = unwrap_trivial_block(else_block); let else_expr = unwrap_trivial_block(else_block);
make::match_arm(vec![pattern], else_expr) make::match_arm(vec![pattern], else_expr)
}; };

View file

@ -50,10 +50,10 @@ pub(crate) fn replace_let_with_if_let(acc: &mut Assists, ctx: &AssistContext) ->
target, target,
|edit| { |edit| {
let with_placeholder: ast::Pat = match happy_variant { let with_placeholder: ast::Pat = match happy_variant {
None => make::placeholder_pat().into(), None => make::wildcard_pat().into(),
Some(var_name) => make::tuple_struct_pat( Some(var_name) => make::tuple_struct_pat(
make::path_unqualified(make::path_segment(make::name_ref(var_name))), make::path_unqualified(make::path_segment(make::name_ref(var_name))),
once(make::placeholder_pat().into()), once(make::wildcard_pat().into()),
) )
.into(), .into(),
}; };

View file

@ -52,7 +52,7 @@ pub(crate) fn replace_unwrap_with_match(acc: &mut Assists, ctx: &AssistContext)
target, target,
|builder| { |builder| {
let ok_path = make::path_unqualified(make::path_segment(make::name_ref(happy_variant))); let ok_path = make::path_unqualified(make::path_segment(make::name_ref(happy_variant)));
let it = make::bind_pat(make::name("a")).into(); let it = make::ident_pat(make::name("a")).into();
let ok_tuple = make::tuple_struct_pat(ok_path, iter::once(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::path_unqualified(make::path_segment(make::name_ref("a")));
@ -60,7 +60,7 @@ pub(crate) fn replace_unwrap_with_match(acc: &mut Assists, ctx: &AssistContext)
let unreachable_call = make::expr_unreachable(); let unreachable_call = make::expr_unreachable();
let err_arm = let err_arm =
make::match_arm(iter::once(make::placeholder_pat().into()), unreachable_call); make::match_arm(iter::once(make::wildcard_pat().into()), unreachable_call);
let match_arm_list = make::match_arm_list(vec![ok_arm, err_arm]); let match_arm_list = make::match_arm_list(vec![ok_arm, err_arm]);
let match_expr = make::expr_match(caller.clone(), match_arm_list) let match_expr = make::expr_match(caller.clone(), match_arm_list)

View file

@ -181,10 +181,10 @@ impl TryEnum {
match self { match self {
TryEnum::Result => make::tuple_struct_pat( TryEnum::Result => make::tuple_struct_pat(
make::path_unqualified(make::path_segment(make::name_ref("Err"))), make::path_unqualified(make::path_segment(make::name_ref("Err"))),
iter::once(make::placeholder_pat().into()), iter::once(make::wildcard_pat().into()),
) )
.into(), .into(),
TryEnum::Option => make::bind_pat(make::name("None")).into(), TryEnum::Option => make::ident_pat(make::name("None")).into(),
} }
} }

View file

@ -78,8 +78,10 @@ pub(crate) fn diagnostics(
} else { } else {
let mut field_list = d.ast(db); let mut field_list = d.ast(db);
for f in d.missed_fields.iter() { for f in d.missed_fields.iter() {
let field = let field = make::record_expr_field(
make::record_field(make::name_ref(&f.to_string()), Some(make::expr_unit())); make::name_ref(&f.to_string()),
Some(make::expr_unit()),
);
field_list = field_list.append_field(&field); field_list = field_list.append_field(&field);
} }
@ -178,9 +180,9 @@ fn missing_struct_field_fix(
if new_field_type.is_unknown() { if new_field_type.is_unknown() {
return None; return None;
} }
let new_field = make::record_field_def( let new_field = make::record_field(
record_expr.field_name()?, record_expr.field_name()?,
make::type_ref(&new_field_type.display_source_code(sema.db, module.into()).ok()?), make::ty(&new_field_type.display_source_code(sema.db, module.into()).ok()?),
); );
let last_field = record_fields.fields().last()?; let last_field = record_fields.fields().last()?;

View file

@ -621,7 +621,7 @@ fn single_node(element: impl Into<SyntaxElement>) -> RangeInclusive<SyntaxElemen
#[test] #[test]
fn test_increase_indent() { fn test_increase_indent() {
let arm_list = { let arm_list = {
let arm = make::match_arm(iter::once(make::placeholder_pat().into()), make::expr_unit()); let arm = make::match_arm(iter::once(make::wildcard_pat().into()), make::expr_unit());
make::match_arm_list(vec![arm.clone(), arm]) make::match_arm_list(vec![arm.clone(), arm])
}; };
assert_eq!( assert_eq!(

View file

@ -17,7 +17,7 @@ pub fn name_ref(text: &str) -> ast::NameRef {
ast_from_text(&format!("fn f() {{ {}; }}", text)) ast_from_text(&format!("fn f() {{ {}; }}", text))
} }
pub fn type_ref(text: &str) -> ast::Type { pub fn ty(text: &str) -> ast::Type {
ast_from_text(&format!("impl {} for D {{}};", text)) ast_from_text(&format!("impl {} for D {{}};", text))
} }
@ -60,11 +60,11 @@ pub fn use_tree_list(use_trees: impl IntoIterator<Item = ast::UseTree>) -> ast::
ast_from_text(&format!("use {{{}}};", use_trees)) ast_from_text(&format!("use {{{}}};", use_trees))
} }
pub fn use_item(use_tree: ast::UseTree) -> ast::Use { pub fn use_(use_tree: ast::UseTree) -> ast::Use {
ast_from_text(&format!("use {};", use_tree)) ast_from_text(&format!("use {};", use_tree))
} }
pub fn record_field(name: ast::NameRef, expr: Option<ast::Expr>) -> ast::RecordExprField { pub fn record_expr_field(name: ast::NameRef, expr: Option<ast::Expr>) -> ast::RecordExprField {
return match expr { return match expr {
Some(expr) => from_text(&format!("{}: {}", name, expr)), Some(expr) => from_text(&format!("{}: {}", name, expr)),
None => from_text(&name.to_string()), None => from_text(&name.to_string()),
@ -75,7 +75,7 @@ pub fn record_field(name: ast::NameRef, expr: Option<ast::Expr>) -> ast::RecordE
} }
} }
pub fn record_field_def(name: ast::NameRef, ty: ast::Type) -> ast::RecordField { pub fn record_field(name: ast::NameRef, ty: ast::Type) -> ast::RecordField {
ast_from_text(&format!("struct S {{ {}: {}, }}", name, ty)) ast_from_text(&format!("struct S {{ {}: {}, }}", name, ty))
} }
@ -148,7 +148,7 @@ pub fn condition(expr: ast::Expr, pattern: Option<ast::Pat>) -> ast::Condition {
} }
} }
pub fn bind_pat(name: ast::Name) -> ast::IdentPat { pub fn ident_pat(name: ast::Name) -> ast::IdentPat {
return from_text(name.text()); return from_text(name.text());
fn from_text(text: &str) -> ast::IdentPat { fn from_text(text: &str) -> ast::IdentPat {
@ -156,7 +156,7 @@ pub fn bind_pat(name: ast::Name) -> ast::IdentPat {
} }
} }
pub fn placeholder_pat() -> ast::WildcardPat { pub fn wildcard_pat() -> ast::WildcardPat {
return from_text("_"); return from_text("_");
fn from_text(text: &str) -> ast::WildcardPat { fn from_text(text: &str) -> ast::WildcardPat {
@ -288,7 +288,7 @@ pub fn visibility_pub_crate() -> ast::Visibility {
ast_from_text("pub(crate) struct S") ast_from_text("pub(crate) struct S")
} }
pub fn fn_def( pub fn fn_(
visibility: Option<ast::Visibility>, visibility: Option<ast::Visibility>,
fn_name: ast::Name, fn_name: ast::Name,
type_params: Option<ast::GenericParamList>, type_params: Option<ast::GenericParamList>,