From 25fca0475343170b8398f5c87c76c455205ed3c4 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 25 Sep 2019 15:09:03 +0300 Subject: cleaned up record field builder --- crates/ra_assists/src/ast_builder.rs | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) (limited to 'crates/ra_assists/src') diff --git a/crates/ra_assists/src/ast_builder.rs b/crates/ra_assists/src/ast_builder.rs index e4ea1fca9..9b9158ae2 100644 --- a/crates/ra_assists/src/ast_builder.rs +++ b/crates/ra_assists/src/ast_builder.rs @@ -1,6 +1,5 @@ use itertools::Itertools; -use hir::Name; use ra_syntax::{ast, AstNode, SourceFile}; pub struct AstBuilder { @@ -8,15 +7,11 @@ pub struct AstBuilder { } impl AstBuilder { - pub fn from_name(name: &Name) -> ast::RecordField { - ast_node_from_file_text(&format!("fn f() {{ S {{ {}: (), }} }}", name)) - } - 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::Expr>) -> ast::RecordField { + 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()), -- cgit v1.2.3 From 546f031e4e643c7cc4df44a2a09ac7c4af5e5ad7 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 25 Sep 2019 15:16:18 +0300 Subject: minor builder cleanups --- .../src/assists/add_missing_impl_members.rs | 2 +- crates/ra_assists/src/assists/fill_match_arms.rs | 6 +- crates/ra_assists/src/ast_builder.rs | 102 ++++++++++----------- 3 files changed, 55 insertions(+), 55 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 2894bdd8a..11d69a04b 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 771aa625f..f1c36b805 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 9b9158ae2..171b1c936 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 @@ impl AstBuilder { 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 @@ impl AstBuilder { } 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 @@ impl AstBuilder { pub fn from_pieces(enum_name: ast::Name, var_name: ast::Name) -> ast::Path { Self::from_text(&format!("{}::{}", enum_name.syntax(), var_name.syntax())) } -} -impl AstBuilder { - fn from_text(text: &str) -> ast::BindPat { - ast_node_from_file_text(&format!("fn f({}: ())", text)) + fn from_text(text: &str) -> ast::Path { + ast_node_from_file_text(text) } +} - pub fn from_name(name: &ast::Name) -> ast::BindPat { +impl AstBuilder { + pub fn from_name(name: ast::Name) -> ast::BindPat { Self::from_text(name.text()) } -} -impl AstBuilder { - fn from_text(text: &str) -> ast::PlaceholderPat { + fn from_text(text: &str) -> ast::BindPat { ast_node_from_file_text(&format!("fn f({}: ())", text)) } +} +impl AstBuilder { pub fn placeholder() -> ast::PlaceholderPat { Self::from_text("_") } -} -impl AstBuilder { - fn from_text(text: &str) -> ast::TupleStructPat { + fn from_text(text: &str) -> ast::PlaceholderPat { ast_node_from_file_text(&format!("fn f({}: ())", text)) } +} +impl AstBuilder { 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)) } -} -impl AstBuilder { - fn from_text(text: &str) -> ast::RecordPat { + fn from_text(text: &str) -> ast::TupleStructPat { ast_node_from_file_text(&format!("fn f({}: ())", text)) } +} - pub fn from_pieces(path: &ast::Path, pats: impl Iterator) -> ast::RecordPat { +impl AstBuilder { + 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)) } -} -impl AstBuilder { - fn from_text(text: &str) -> ast::PathPat { + fn from_text(text: &str) -> ast::RecordPat { ast_node_from_file_text(&format!("fn f({}: ())", text)) } +} - pub fn from_path(path: &ast::Path) -> ast::PathPat { +impl AstBuilder { + pub fn from_path(path: ast::Path) -> ast::PathPat { let path_str = path.syntax().text().to_string(); Self::from_text(path_str.as_str()) } -} -impl AstBuilder { - fn from_text(text: &str) -> ast::MatchArm { - ast_node_from_file_text(&format!("fn f() {{ match () {{{}}} }}", text)) + fn from_text(text: &str) -> ast::PathPat { + ast_node_from_file_text(&format!("fn f({}: ())", text)) } +} - pub fn from_pieces(pats: impl Iterator, expr: &ast::Expr) -> ast::MatchArm { +impl AstBuilder { + 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())) } -} -impl AstBuilder { - fn from_text(text: &str) -> ast::MatchArmList { + fn from_text(text: &str) -> ast::MatchArm { ast_node_from_file_text(&format!("fn f() {{ match () {{{}}} }}", text)) } +} +impl AstBuilder { 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)) } -} -impl AstBuilder { - fn from_text(text: &str) -> ast::WherePred { - ast_node_from_file_text(&format!("fn f() where {} {{ }}", text)) + fn from_text(text: &str) -> ast::MatchArmList { + ast_node_from_file_text(&format!("fn f() {{ match () {{{}}} }}", text)) } +} +impl AstBuilder { pub fn from_pieces( path: ast::Path, bounds: impl Iterator, @@ -154,17 +150,21 @@ impl AstBuilder { let bounds = bounds.map(|b| b.syntax().to_string()).collect::>().join(" + "); Self::from_text(&format!("{}: {}", path.syntax(), bounds)) } -} -impl AstBuilder { - fn from_text(text: &str) -> ast::WhereClause { + fn from_text(text: &str) -> ast::WherePred { ast_node_from_file_text(&format!("fn f() where {} {{ }}", text)) } +} +impl AstBuilder { 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 { -- cgit v1.2.3 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 From efeae82f5221d2fdeeeed0bc67a2234647e160dd Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 25 Sep 2019 15:58:40 +0300 Subject: clean up naming --- crates/ra_assists/src/ast_builder.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'crates/ra_assists/src') diff --git a/crates/ra_assists/src/ast_builder.rs b/crates/ra_assists/src/ast_builder.rs index f6409085a..9a62b96b3 100644 --- a/crates/ra_assists/src/ast_builder.rs +++ b/crates/ra_assists/src/ast_builder.rs @@ -44,7 +44,7 @@ impl Make { } impl Make { - pub fn new(text: &str) -> ast::NameRef { + pub fn from(text: &str) -> ast::NameRef { ast_node_from_file_text(&format!("fn f() {{ {}; }}", text)) } } -- cgit v1.2.3