aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_assists
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_assists')
-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.