From e177c65e36f432821087b215b83c2dad1c97f478 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 13 Nov 2019 11:40:51 +0300 Subject: Use strongly-typed ast building for early-return assist --- crates/ra_syntax/src/ast/make.rs | 52 ++++++++++++++++++++++++---------------- 1 file changed, 31 insertions(+), 21 deletions(-) (limited to 'crates/ra_syntax/src/ast/make.rs') diff --git a/crates/ra_syntax/src/ast/make.rs b/crates/ra_syntax/src/ast/make.rs index 95062ef6c..6c903ca64 100644 --- a/crates/ra_syntax/src/ast/make.rs +++ b/crates/ra_syntax/src/ast/make.rs @@ -4,6 +4,10 @@ use itertools::Itertools; use crate::{ast, AstNode, SourceFile}; +pub fn name(text: &str) -> ast::Name { + ast_from_text(&format!("mod {};", text)) +} + pub fn name_ref(text: &str) -> ast::NameRef { ast_from_text(&format!("fn f() {{ {}; }}", text)) } @@ -43,6 +47,21 @@ pub fn expr_unit() -> ast::Expr { pub fn expr_unimplemented() -> ast::Expr { expr_from_text("unimplemented!()") } +pub fn expr_path(path: ast::Path) -> ast::Expr { + expr_from_text(&path.syntax().to_string()) +} +pub fn expr_continue() -> ast::Expr { + expr_from_text("continue") +} +pub fn expr_break() -> ast::Expr { + expr_from_text("break") +} +pub fn expr_return() -> ast::Expr { + expr_from_text("return") +} +pub fn expr_match(expr: ast::Expr, match_arm_list: ast::MatchArmList) -> ast::Expr { + expr_from_text(&format!("match {} {}", expr.syntax(), match_arm_list.syntax())) +} fn expr_from_text(text: &str) -> ast::Expr { ast_from_text(&format!("const C: () = {};", text)) } @@ -92,8 +111,8 @@ pub fn path_pat(path: ast::Path) -> ast::PathPat { } } -pub fn match_arm(pats: impl Iterator, expr: ast::Expr) -> ast::MatchArm { - let pats_str = pats.map(|p| p.syntax().to_string()).join(" | "); +pub fn match_arm(pats: impl IntoIterator, expr: ast::Expr) -> ast::MatchArm { + let pats_str = pats.into_iter().map(|p| p.syntax().to_string()).join(" | "); return from_text(&format!("{} => {}", pats_str, expr.syntax())); fn from_text(text: &str) -> ast::MatchArm { @@ -101,8 +120,8 @@ pub fn match_arm(pats: impl Iterator, expr: ast::Expr) -> ast:: } } -pub fn match_arm_list(arms: impl Iterator) -> ast::MatchArmList { - let arms_str = arms.map(|arm| format!("\n {}", arm.syntax())).join(","); +pub fn match_arm_list(arms: impl IntoIterator) -> ast::MatchArmList { + let arms_str = arms.into_iter().map(|arm| format!("\n {}", arm.syntax())).join(","); return from_text(&format!("{},\n", arms_str)); fn from_text(text: &str) -> ast::MatchArmList { @@ -110,23 +129,6 @@ pub fn match_arm_list(arms: impl Iterator) -> ast::MatchAr } } -pub fn let_match_early(expr: ast::Expr, path: &str, early_expression: &str) -> ast::LetStmt { - return from_text(&format!( - r#"let {} = match {} {{ - {}(it) => it, - None => {}, -}};"#, - expr.syntax().text(), - expr.syntax().text(), - path, - early_expression - )); - - fn from_text(text: &str) -> ast::LetStmt { - ast_from_text(&format!("fn f() {{ {} }}", text)) - } -} - pub fn where_pred(path: ast::Path, bounds: impl Iterator) -> ast::WherePred { let bounds = bounds.map(|b| b.syntax().to_string()).join(" + "); return from_text(&format!("{}: {}", path.syntax(), bounds)); @@ -153,6 +155,14 @@ pub fn if_expression(condition: &ast::Expr, statement: &str) -> ast::IfExpr { )) } +pub fn let_stmt(pattern: ast::Pat, initializer: Option) -> ast::LetStmt { + let text = match initializer { + Some(it) => format!("let {} = {};", pattern.syntax(), it.syntax()), + None => format!("let {};", pattern.syntax()), + }; + ast_from_text(&format!("fn f() {{ {} }}", text)) +} + fn ast_from_text(text: &str) -> N { let parse = SourceFile::parse(text); let res = parse.tree().syntax().descendants().find_map(N::cast).unwrap(); -- cgit v1.2.3 From 4cea6bb6f11e28a2d1d2e023d46caa82b0f38796 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 13 Nov 2019 11:55:43 +0300 Subject: Make make:: builders slightly more convenient --- crates/ra_syntax/src/ast/make.rs | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) (limited to 'crates/ra_syntax/src/ast/make.rs') diff --git a/crates/ra_syntax/src/ast/make.rs b/crates/ra_syntax/src/ast/make.rs index 6c903ca64..9749327fa 100644 --- a/crates/ra_syntax/src/ast/make.rs +++ b/crates/ra_syntax/src/ast/make.rs @@ -84,9 +84,9 @@ pub fn placeholder_pat() -> ast::PlaceholderPat { pub fn tuple_struct_pat( path: ast::Path, - pats: impl Iterator, + pats: impl IntoIterator, ) -> ast::TupleStructPat { - let pats_str = pats.map(|p| p.syntax().to_string()).join(", "); + let pats_str = pats.into_iter().map(|p| p.syntax().to_string()).join(", "); return from_text(&format!("{}({})", path.syntax(), pats_str)); fn from_text(text: &str) -> ast::TupleStructPat { @@ -94,8 +94,8 @@ pub fn tuple_struct_pat( } } -pub fn record_pat(path: ast::Path, pats: impl Iterator) -> ast::RecordPat { - let pats_str = pats.map(|p| p.syntax().to_string()).join(", "); +pub fn record_pat(path: ast::Path, pats: impl IntoIterator) -> ast::RecordPat { + let pats_str = pats.into_iter().map(|p| p.syntax().to_string()).join(", "); return from_text(&format!("{} {{ {} }}", path.syntax(), pats_str)); fn from_text(text: &str) -> ast::RecordPat { @@ -129,8 +129,11 @@ pub fn match_arm_list(arms: impl IntoIterator) -> ast::Mat } } -pub fn where_pred(path: ast::Path, bounds: impl Iterator) -> ast::WherePred { - let bounds = bounds.map(|b| b.syntax().to_string()).join(" + "); +pub fn where_pred( + path: ast::Path, + bounds: impl IntoIterator, +) -> ast::WherePred { + let bounds = bounds.into_iter().map(|b| b.syntax().to_string()).join(" + "); return from_text(&format!("{}: {}", path.syntax(), bounds)); fn from_text(text: &str) -> ast::WherePred { @@ -138,8 +141,8 @@ pub fn where_pred(path: ast::Path, bounds: impl Iterator) } } -pub fn where_clause(preds: impl Iterator) -> ast::WhereClause { - let preds = preds.map(|p| p.syntax().to_string()).join(", "); +pub fn where_clause(preds: impl IntoIterator) -> ast::WhereClause { + let preds = preds.into_iter().map(|p| p.syntax().to_string()).join(", "); return from_text(preds.as_str()); fn from_text(text: &str) -> ast::WhereClause { -- cgit v1.2.3