aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_assists
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_assists')
-rw-r--r--crates/ra_assists/src/assists/add_missing_impl_members.rs6
-rw-r--r--crates/ra_assists/src/assists/fill_match_arms.rs25
-rw-r--r--crates/ra_assists/src/assists/move_bounds.rs9
-rw-r--r--crates/ra_assists/src/ast_builder.rs131
4 files changed, 77 insertions, 94 deletions
diff --git a/crates/ra_assists/src/assists/add_missing_impl_members.rs b/crates/ra_assists/src/assists/add_missing_impl_members.rs
index 2894bdd8a..22d20909d 100644
--- a/crates/ra_assists/src/assists/add_missing_impl_members.rs
+++ b/crates/ra_assists/src/assists/add_missing_impl_members.rs
@@ -4,7 +4,7 @@ use ra_syntax::{
4 SmolStr, 4 SmolStr,
5}; 5};
6 6
7use crate::{ast_builder::AstBuilder, ast_editor::AstEditor, Assist, AssistCtx, AssistId}; 7use crate::{ast_builder::Make, ast_editor::AstEditor, Assist, AssistCtx, AssistId};
8 8
9#[derive(PartialEq)] 9#[derive(PartialEq)]
10enum AddMissingImplMembersMode { 10enum AddMissingImplMembersMode {
@@ -102,9 +102,7 @@ fn strip_docstring(item: ast::ImplItem) -> ast::ImplItem {
102fn add_body(fn_def: ast::FnDef) -> ast::FnDef { 102fn add_body(fn_def: ast::FnDef) -> ast::FnDef {
103 let mut ast_editor = AstEditor::new(fn_def.clone()); 103 let mut ast_editor = AstEditor::new(fn_def.clone());
104 if fn_def.body().is_none() { 104 if fn_def.body().is_none() {
105 ast_editor.set_body(&AstBuilder::<ast::Block>::single_expr( 105 ast_editor.set_body(&Make::<ast::Block>::single_expr(Make::<ast::Expr>::unimplemented()));
106 &AstBuilder::<ast::Expr>::unimplemented(),
107 ));
108 } 106 }
109 ast_editor.ast().to_owned() 107 ast_editor.ast().to_owned()
110} 108}
diff --git a/crates/ra_assists/src/assists/fill_match_arms.rs b/crates/ra_assists/src/assists/fill_match_arms.rs
index 771aa625f..817433526 100644
--- a/crates/ra_assists/src/assists/fill_match_arms.rs
+++ b/crates/ra_assists/src/assists/fill_match_arms.rs
@@ -3,7 +3,7 @@ use std::iter;
3use hir::{db::HirDatabase, Adt, HasSource}; 3use hir::{db::HirDatabase, Adt, HasSource};
4use ra_syntax::ast::{self, AstNode, NameOwner}; 4use ra_syntax::ast::{self, AstNode, NameOwner};
5 5
6use crate::{ast_builder::AstBuilder, Assist, AssistCtx, AssistId}; 6use crate::{ast_builder::Make, Assist, AssistCtx, AssistId};
7 7
8pub(crate) fn fill_match_arms(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { 8pub(crate) fn fill_match_arms(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
9 let match_expr = ctx.node_at_offset::<ast::MatchExpr>()?; 9 let match_expr = ctx.node_at_offset::<ast::MatchExpr>()?;
@@ -29,13 +29,10 @@ pub(crate) fn fill_match_arms(mut ctx: AssistCtx<impl HirDatabase>) -> Option<As
29 29
30 ctx.add_action(AssistId("fill_match_arms"), "fill match arms", |edit| { 30 ctx.add_action(AssistId("fill_match_arms"), "fill match arms", |edit| {
31 let variants = variant_list.variants(); 31 let variants = variant_list.variants();
32 let arms = variants.filter_map(build_pat).map(|pat| { 32 let arms = variants
33 AstBuilder::<ast::MatchArm>::from_pieces( 33 .filter_map(build_pat)
34 iter::once(pat), 34 .map(|pat| Make::<ast::MatchArm>::from(iter::once(pat), Make::<ast::Expr>::unit()));
35 &AstBuilder::<ast::Expr>::unit(), 35 let new_arm_list = Make::<ast::MatchArmList>::from_arms(arms);
36 )
37 });
38 let new_arm_list = AstBuilder::<ast::MatchArmList>::from_arms(arms);
39 36
40 edit.target(match_expr.syntax().text_range()); 37 edit.target(match_expr.syntax().text_range());
41 edit.set_cursor(expr.syntax().text_range().start()); 38 edit.set_cursor(expr.syntax().text_range().start());
@@ -66,21 +63,21 @@ fn resolve_enum_def(
66} 63}
67 64
68fn build_pat(var: ast::EnumVariant) -> Option<ast::Pat> { 65fn build_pat(var: ast::EnumVariant) -> Option<ast::Pat> {
69 let path = &AstBuilder::<ast::Path>::from_pieces(var.parent_enum().name()?, var.name()?); 66 let path = Make::<ast::Path>::from(var.parent_enum().name()?, var.name()?);
70 67
71 let pat: ast::Pat = match var.kind() { 68 let pat: ast::Pat = match var.kind() {
72 ast::StructKind::Tuple(field_list) => { 69 ast::StructKind::Tuple(field_list) => {
73 let pats = iter::repeat(AstBuilder::<ast::PlaceholderPat>::placeholder().into()) 70 let pats = iter::repeat(Make::<ast::PlaceholderPat>::placeholder().into())
74 .take(field_list.fields().count()); 71 .take(field_list.fields().count());
75 AstBuilder::<ast::TupleStructPat>::from_pieces(path, pats).into() 72 Make::<ast::TupleStructPat>::from(path, pats).into()
76 } 73 }
77 ast::StructKind::Named(field_list) => { 74 ast::StructKind::Named(field_list) => {
78 let pats = field_list 75 let pats = field_list
79 .fields() 76 .fields()
80 .map(|f| AstBuilder::<ast::BindPat>::from_name(&f.name().unwrap()).into()); 77 .map(|f| Make::<ast::BindPat>::from_name(f.name().unwrap()).into());
81 AstBuilder::<ast::RecordPat>::from_pieces(path, pats).into() 78 Make::<ast::RecordPat>::from(path, pats).into()
82 } 79 }
83 ast::StructKind::Unit => AstBuilder::<ast::PathPat>::from_path(path).into(), 80 ast::StructKind::Unit => Make::<ast::PathPat>::from_path(path).into(),
84 }; 81 };
85 82
86 Some(pat) 83 Some(pat)
diff --git a/crates/ra_assists/src/assists/move_bounds.rs b/crates/ra_assists/src/assists/move_bounds.rs
index aa9036fed..6fd2fb72b 100644
--- a/crates/ra_assists/src/assists/move_bounds.rs
+++ b/crates/ra_assists/src/assists/move_bounds.rs
@@ -6,7 +6,7 @@ use ra_syntax::{
6 TextRange, 6 TextRange,
7}; 7};
8 8
9use crate::{ast_builder::AstBuilder, Assist, AssistCtx, AssistId}; 9use crate::{ast_builder::Make, Assist, AssistCtx, AssistId};
10 10
11pub(crate) fn move_bounds_to_where_clause(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { 11pub(crate) fn move_bounds_to_where_clause(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
12 let type_param_list = ctx.node_at_offset::<ast::TypeParamList>()?; 12 let type_param_list = ctx.node_at_offset::<ast::TypeParamList>()?;
@@ -52,7 +52,7 @@ pub(crate) fn move_bounds_to_where_clause(mut ctx: AssistCtx<impl HirDatabase>)
52 } 52 }
53 53
54 let predicates = type_params.iter().filter_map(build_predicate); 54 let predicates = type_params.iter().filter_map(build_predicate);
55 let where_clause = AstBuilder::<ast::WhereClause>::from_predicates(predicates); 55 let where_clause = Make::<ast::WhereClause>::from_predicates(predicates);
56 56
57 let to_insert = match anchor.prev_sibling_or_token() { 57 let to_insert = match anchor.prev_sibling_or_token() {
58 Some(ref elem) if elem.kind() == WHITESPACE => { 58 Some(ref elem) if elem.kind() == WHITESPACE => {
@@ -69,9 +69,8 @@ pub(crate) fn move_bounds_to_where_clause(mut ctx: AssistCtx<impl HirDatabase>)
69} 69}
70 70
71fn build_predicate(param: &ast::TypeParam) -> Option<ast::WherePred> { 71fn build_predicate(param: &ast::TypeParam) -> Option<ast::WherePred> {
72 let path = AstBuilder::<ast::Path>::from_name(param.name()?); 72 let path = Make::<ast::Path>::from_name(param.name()?);
73 let predicate = 73 let predicate = Make::<ast::WherePred>::from(path, param.type_bound_list()?.bounds());
74 AstBuilder::<ast::WherePred>::from_pieces(path, param.type_bound_list()?.bounds());
75 Some(predicate) 74 Some(predicate)
76} 75}
77 76
diff --git a/crates/ra_assists/src/ast_builder.rs b/crates/ra_assists/src/ast_builder.rs
index e4ea1fca9..9a62b96b3 100644
--- a/crates/ra_assists/src/ast_builder.rs
+++ b/crates/ra_assists/src/ast_builder.rs
@@ -1,44 +1,35 @@
1use itertools::Itertools; 1use itertools::Itertools;
2 2
3use hir::Name;
4use ra_syntax::{ast, AstNode, SourceFile}; 3use ra_syntax::{ast, AstNode, SourceFile};
5 4
6pub struct AstBuilder<N: AstNode> { 5pub struct Make<N: AstNode> {
7 _phantom: std::marker::PhantomData<N>, 6 _phantom: std::marker::PhantomData<N>,
8} 7}
9 8
10impl AstBuilder<ast::RecordField> { 9impl Make<ast::RecordField> {
11 pub fn from_name(name: &Name) -> ast::RecordField { 10 pub fn from(name: ast::NameRef, expr: Option<ast::Expr>) -> ast::RecordField {
12 ast_node_from_file_text(&format!("fn f() {{ S {{ {}: (), }} }}", name))
13 }
14
15 fn from_text(text: &str) -> ast::RecordField {
16 ast_node_from_file_text(&format!("fn f() {{ S {{ {}, }} }}", text))
17 }
18
19 pub fn from_pieces(name: &ast::NameRef, expr: Option<&ast::Expr>) -> ast::RecordField {
20 match expr { 11 match expr {
21 Some(expr) => Self::from_text(&format!("{}: {}", name.syntax(), expr.syntax())), 12 Some(expr) => Self::from_text(&format!("{}: {}", name.syntax(), expr.syntax())),
22 None => Self::from_text(&name.syntax().to_string()), 13 None => Self::from_text(&name.syntax().to_string()),
23 } 14 }
24 } 15 }
25}
26 16
27impl AstBuilder<ast::Block> { 17 fn from_text(text: &str) -> ast::RecordField {
28 fn from_text(text: &str) -> ast::Block { 18 ast_node_from_file_text(&format!("fn f() {{ S {{ {}, }} }}", text))
29 ast_node_from_file_text(&format!("fn f() {}", text))
30 } 19 }
20}
31 21
32 pub fn single_expr(e: &ast::Expr) -> ast::Block { 22impl Make<ast::Block> {
23 pub fn single_expr(e: ast::Expr) -> ast::Block {
33 Self::from_text(&format!("{{ {} }}", e.syntax())) 24 Self::from_text(&format!("{{ {} }}", e.syntax()))
34 } 25 }
35}
36 26
37impl AstBuilder<ast::Expr> { 27 fn from_text(text: &str) -> ast::Block {
38 fn from_text(text: &str) -> ast::Expr { 28 ast_node_from_file_text(&format!("fn f() {}", text))
39 ast_node_from_file_text(&format!("const C: () = {};", text))
40 } 29 }
30}
41 31
32impl Make<ast::Expr> {
42 pub fn unit() -> ast::Expr { 33 pub fn unit() -> ast::Expr {
43 Self::from_text("()") 34 Self::from_text("()")
44 } 35 }
@@ -46,130 +37,128 @@ impl AstBuilder<ast::Expr> {
46 pub fn unimplemented() -> ast::Expr { 37 pub fn unimplemented() -> ast::Expr {
47 Self::from_text("unimplemented!()") 38 Self::from_text("unimplemented!()")
48 } 39 }
49}
50 40
51impl AstBuilder<ast::NameRef> { 41 fn from_text(text: &str) -> ast::Expr {
52 pub fn new(text: &str) -> ast::NameRef { 42 ast_node_from_file_text(&format!("const C: () = {};", text))
53 ast_node_from_file_text(&format!("fn f() {{ {}; }}", text))
54 } 43 }
55} 44}
56 45
57impl AstBuilder<ast::Path> { 46impl Make<ast::NameRef> {
58 fn from_text(text: &str) -> ast::Path { 47 pub fn from(text: &str) -> ast::NameRef {
59 ast_node_from_file_text(text) 48 ast_node_from_file_text(&format!("fn f() {{ {}; }}", text))
60 } 49 }
50}
61 51
52impl Make<ast::Path> {
62 pub fn from_name(name: ast::Name) -> ast::Path { 53 pub fn from_name(name: ast::Name) -> ast::Path {
63 let name = name.syntax().to_string(); 54 let name = name.syntax().to_string();
64 Self::from_text(name.as_str()) 55 Self::from_text(name.as_str())
65 } 56 }
66 57
67 pub fn from_pieces(enum_name: ast::Name, var_name: ast::Name) -> ast::Path { 58 pub fn from(enum_name: ast::Name, var_name: ast::Name) -> ast::Path {
68 Self::from_text(&format!("{}::{}", enum_name.syntax(), var_name.syntax())) 59 Self::from_text(&format!("{}::{}", enum_name.syntax(), var_name.syntax()))
69 } 60 }
70}
71 61
72impl AstBuilder<ast::BindPat> { 62 fn from_text(text: &str) -> ast::Path {
73 fn from_text(text: &str) -> ast::BindPat { 63 ast_node_from_file_text(text)
74 ast_node_from_file_text(&format!("fn f({}: ())", text))
75 } 64 }
65}
76 66
77 pub fn from_name(name: &ast::Name) -> ast::BindPat { 67impl Make<ast::BindPat> {
68 pub fn from_name(name: ast::Name) -> ast::BindPat {
78 Self::from_text(name.text()) 69 Self::from_text(name.text())
79 } 70 }
80}
81 71
82impl AstBuilder<ast::PlaceholderPat> { 72 fn from_text(text: &str) -> ast::BindPat {
83 fn from_text(text: &str) -> ast::PlaceholderPat {
84 ast_node_from_file_text(&format!("fn f({}: ())", text)) 73 ast_node_from_file_text(&format!("fn f({}: ())", text))
85 } 74 }
75}
86 76
77impl Make<ast::PlaceholderPat> {
87 pub fn placeholder() -> ast::PlaceholderPat { 78 pub fn placeholder() -> ast::PlaceholderPat {
88 Self::from_text("_") 79 Self::from_text("_")
89 } 80 }
90}
91 81
92impl AstBuilder<ast::TupleStructPat> { 82 fn from_text(text: &str) -> ast::PlaceholderPat {
93 fn from_text(text: &str) -> ast::TupleStructPat {
94 ast_node_from_file_text(&format!("fn f({}: ())", text)) 83 ast_node_from_file_text(&format!("fn f({}: ())", text))
95 } 84 }
85}
96 86
97 pub fn from_pieces( 87impl Make<ast::TupleStructPat> {
98 path: &ast::Path, 88 pub fn from(path: ast::Path, pats: impl Iterator<Item = ast::Pat>) -> ast::TupleStructPat {
99 pats: impl Iterator<Item = ast::Pat>,
100 ) -> ast::TupleStructPat {
101 let pats_str = pats.map(|p| p.syntax().to_string()).collect::<Vec<_>>().join(", "); 89 let pats_str = pats.map(|p| p.syntax().to_string()).collect::<Vec<_>>().join(", ");
102 Self::from_text(&format!("{}({})", path.syntax(), pats_str)) 90 Self::from_text(&format!("{}({})", path.syntax(), pats_str))
103 } 91 }
104}
105 92
106impl AstBuilder<ast::RecordPat> { 93 fn from_text(text: &str) -> ast::TupleStructPat {
107 fn from_text(text: &str) -> ast::RecordPat {
108 ast_node_from_file_text(&format!("fn f({}: ())", text)) 94 ast_node_from_file_text(&format!("fn f({}: ())", text))
109 } 95 }
96}
110 97
111 pub fn from_pieces(path: &ast::Path, pats: impl Iterator<Item = ast::Pat>) -> ast::RecordPat { 98impl Make<ast::RecordPat> {
99 pub fn from(path: ast::Path, pats: impl Iterator<Item = ast::Pat>) -> ast::RecordPat {
112 let pats_str = pats.map(|p| p.syntax().to_string()).collect::<Vec<_>>().join(", "); 100 let pats_str = pats.map(|p| p.syntax().to_string()).collect::<Vec<_>>().join(", ");
113 Self::from_text(&format!("{}{{ {} }}", path.syntax(), pats_str)) 101 Self::from_text(&format!("{}{{ {} }}", path.syntax(), pats_str))
114 } 102 }
115}
116 103
117impl AstBuilder<ast::PathPat> { 104 fn from_text(text: &str) -> ast::RecordPat {
118 fn from_text(text: &str) -> ast::PathPat {
119 ast_node_from_file_text(&format!("fn f({}: ())", text)) 105 ast_node_from_file_text(&format!("fn f({}: ())", text))
120 } 106 }
107}
121 108
122 pub fn from_path(path: &ast::Path) -> ast::PathPat { 109impl Make<ast::PathPat> {
110 pub fn from_path(path: ast::Path) -> ast::PathPat {
123 let path_str = path.syntax().text().to_string(); 111 let path_str = path.syntax().text().to_string();
124 Self::from_text(path_str.as_str()) 112 Self::from_text(path_str.as_str())
125 } 113 }
126}
127 114
128impl AstBuilder<ast::MatchArm> { 115 fn from_text(text: &str) -> ast::PathPat {
129 fn from_text(text: &str) -> ast::MatchArm { 116 ast_node_from_file_text(&format!("fn f({}: ())", text))
130 ast_node_from_file_text(&format!("fn f() {{ match () {{{}}} }}", text))
131 } 117 }
118}
132 119
133 pub fn from_pieces(pats: impl Iterator<Item = ast::Pat>, expr: &ast::Expr) -> ast::MatchArm { 120impl Make<ast::MatchArm> {
121 pub fn from(pats: impl Iterator<Item = ast::Pat>, expr: ast::Expr) -> ast::MatchArm {
134 let pats_str = pats.map(|p| p.syntax().to_string()).join(" | "); 122 let pats_str = pats.map(|p| p.syntax().to_string()).join(" | ");
135 Self::from_text(&format!("{} => {}", pats_str, expr.syntax())) 123 Self::from_text(&format!("{} => {}", pats_str, expr.syntax()))
136 } 124 }
137}
138 125
139impl AstBuilder<ast::MatchArmList> { 126 fn from_text(text: &str) -> ast::MatchArm {
140 fn from_text(text: &str) -> ast::MatchArmList {
141 ast_node_from_file_text(&format!("fn f() {{ match () {{{}}} }}", text)) 127 ast_node_from_file_text(&format!("fn f() {{ match () {{{}}} }}", text))
142 } 128 }
129}
143 130
131impl Make<ast::MatchArmList> {
144 pub fn from_arms(arms: impl Iterator<Item = ast::MatchArm>) -> ast::MatchArmList { 132 pub fn from_arms(arms: impl Iterator<Item = ast::MatchArm>) -> ast::MatchArmList {
145 let arms_str = arms.map(|arm| format!("\n {}", arm.syntax())).join(","); 133 let arms_str = arms.map(|arm| format!("\n {}", arm.syntax())).join(",");
146 Self::from_text(&format!("{},\n", arms_str)) 134 Self::from_text(&format!("{},\n", arms_str))
147 } 135 }
148}
149 136
150impl AstBuilder<ast::WherePred> { 137 fn from_text(text: &str) -> ast::MatchArmList {
151 fn from_text(text: &str) -> ast::WherePred { 138 ast_node_from_file_text(&format!("fn f() {{ match () {{{}}} }}", text))
152 ast_node_from_file_text(&format!("fn f() where {} {{ }}", text))
153 } 139 }
140}
154 141
155 pub fn from_pieces( 142impl Make<ast::WherePred> {
156 path: ast::Path, 143 pub fn from(path: ast::Path, bounds: impl Iterator<Item = ast::TypeBound>) -> ast::WherePred {
157 bounds: impl Iterator<Item = ast::TypeBound>,
158 ) -> ast::WherePred {
159 let bounds = bounds.map(|b| b.syntax().to_string()).collect::<Vec<_>>().join(" + "); 144 let bounds = bounds.map(|b| b.syntax().to_string()).collect::<Vec<_>>().join(" + ");
160 Self::from_text(&format!("{}: {}", path.syntax(), bounds)) 145 Self::from_text(&format!("{}: {}", path.syntax(), bounds))
161 } 146 }
162}
163 147
164impl AstBuilder<ast::WhereClause> { 148 fn from_text(text: &str) -> ast::WherePred {
165 fn from_text(text: &str) -> ast::WhereClause {
166 ast_node_from_file_text(&format!("fn f() where {} {{ }}", text)) 149 ast_node_from_file_text(&format!("fn f() where {} {{ }}", text))
167 } 150 }
151}
168 152
153impl Make<ast::WhereClause> {
169 pub fn from_predicates(preds: impl Iterator<Item = ast::WherePred>) -> ast::WhereClause { 154 pub fn from_predicates(preds: impl Iterator<Item = ast::WherePred>) -> ast::WhereClause {
170 let preds = preds.map(|p| p.syntax().to_string()).collect::<Vec<_>>().join(", "); 155 let preds = preds.map(|p| p.syntax().to_string()).collect::<Vec<_>>().join(", ");
171 Self::from_text(preds.as_str()) 156 Self::from_text(preds.as_str())
172 } 157 }
158
159 fn from_text(text: &str) -> ast::WhereClause {
160 ast_node_from_file_text(&format!("fn f() where {} {{ }}", text))
161 }
173} 162}
174 163
175fn ast_node_from_file_text<N: AstNode>(text: &str) -> N { 164fn ast_node_from_file_text<N: AstNode>(text: &str) -> N {