From 32843ae6f7f91cea88edcec428cb5bad07bd5e98 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 25 Sep 2019 15:28:26 +0300 Subject: shorten AstBuilder names --- .../src/assists/add_missing_impl_members.rs | 6 +-- crates/ra_assists/src/assists/fill_match_arms.rs | 25 +++++------ crates/ra_assists/src/assists/move_bounds.rs | 9 ++-- crates/ra_assists/src/ast_builder.rs | 48 ++++++++++------------ 4 files changed, 38 insertions(+), 50 deletions(-) (limited to 'crates/ra_assists/src') diff --git a/crates/ra_assists/src/assists/add_missing_impl_members.rs b/crates/ra_assists/src/assists/add_missing_impl_members.rs index 11d69a04b..22d20909d 100644 --- a/crates/ra_assists/src/assists/add_missing_impl_members.rs +++ b/crates/ra_assists/src/assists/add_missing_impl_members.rs @@ -4,7 +4,7 @@ use ra_syntax::{ SmolStr, }; -use crate::{ast_builder::AstBuilder, ast_editor::AstEditor, Assist, AssistCtx, AssistId}; +use crate::{ast_builder::Make, ast_editor::AstEditor, Assist, AssistCtx, AssistId}; #[derive(PartialEq)] enum AddMissingImplMembersMode { @@ -102,9 +102,7 @@ fn strip_docstring(item: ast::ImplItem) -> ast::ImplItem { fn add_body(fn_def: ast::FnDef) -> ast::FnDef { let mut ast_editor = AstEditor::new(fn_def.clone()); if fn_def.body().is_none() { - ast_editor.set_body(&AstBuilder::::single_expr( - AstBuilder::::unimplemented(), - )); + ast_editor.set_body(&Make::::single_expr(Make::::unimplemented())); } ast_editor.ast().to_owned() } diff --git a/crates/ra_assists/src/assists/fill_match_arms.rs b/crates/ra_assists/src/assists/fill_match_arms.rs index f1c36b805..817433526 100644 --- a/crates/ra_assists/src/assists/fill_match_arms.rs +++ b/crates/ra_assists/src/assists/fill_match_arms.rs @@ -3,7 +3,7 @@ use std::iter; use hir::{db::HirDatabase, Adt, HasSource}; use ra_syntax::ast::{self, AstNode, NameOwner}; -use crate::{ast_builder::AstBuilder, Assist, AssistCtx, AssistId}; +use crate::{ast_builder::Make, Assist, AssistCtx, AssistId}; pub(crate) fn fill_match_arms(mut ctx: AssistCtx) -> Option { let match_expr = ctx.node_at_offset::()?; @@ -29,13 +29,10 @@ pub(crate) fn fill_match_arms(mut ctx: AssistCtx) -> Option::from_pieces( - iter::once(pat), - AstBuilder::::unit(), - ) - }); - let new_arm_list = AstBuilder::::from_arms(arms); + let arms = variants + .filter_map(build_pat) + .map(|pat| Make::::from(iter::once(pat), Make::::unit())); + let new_arm_list = Make::::from_arms(arms); edit.target(match_expr.syntax().text_range()); edit.set_cursor(expr.syntax().text_range().start()); @@ -66,21 +63,21 @@ fn resolve_enum_def( } fn build_pat(var: ast::EnumVariant) -> Option { - let path = AstBuilder::::from_pieces(var.parent_enum().name()?, var.name()?); + let path = Make::::from(var.parent_enum().name()?, var.name()?); let pat: ast::Pat = match var.kind() { ast::StructKind::Tuple(field_list) => { - let pats = iter::repeat(AstBuilder::::placeholder().into()) + let pats = iter::repeat(Make::::placeholder().into()) .take(field_list.fields().count()); - AstBuilder::::from_pieces(path, pats).into() + Make::::from(path, pats).into() } ast::StructKind::Named(field_list) => { let pats = field_list .fields() - .map(|f| AstBuilder::::from_name(f.name().unwrap()).into()); - AstBuilder::::from_pieces(path, pats).into() + .map(|f| Make::::from_name(f.name().unwrap()).into()); + Make::::from(path, pats).into() } - ast::StructKind::Unit => AstBuilder::::from_path(path).into(), + ast::StructKind::Unit => Make::::from_path(path).into(), }; Some(pat) diff --git a/crates/ra_assists/src/assists/move_bounds.rs b/crates/ra_assists/src/assists/move_bounds.rs index aa9036fed..6fd2fb72b 100644 --- a/crates/ra_assists/src/assists/move_bounds.rs +++ b/crates/ra_assists/src/assists/move_bounds.rs @@ -6,7 +6,7 @@ use ra_syntax::{ TextRange, }; -use crate::{ast_builder::AstBuilder, Assist, AssistCtx, AssistId}; +use crate::{ast_builder::Make, Assist, AssistCtx, AssistId}; pub(crate) fn move_bounds_to_where_clause(mut ctx: AssistCtx) -> Option { let type_param_list = ctx.node_at_offset::()?; @@ -52,7 +52,7 @@ pub(crate) fn move_bounds_to_where_clause(mut ctx: AssistCtx) } let predicates = type_params.iter().filter_map(build_predicate); - let where_clause = AstBuilder::::from_predicates(predicates); + let where_clause = Make::::from_predicates(predicates); let to_insert = match anchor.prev_sibling_or_token() { Some(ref elem) if elem.kind() == WHITESPACE => { @@ -69,9 +69,8 @@ pub(crate) fn move_bounds_to_where_clause(mut ctx: AssistCtx) } fn build_predicate(param: &ast::TypeParam) -> Option { - let path = AstBuilder::::from_name(param.name()?); - let predicate = - AstBuilder::::from_pieces(path, param.type_bound_list()?.bounds()); + let path = Make::::from_name(param.name()?); + let predicate = Make::::from(path, param.type_bound_list()?.bounds()); Some(predicate) } diff --git a/crates/ra_assists/src/ast_builder.rs b/crates/ra_assists/src/ast_builder.rs index 171b1c936..f6409085a 100644 --- a/crates/ra_assists/src/ast_builder.rs +++ b/crates/ra_assists/src/ast_builder.rs @@ -2,12 +2,12 @@ use itertools::Itertools; use ra_syntax::{ast, AstNode, SourceFile}; -pub struct AstBuilder { +pub struct Make { _phantom: std::marker::PhantomData, } -impl AstBuilder { - pub fn from_pieces(name: ast::NameRef, expr: Option) -> ast::RecordField { +impl Make { + pub fn from(name: ast::NameRef, expr: Option) -> ast::RecordField { match expr { Some(expr) => Self::from_text(&format!("{}: {}", name.syntax(), expr.syntax())), None => Self::from_text(&name.syntax().to_string()), @@ -19,7 +19,7 @@ impl AstBuilder { } } -impl AstBuilder { +impl Make { pub fn single_expr(e: ast::Expr) -> ast::Block { Self::from_text(&format!("{{ {} }}", e.syntax())) } @@ -29,7 +29,7 @@ impl AstBuilder { } } -impl AstBuilder { +impl Make { pub fn unit() -> ast::Expr { Self::from_text("()") } @@ -43,19 +43,19 @@ impl AstBuilder { } } -impl AstBuilder { +impl Make { pub fn new(text: &str) -> ast::NameRef { ast_node_from_file_text(&format!("fn f() {{ {}; }}", text)) } } -impl AstBuilder { +impl Make { pub fn from_name(name: ast::Name) -> ast::Path { let name = name.syntax().to_string(); Self::from_text(name.as_str()) } - pub fn from_pieces(enum_name: ast::Name, var_name: ast::Name) -> ast::Path { + pub fn from(enum_name: ast::Name, var_name: ast::Name) -> ast::Path { Self::from_text(&format!("{}::{}", enum_name.syntax(), var_name.syntax())) } @@ -64,7 +64,7 @@ impl AstBuilder { } } -impl AstBuilder { +impl Make { pub fn from_name(name: ast::Name) -> ast::BindPat { Self::from_text(name.text()) } @@ -74,7 +74,7 @@ impl AstBuilder { } } -impl AstBuilder { +impl Make { pub fn placeholder() -> ast::PlaceholderPat { Self::from_text("_") } @@ -84,11 +84,8 @@ impl AstBuilder { } } -impl AstBuilder { - pub fn from_pieces( - path: ast::Path, - pats: impl Iterator, - ) -> ast::TupleStructPat { +impl Make { + pub fn from(path: ast::Path, pats: impl Iterator) -> ast::TupleStructPat { let pats_str = pats.map(|p| p.syntax().to_string()).collect::>().join(", "); Self::from_text(&format!("{}({})", path.syntax(), pats_str)) } @@ -98,8 +95,8 @@ impl AstBuilder { } } -impl AstBuilder { - pub fn from_pieces(path: ast::Path, pats: impl Iterator) -> ast::RecordPat { +impl Make { + pub fn from(path: ast::Path, pats: impl Iterator) -> ast::RecordPat { let pats_str = pats.map(|p| p.syntax().to_string()).collect::>().join(", "); Self::from_text(&format!("{}{{ {} }}", path.syntax(), pats_str)) } @@ -109,7 +106,7 @@ impl AstBuilder { } } -impl AstBuilder { +impl Make { pub fn from_path(path: ast::Path) -> ast::PathPat { let path_str = path.syntax().text().to_string(); Self::from_text(path_str.as_str()) @@ -120,8 +117,8 @@ impl AstBuilder { } } -impl AstBuilder { - pub fn from_pieces(pats: impl Iterator, expr: ast::Expr) -> ast::MatchArm { +impl Make { + pub fn from(pats: impl Iterator, expr: ast::Expr) -> ast::MatchArm { let pats_str = pats.map(|p| p.syntax().to_string()).join(" | "); Self::from_text(&format!("{} => {}", pats_str, expr.syntax())) } @@ -131,7 +128,7 @@ impl AstBuilder { } } -impl AstBuilder { +impl Make { pub fn from_arms(arms: impl Iterator) -> ast::MatchArmList { let arms_str = arms.map(|arm| format!("\n {}", arm.syntax())).join(","); Self::from_text(&format!("{},\n", arms_str)) @@ -142,11 +139,8 @@ impl AstBuilder { } } -impl AstBuilder { - pub fn from_pieces( - path: ast::Path, - bounds: impl Iterator, - ) -> ast::WherePred { +impl Make { + pub fn from(path: ast::Path, bounds: impl Iterator) -> ast::WherePred { let bounds = bounds.map(|b| b.syntax().to_string()).collect::>().join(" + "); Self::from_text(&format!("{}: {}", path.syntax(), bounds)) } @@ -156,7 +150,7 @@ impl AstBuilder { } } -impl AstBuilder { +impl Make { pub fn from_predicates(preds: impl Iterator) -> ast::WhereClause { let preds = preds.map(|p| p.syntax().to_string()).collect::>().join(", "); Self::from_text(preds.as_str()) -- cgit v1.2.3