minor builder cleanups

This commit is contained in:
Aleksey Kladov 2019-09-25 15:16:18 +03:00
parent 25fca04753
commit 546f031e4e
3 changed files with 60 additions and 60 deletions

View File

@ -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::<ast::Block>::single_expr(
&AstBuilder::<ast::Expr>::unimplemented(),
AstBuilder::<ast::Expr>::unimplemented(),
));
}
ast_editor.ast().to_owned()

View File

@ -32,7 +32,7 @@ pub(crate) fn fill_match_arms(mut ctx: AssistCtx<impl HirDatabase>) -> Option<As
let arms = variants.filter_map(build_pat).map(|pat| {
AstBuilder::<ast::MatchArm>::from_pieces(
iter::once(pat),
&AstBuilder::<ast::Expr>::unit(),
AstBuilder::<ast::Expr>::unit(),
)
});
let new_arm_list = AstBuilder::<ast::MatchArmList>::from_arms(arms);
@ -66,7 +66,7 @@ fn resolve_enum_def(
}
fn build_pat(var: ast::EnumVariant) -> Option<ast::Pat> {
let path = &AstBuilder::<ast::Path>::from_pieces(var.parent_enum().name()?, var.name()?);
let path = AstBuilder::<ast::Path>::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::Pat> {
ast::StructKind::Named(field_list) => {
let pats = field_list
.fields()
.map(|f| AstBuilder::<ast::BindPat>::from_name(&f.name().unwrap()).into());
.map(|f| AstBuilder::<ast::BindPat>::from_name(f.name().unwrap()).into());
AstBuilder::<ast::RecordPat>::from_pieces(path, pats).into()
}
ast::StructKind::Unit => AstBuilder::<ast::PathPat>::from_path(path).into(),

View File

@ -7,33 +7,29 @@ pub struct AstBuilder<N: AstNode> {
}
impl AstBuilder<ast::RecordField> {
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 {
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<ast::Block> {
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<ast::Expr> {
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<ast::NameRef> {
@ -50,10 +50,6 @@ pub fn new(text: &str) -> ast::NameRef {
}
impl AstBuilder<ast::Path> {
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<ast::BindPat> {
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<ast::PlaceholderPat> {
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<ast::TupleStructPat> {
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<Item = ast::Pat>,
) -> ast::TupleStructPat {
let pats_str = pats.map(|p| p.syntax().to_string()).collect::<Vec<_>>().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<ast::RecordPat> {
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<Item = ast::Pat>) -> ast::RecordPat {
pub fn from_pieces(path: ast::Path, pats: impl Iterator<Item = ast::Pat>) -> ast::RecordPat {
let pats_str = pats.map(|p| p.syntax().to_string()).collect::<Vec<_>>().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<ast::PathPat> {
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<ast::MatchArm> {
fn from_text(text: &str) -> ast::MatchArm {
ast_node_from_file_text(&format!("fn f() {{ match () {{{}}} }}", text))
}
pub fn from_pieces(pats: impl Iterator<Item = ast::Pat>, expr: &ast::Expr) -> ast::MatchArm {
pub fn from_pieces(pats: impl Iterator<Item = ast::Pat>, 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<ast::MatchArmList> {
fn from_text(text: &str) -> ast::MatchArmList {
ast_node_from_file_text(&format!("fn f() {{ match () {{{}}} }}", text))
}
pub fn from_arms(arms: impl Iterator<Item = ast::MatchArm>) -> 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<ast::WherePred> {
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<Item = ast::TypeBound>,
@ -154,17 +150,21 @@ pub fn from_pieces(
let bounds = bounds.map(|b| b.syntax().to_string()).collect::<Vec<_>>().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<ast::WhereClause> {
fn from_text(text: &str) -> ast::WhereClause {
ast_node_from_file_text(&format!("fn f() where {} {{ }}", text))
}
pub fn from_predicates(preds: impl Iterator<Item = ast::WherePred>) -> ast::WhereClause {
let preds = preds.map(|p| p.syntax().to_string()).collect::<Vec<_>>().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<N: AstNode>(text: &str) -> N {