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 ++++++++++++++++++++++++- crates/ra_assists/src/fill_struct_fields.rs | 6 ++++-- crates/ra_assists/src/lib.rs | 2 +- 3 files changed, 29 insertions(+), 4 deletions(-) (limited to 'crates/ra_assists') 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 { diff --git a/crates/ra_assists/src/fill_struct_fields.rs b/crates/ra_assists/src/fill_struct_fields.rs index 302d62ab1..54b70e17d 100644 --- a/crates/ra_assists/src/fill_struct_fields.rs +++ b/crates/ra_assists/src/fill_struct_fields.rs @@ -37,8 +37,10 @@ pub(crate) fn fill_struct_fields(mut ctx: AssistCtx) -> Option }; for field in fields { - let field = - AstBuilder::::from_text(&format!("{}: ()", field.name(db))); + let field = AstBuilder::::from_pieces( + &AstBuilder::::new(&field.name(db).to_string()), + Some(&AstBuilder::::unit()), + ); ast_editor.append_field(&field); } diff --git a/crates/ra_assists/src/lib.rs b/crates/ra_assists/src/lib.rs index 3151b1c44..60b4d5c63 100644 --- a/crates/ra_assists/src/lib.rs +++ b/crates/ra_assists/src/lib.rs @@ -37,7 +37,7 @@ pub struct AssistAction { pub target: Option, } -/// Return all the assists applicable at the given position. +/// Return all the assists eapplicable at the given position. /// /// Assists are returned in the "unresolved" state, that is only labels are /// returned, without actual edits. -- cgit v1.2.3