aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_assists/src
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2019-04-22 08:10:05 +0100
committerAleksey Kladov <[email protected]>2019-04-22 08:11:06 +0100
commit5b2fd86d2523299c4a5ec58ad33c3474f52f85a2 (patch)
treea160da639b703d328cb0e1b7f755f64c22864656 /crates/ra_assists/src
parent97f41d7343a4801f296cd6a7a45ece9249c15886 (diff)
more type safety
Diffstat (limited to 'crates/ra_assists/src')
-rw-r--r--crates/ra_assists/src/ast_editor.rs25
-rw-r--r--crates/ra_assists/src/fill_struct_fields.rs6
-rw-r--r--crates/ra_assists/src/lib.rs2
3 files changed, 29 insertions, 4 deletions
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<N: AstNode> {
137} 137}
138 138
139impl AstBuilder<ast::NamedField> { 139impl AstBuilder<ast::NamedField> {
140 pub fn from_text(text: &str) -> TreeArc<ast::NamedField> { 140 fn from_text(text: &str) -> TreeArc<ast::NamedField> {
141 ast_node_from_file_text(&format!("fn f() {{ S {{ {}, }} }}", text)) 141 ast_node_from_file_text(&format!("fn f() {{ S {{ {}, }} }}", text))
142 } 142 }
143
144 pub fn from_pieces(name: &ast::NameRef, expr: Option<&ast::Expr>) -> TreeArc<ast::NamedField> {
145 match expr {
146 Some(expr) => Self::from_text(&format!("{}: {}", name.syntax(), expr.syntax())),
147 None => Self::from_text(&name.syntax().to_string()),
148 }
149 }
150}
151
152impl AstBuilder<ast::Expr> {
153 fn from_text(text: &str) -> TreeArc<ast::Expr> {
154 ast_node_from_file_text(&format!("fn f() {{ {}; }}", text))
155 }
156
157 pub fn unit() -> TreeArc<ast::Expr> {
158 Self::from_text("()")
159 }
160}
161
162impl AstBuilder<ast::NameRef> {
163 pub fn new(text: &str) -> TreeArc<ast::NameRef> {
164 ast_node_from_file_text(&format!("fn f() {{ {}; }}", text))
165 }
143} 166}
144 167
145fn ast_node_from_file_text<N: AstNode>(text: &str) -> TreeArc<N> { 168fn ast_node_from_file_text<N: AstNode>(text: &str) -> TreeArc<N> {
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<impl HirDatabase>) -> Option
37 }; 37 };
38 38
39 for field in fields { 39 for field in fields {
40 let field = 40 let field = AstBuilder::<ast::NamedField>::from_pieces(
41 AstBuilder::<ast::NamedField>::from_text(&format!("{}: ()", field.name(db))); 41 &AstBuilder::<ast::NameRef>::new(&field.name(db).to_string()),
42 Some(&AstBuilder::<ast::Expr>::unit()),
43 );
42 ast_editor.append_field(&field); 44 ast_editor.append_field(&field);
43 } 45 }
44 46
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 {
37 pub target: Option<TextRange>, 37 pub target: Option<TextRange>,
38} 38}
39 39
40/// Return all the assists applicable at the given position. 40/// Return all the assists eapplicable at the given position.
41/// 41///
42/// Assists are returned in the "unresolved" state, that is only labels are 42/// Assists are returned in the "unresolved" state, that is only labels are
43/// returned, without actual edits. 43/// returned, without actual edits.