From 5b2fd86d2523299c4a5ec58ad33c3474f52f85a2 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 22 Apr 2019 10:10:05 +0300 Subject: more type safety --- crates/ra_assists/src/ast_editor.rs | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) (limited to 'crates/ra_assists/src/ast_editor.rs') diff --git a/crates/ra_assists/src/ast_editor.rs b/crates/ra_assists/src/ast_editor.rs index 7ef58aa8e..13ee82879 100644 --- a/crates/ra_assists/src/ast_editor.rs +++ b/crates/ra_assists/src/ast_editor.rs @@ -137,9 +137,32 @@ pub struct AstBuilder { } impl AstBuilder { - pub fn from_text(text: &str) -> TreeArc { + fn from_text(text: &str) -> TreeArc { ast_node_from_file_text(&format!("fn f() {{ S {{ {}, }} }}", text)) } + + pub fn from_pieces(name: &ast::NameRef, expr: Option<&ast::Expr>) -> TreeArc { + match expr { + Some(expr) => Self::from_text(&format!("{}: {}", name.syntax(), expr.syntax())), + None => Self::from_text(&name.syntax().to_string()), + } + } +} + +impl AstBuilder { + fn from_text(text: &str) -> TreeArc { + ast_node_from_file_text(&format!("fn f() {{ {}; }}", text)) + } + + pub fn unit() -> TreeArc { + Self::from_text("()") + } +} + +impl AstBuilder { + pub fn new(text: &str) -> TreeArc { + ast_node_from_file_text(&format!("fn f() {{ {}; }}", text)) + } } fn ast_node_from_file_text(text: &str) -> TreeArc { -- cgit v1.2.3