From 546f031e4e643c7cc4df44a2a09ac7c4af5e5ad7 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 25 Sep 2019 15:16:18 +0300 Subject: [PATCH] minor builder cleanups --- .../src/assists/add_missing_impl_members.rs | 2 +- .../ra_assists/src/assists/fill_match_arms.rs | 6 +- crates/ra_assists/src/ast_builder.rs | 112 +++++++++--------- 3 files changed, 60 insertions(+), 60 deletions(-) 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 2894bdd8a1e..11d69a04bf3 100644 --- a/crates/ra_assists/src/assists/add_missing_impl_members.rs +++ b/crates/ra_assists/src/assists/add_missing_impl_members.rs @@ -103,7 +103,7 @@ 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(), + AstBuilder::::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 771aa625fc9..f1c36b805a9 100644 --- a/crates/ra_assists/src/assists/fill_match_arms.rs +++ b/crates/ra_assists/src/assists/fill_match_arms.rs @@ -32,7 +32,7 @@ pub(crate) fn fill_match_arms(mut ctx: AssistCtx) -> Option::from_pieces( iter::once(pat), - &AstBuilder::::unit(), + AstBuilder::::unit(), ) }); let new_arm_list = AstBuilder::::from_arms(arms); @@ -66,7 +66,7 @@ fn resolve_enum_def( } fn build_pat(var: ast::EnumVariant) -> Option { - let path = &AstBuilder::::from_pieces(var.parent_enum().name()?, var.name()?); + let path = AstBuilder::::from_pieces(var.parent_enum().name()?, var.name()?); let pat: ast::Pat = match var.kind() { ast::StructKind::Tuple(field_list) => { @@ -77,7 +77,7 @@ fn build_pat(var: ast::EnumVariant) -> Option { ast::StructKind::Named(field_list) => { let pats = field_list .fields() - .map(|f| AstBuilder::::from_name(&f.name().unwrap()).into()); + .map(|f| AstBuilder::::from_name(f.name().unwrap()).into()); AstBuilder::::from_pieces(path, pats).into() } ast::StructKind::Unit => AstBuilder::::from_path(path).into(), diff --git a/crates/ra_assists/src/ast_builder.rs b/crates/ra_assists/src/ast_builder.rs index 9b9158ae219..171b1c936d3 100644 --- a/crates/ra_assists/src/ast_builder.rs +++ b/crates/ra_assists/src/ast_builder.rs @@ -7,33 +7,29 @@ pub struct AstBuilder { } impl AstBuilder { - fn from_text(text: &str) -> ast::RecordField { - ast_node_from_file_text(&format!("fn f() {{ S {{ {}, }} }}", text)) - } - pub fn from_pieces(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()), } } + + fn from_text(text: &str) -> ast::RecordField { + ast_node_from_file_text(&format!("fn f() {{ S {{ {}, }} }}", text)) + } } impl AstBuilder { - fn from_text(text: &str) -> ast::Block { - ast_node_from_file_text(&format!("fn f() {}", text)) + pub fn single_expr(e: ast::Expr) -> ast::Block { + Self::from_text(&format!("{{ {} }}", e.syntax())) } - pub fn single_expr(e: &ast::Expr) -> ast::Block { - Self::from_text(&format!("{{ {} }}", e.syntax())) + fn from_text(text: &str) -> ast::Block { + ast_node_from_file_text(&format!("fn f() {}", text)) } } impl AstBuilder { - fn from_text(text: &str) -> ast::Expr { - ast_node_from_file_text(&format!("const C: () = {};", text)) - } - pub fn unit() -> ast::Expr { Self::from_text("()") } @@ -41,6 +37,10 @@ pub fn unit() -> ast::Expr { pub fn unimplemented() -> ast::Expr { Self::from_text("unimplemented!()") } + + fn from_text(text: &str) -> ast::Expr { + ast_node_from_file_text(&format!("const C: () = {};", text)) + } } impl AstBuilder { @@ -50,10 +50,6 @@ pub fn new(text: &str) -> ast::NameRef { } impl AstBuilder { - fn from_text(text: &str) -> ast::Path { - ast_node_from_file_text(text) - } - pub fn from_name(name: ast::Name) -> ast::Path { let name = name.syntax().to_string(); Self::from_text(name.as_str()) @@ -62,91 +58,91 @@ pub fn from_name(name: ast::Name) -> ast::Path { pub fn from_pieces(enum_name: ast::Name, var_name: ast::Name) -> ast::Path { Self::from_text(&format!("{}::{}", enum_name.syntax(), var_name.syntax())) } + + fn from_text(text: &str) -> ast::Path { + ast_node_from_file_text(text) + } } impl AstBuilder { - fn from_text(text: &str) -> ast::BindPat { - ast_node_from_file_text(&format!("fn f({}: ())", text)) + pub fn from_name(name: ast::Name) -> ast::BindPat { + Self::from_text(name.text()) } - pub fn from_name(name: &ast::Name) -> ast::BindPat { - Self::from_text(name.text()) + fn from_text(text: &str) -> ast::BindPat { + ast_node_from_file_text(&format!("fn f({}: ())", text)) } } impl AstBuilder { - fn from_text(text: &str) -> ast::PlaceholderPat { - ast_node_from_file_text(&format!("fn f({}: ())", text)) - } - pub fn placeholder() -> ast::PlaceholderPat { Self::from_text("_") } + + fn from_text(text: &str) -> ast::PlaceholderPat { + ast_node_from_file_text(&format!("fn f({}: ())", text)) + } } impl AstBuilder { - fn from_text(text: &str) -> ast::TupleStructPat { - ast_node_from_file_text(&format!("fn f({}: ())", text)) - } - pub fn from_pieces( - path: &ast::Path, + 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)) } + + fn from_text(text: &str) -> ast::TupleStructPat { + ast_node_from_file_text(&format!("fn f({}: ())", text)) + } } impl AstBuilder { - fn from_text(text: &str) -> ast::RecordPat { - ast_node_from_file_text(&format!("fn f({}: ())", text)) - } - - pub fn from_pieces(path: &ast::Path, pats: impl Iterator) -> ast::RecordPat { + pub fn from_pieces(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)) } + + fn from_text(text: &str) -> ast::RecordPat { + ast_node_from_file_text(&format!("fn f({}: ())", text)) + } } impl AstBuilder { - fn from_text(text: &str) -> ast::PathPat { - ast_node_from_file_text(&format!("fn f({}: ())", text)) - } - - pub fn from_path(path: &ast::Path) -> ast::PathPat { + pub fn from_path(path: ast::Path) -> ast::PathPat { let path_str = path.syntax().text().to_string(); Self::from_text(path_str.as_str()) } + + fn from_text(text: &str) -> ast::PathPat { + ast_node_from_file_text(&format!("fn f({}: ())", text)) + } } impl AstBuilder { - fn from_text(text: &str) -> ast::MatchArm { - ast_node_from_file_text(&format!("fn f() {{ match () {{{}}} }}", text)) - } - - pub fn from_pieces(pats: impl Iterator, expr: &ast::Expr) -> ast::MatchArm { + pub fn from_pieces(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())) } + + fn from_text(text: &str) -> ast::MatchArm { + ast_node_from_file_text(&format!("fn f() {{ match () {{{}}} }}", text)) + } } impl AstBuilder { - fn from_text(text: &str) -> ast::MatchArmList { - ast_node_from_file_text(&format!("fn f() {{ match () {{{}}} }}", text)) - } - 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)) } + + fn from_text(text: &str) -> ast::MatchArmList { + ast_node_from_file_text(&format!("fn f() {{ match () {{{}}} }}", text)) + } } impl AstBuilder { - fn from_text(text: &str) -> ast::WherePred { - ast_node_from_file_text(&format!("fn f() where {} {{ }}", text)) - } - pub fn from_pieces( path: ast::Path, bounds: impl Iterator, @@ -154,17 +150,21 @@ pub fn from_pieces( let bounds = bounds.map(|b| b.syntax().to_string()).collect::>().join(" + "); Self::from_text(&format!("{}: {}", path.syntax(), bounds)) } + + fn from_text(text: &str) -> ast::WherePred { + ast_node_from_file_text(&format!("fn f() where {} {{ }}", text)) + } } impl AstBuilder { - fn from_text(text: &str) -> ast::WhereClause { - ast_node_from_file_text(&format!("fn f() where {} {{ }}", text)) - } - 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()) } + + fn from_text(text: &str) -> ast::WhereClause { + ast_node_from_file_text(&format!("fn f() where {} {{ }}", text)) + } } fn ast_node_from_file_text(text: &str) -> N {