aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2019-09-25 13:16:18 +0100
committerAleksey Kladov <[email protected]>2019-09-25 13:26:05 +0100
commit546f031e4e643c7cc4df44a2a09ac7c4af5e5ad7 (patch)
tree0966100a969ea40544aba805eed93711fb3a85ba /crates
parent25fca0475343170b8398f5c87c76c455205ed3c4 (diff)
minor builder cleanups
Diffstat (limited to 'crates')
-rw-r--r--crates/ra_assists/src/assists/add_missing_impl_members.rs2
-rw-r--r--crates/ra_assists/src/assists/fill_match_arms.rs6
-rw-r--r--crates/ra_assists/src/ast_builder.rs102
3 files changed, 55 insertions, 55 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..11d69a04b 100644
--- a/crates/ra_assists/src/assists/add_missing_impl_members.rs
+++ b/crates/ra_assists/src/assists/add_missing_impl_members.rs
@@ -103,7 +103,7 @@ fn 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(&AstBuilder::<ast::Block>::single_expr(
106 &AstBuilder::<ast::Expr>::unimplemented(), 106 AstBuilder::<ast::Expr>::unimplemented(),
107 )); 107 ));
108 } 108 }
109 ast_editor.ast().to_owned() 109 ast_editor.ast().to_owned()
diff --git a/crates/ra_assists/src/assists/fill_match_arms.rs b/crates/ra_assists/src/assists/fill_match_arms.rs
index 771aa625f..f1c36b805 100644
--- a/crates/ra_assists/src/assists/fill_match_arms.rs
+++ b/crates/ra_assists/src/assists/fill_match_arms.rs
@@ -32,7 +32,7 @@ pub(crate) fn fill_match_arms(mut ctx: AssistCtx<impl HirDatabase>) -> Option<As
32 let arms = variants.filter_map(build_pat).map(|pat| { 32 let arms = variants.filter_map(build_pat).map(|pat| {
33 AstBuilder::<ast::MatchArm>::from_pieces( 33 AstBuilder::<ast::MatchArm>::from_pieces(
34 iter::once(pat), 34 iter::once(pat),
35 &AstBuilder::<ast::Expr>::unit(), 35 AstBuilder::<ast::Expr>::unit(),
36 ) 36 )
37 }); 37 });
38 let new_arm_list = AstBuilder::<ast::MatchArmList>::from_arms(arms); 38 let new_arm_list = AstBuilder::<ast::MatchArmList>::from_arms(arms);
@@ -66,7 +66,7 @@ fn resolve_enum_def(
66} 66}
67 67
68fn build_pat(var: ast::EnumVariant) -> Option<ast::Pat> { 68fn build_pat(var: ast::EnumVariant) -> Option<ast::Pat> {
69 let path = &AstBuilder::<ast::Path>::from_pieces(var.parent_enum().name()?, var.name()?); 69 let path = AstBuilder::<ast::Path>::from_pieces(var.parent_enum().name()?, var.name()?);
70 70
71 let pat: ast::Pat = match var.kind() { 71 let pat: ast::Pat = match var.kind() {
72 ast::StructKind::Tuple(field_list) => { 72 ast::StructKind::Tuple(field_list) => {
@@ -77,7 +77,7 @@ fn build_pat(var: ast::EnumVariant) -> Option<ast::Pat> {
77 ast::StructKind::Named(field_list) => { 77 ast::StructKind::Named(field_list) => {
78 let pats = field_list 78 let pats = field_list
79 .fields() 79 .fields()
80 .map(|f| AstBuilder::<ast::BindPat>::from_name(&f.name().unwrap()).into()); 80 .map(|f| AstBuilder::<ast::BindPat>::from_name(f.name().unwrap()).into());
81 AstBuilder::<ast::RecordPat>::from_pieces(path, pats).into() 81 AstBuilder::<ast::RecordPat>::from_pieces(path, pats).into()
82 } 82 }
83 ast::StructKind::Unit => AstBuilder::<ast::PathPat>::from_path(path).into(), 83 ast::StructKind::Unit => AstBuilder::<ast::PathPat>::from_path(path).into(),
diff --git a/crates/ra_assists/src/ast_builder.rs b/crates/ra_assists/src/ast_builder.rs
index 9b9158ae2..171b1c936 100644
--- a/crates/ra_assists/src/ast_builder.rs
+++ b/crates/ra_assists/src/ast_builder.rs
@@ -7,33 +7,29 @@ pub struct AstBuilder<N: AstNode> {
7} 7}
8 8
9impl AstBuilder<ast::RecordField> { 9impl AstBuilder<ast::RecordField> {
10 fn from_text(text: &str) -> ast::RecordField {
11 ast_node_from_file_text(&format!("fn f() {{ S {{ {}, }} }}", text))
12 }
13
14 pub fn from_pieces(name: ast::NameRef, expr: Option<ast::Expr>) -> ast::RecordField { 10 pub fn from_pieces(name: ast::NameRef, expr: Option<ast::Expr>) -> ast::RecordField {
15 match expr { 11 match expr {
16 Some(expr) => Self::from_text(&format!("{}: {}", name.syntax(), expr.syntax())), 12 Some(expr) => Self::from_text(&format!("{}: {}", name.syntax(), expr.syntax())),
17 None => Self::from_text(&name.syntax().to_string()), 13 None => Self::from_text(&name.syntax().to_string()),
18 } 14 }
19 } 15 }
16
17 fn from_text(text: &str) -> ast::RecordField {
18 ast_node_from_file_text(&format!("fn f() {{ S {{ {}, }} }}", text))
19 }
20} 20}
21 21
22impl AstBuilder<ast::Block> { 22impl AstBuilder<ast::Block> {
23 fn from_text(text: &str) -> ast::Block { 23 pub fn single_expr(e: ast::Expr) -> ast::Block {
24 ast_node_from_file_text(&format!("fn f() {}", text)) 24 Self::from_text(&format!("{{ {} }}", e.syntax()))
25 } 25 }
26 26
27 pub fn single_expr(e: &ast::Expr) -> ast::Block { 27 fn from_text(text: &str) -> ast::Block {
28 Self::from_text(&format!("{{ {} }}", e.syntax())) 28 ast_node_from_file_text(&format!("fn f() {}", text))
29 } 29 }
30} 30}
31 31
32impl AstBuilder<ast::Expr> { 32impl AstBuilder<ast::Expr> {
33 fn from_text(text: &str) -> ast::Expr {
34 ast_node_from_file_text(&format!("const C: () = {};", text))
35 }
36
37 pub fn unit() -> ast::Expr { 33 pub fn unit() -> ast::Expr {
38 Self::from_text("()") 34 Self::from_text("()")
39 } 35 }
@@ -41,6 +37,10 @@ impl AstBuilder<ast::Expr> {
41 pub fn unimplemented() -> ast::Expr { 37 pub fn unimplemented() -> ast::Expr {
42 Self::from_text("unimplemented!()") 38 Self::from_text("unimplemented!()")
43 } 39 }
40
41 fn from_text(text: &str) -> ast::Expr {
42 ast_node_from_file_text(&format!("const C: () = {};", text))
43 }
44} 44}
45 45
46impl AstBuilder<ast::NameRef> { 46impl AstBuilder<ast::NameRef> {
@@ -50,10 +50,6 @@ impl AstBuilder<ast::NameRef> {
50} 50}
51 51
52impl AstBuilder<ast::Path> { 52impl AstBuilder<ast::Path> {
53 fn from_text(text: &str) -> ast::Path {
54 ast_node_from_file_text(text)
55 }
56
57 pub fn from_name(name: ast::Name) -> ast::Path { 53 pub fn from_name(name: ast::Name) -> ast::Path {
58 let name = name.syntax().to_string(); 54 let name = name.syntax().to_string();
59 Self::from_text(name.as_str()) 55 Self::from_text(name.as_str())
@@ -62,91 +58,91 @@ impl AstBuilder<ast::Path> {
62 pub fn from_pieces(enum_name: ast::Name, var_name: ast::Name) -> ast::Path { 58 pub fn from_pieces(enum_name: ast::Name, var_name: ast::Name) -> ast::Path {
63 Self::from_text(&format!("{}::{}", enum_name.syntax(), var_name.syntax())) 59 Self::from_text(&format!("{}::{}", enum_name.syntax(), var_name.syntax()))
64 } 60 }
65}
66 61
67impl AstBuilder<ast::BindPat> { 62 fn from_text(text: &str) -> ast::Path {
68 fn from_text(text: &str) -> ast::BindPat { 63 ast_node_from_file_text(text)
69 ast_node_from_file_text(&format!("fn f({}: ())", text))
70 } 64 }
65}
71 66
72 pub fn from_name(name: &ast::Name) -> ast::BindPat { 67impl AstBuilder<ast::BindPat> {
68 pub fn from_name(name: ast::Name) -> ast::BindPat {
73 Self::from_text(name.text()) 69 Self::from_text(name.text())
74 } 70 }
75}
76 71
77impl AstBuilder<ast::PlaceholderPat> { 72 fn from_text(text: &str) -> ast::BindPat {
78 fn from_text(text: &str) -> ast::PlaceholderPat {
79 ast_node_from_file_text(&format!("fn f({}: ())", text)) 73 ast_node_from_file_text(&format!("fn f({}: ())", text))
80 } 74 }
75}
81 76
77impl AstBuilder<ast::PlaceholderPat> {
82 pub fn placeholder() -> ast::PlaceholderPat { 78 pub fn placeholder() -> ast::PlaceholderPat {
83 Self::from_text("_") 79 Self::from_text("_")
84 } 80 }
85}
86 81
87impl AstBuilder<ast::TupleStructPat> { 82 fn from_text(text: &str) -> ast::PlaceholderPat {
88 fn from_text(text: &str) -> ast::TupleStructPat {
89 ast_node_from_file_text(&format!("fn f({}: ())", text)) 83 ast_node_from_file_text(&format!("fn f({}: ())", text))
90 } 84 }
85}
91 86
87impl AstBuilder<ast::TupleStructPat> {
92 pub fn from_pieces( 88 pub fn from_pieces(
93 path: &ast::Path, 89 path: ast::Path,
94 pats: impl Iterator<Item = ast::Pat>, 90 pats: impl Iterator<Item = ast::Pat>,
95 ) -> ast::TupleStructPat { 91 ) -> ast::TupleStructPat {
96 let pats_str = pats.map(|p| p.syntax().to_string()).collect::<Vec<_>>().join(", "); 92 let pats_str = pats.map(|p| p.syntax().to_string()).collect::<Vec<_>>().join(", ");
97 Self::from_text(&format!("{}({})", path.syntax(), pats_str)) 93 Self::from_text(&format!("{}({})", path.syntax(), pats_str))
98 } 94 }
99}
100 95
101impl AstBuilder<ast::RecordPat> { 96 fn from_text(text: &str) -> ast::TupleStructPat {
102 fn from_text(text: &str) -> ast::RecordPat {
103 ast_node_from_file_text(&format!("fn f({}: ())", text)) 97 ast_node_from_file_text(&format!("fn f({}: ())", text))
104 } 98 }
99}
105 100
106 pub fn from_pieces(path: &ast::Path, pats: impl Iterator<Item = ast::Pat>) -> ast::RecordPat { 101impl AstBuilder<ast::RecordPat> {
102 pub fn from_pieces(path: ast::Path, pats: impl Iterator<Item = ast::Pat>) -> ast::RecordPat {
107 let pats_str = pats.map(|p| p.syntax().to_string()).collect::<Vec<_>>().join(", "); 103 let pats_str = pats.map(|p| p.syntax().to_string()).collect::<Vec<_>>().join(", ");
108 Self::from_text(&format!("{}{{ {} }}", path.syntax(), pats_str)) 104 Self::from_text(&format!("{}{{ {} }}", path.syntax(), pats_str))
109 } 105 }
110}
111 106
112impl AstBuilder<ast::PathPat> { 107 fn from_text(text: &str) -> ast::RecordPat {
113 fn from_text(text: &str) -> ast::PathPat {
114 ast_node_from_file_text(&format!("fn f({}: ())", text)) 108 ast_node_from_file_text(&format!("fn f({}: ())", text))
115 } 109 }
110}
116 111
117 pub fn from_path(path: &ast::Path) -> ast::PathPat { 112impl AstBuilder<ast::PathPat> {
113 pub fn from_path(path: ast::Path) -> ast::PathPat {
118 let path_str = path.syntax().text().to_string(); 114 let path_str = path.syntax().text().to_string();
119 Self::from_text(path_str.as_str()) 115 Self::from_text(path_str.as_str())
120 } 116 }
121}
122 117
123impl AstBuilder<ast::MatchArm> { 118 fn from_text(text: &str) -> ast::PathPat {
124 fn from_text(text: &str) -> ast::MatchArm { 119 ast_node_from_file_text(&format!("fn f({}: ())", text))
125 ast_node_from_file_text(&format!("fn f() {{ match () {{{}}} }}", text))
126 } 120 }
121}
127 122
128 pub fn from_pieces(pats: impl Iterator<Item = ast::Pat>, expr: &ast::Expr) -> ast::MatchArm { 123impl AstBuilder<ast::MatchArm> {
124 pub fn from_pieces(pats: impl Iterator<Item = ast::Pat>, expr: ast::Expr) -> ast::MatchArm {
129 let pats_str = pats.map(|p| p.syntax().to_string()).join(" | "); 125 let pats_str = pats.map(|p| p.syntax().to_string()).join(" | ");
130 Self::from_text(&format!("{} => {}", pats_str, expr.syntax())) 126 Self::from_text(&format!("{} => {}", pats_str, expr.syntax()))
131 } 127 }
132}
133 128
134impl AstBuilder<ast::MatchArmList> { 129 fn from_text(text: &str) -> ast::MatchArm {
135 fn from_text(text: &str) -> ast::MatchArmList {
136 ast_node_from_file_text(&format!("fn f() {{ match () {{{}}} }}", text)) 130 ast_node_from_file_text(&format!("fn f() {{ match () {{{}}} }}", text))
137 } 131 }
132}
138 133
134impl AstBuilder<ast::MatchArmList> {
139 pub fn from_arms(arms: impl Iterator<Item = ast::MatchArm>) -> ast::MatchArmList { 135 pub fn from_arms(arms: impl Iterator<Item = ast::MatchArm>) -> ast::MatchArmList {
140 let arms_str = arms.map(|arm| format!("\n {}", arm.syntax())).join(","); 136 let arms_str = arms.map(|arm| format!("\n {}", arm.syntax())).join(",");
141 Self::from_text(&format!("{},\n", arms_str)) 137 Self::from_text(&format!("{},\n", arms_str))
142 } 138 }
143}
144 139
145impl AstBuilder<ast::WherePred> { 140 fn from_text(text: &str) -> ast::MatchArmList {
146 fn from_text(text: &str) -> ast::WherePred { 141 ast_node_from_file_text(&format!("fn f() {{ match () {{{}}} }}", text))
147 ast_node_from_file_text(&format!("fn f() where {} {{ }}", text))
148 } 142 }
143}
149 144
145impl AstBuilder<ast::WherePred> {
150 pub fn from_pieces( 146 pub fn from_pieces(
151 path: ast::Path, 147 path: ast::Path,
152 bounds: impl Iterator<Item = ast::TypeBound>, 148 bounds: impl Iterator<Item = ast::TypeBound>,
@@ -154,17 +150,21 @@ impl AstBuilder<ast::WherePred> {
154 let bounds = bounds.map(|b| b.syntax().to_string()).collect::<Vec<_>>().join(" + "); 150 let bounds = bounds.map(|b| b.syntax().to_string()).collect::<Vec<_>>().join(" + ");
155 Self::from_text(&format!("{}: {}", path.syntax(), bounds)) 151 Self::from_text(&format!("{}: {}", path.syntax(), bounds))
156 } 152 }
157}
158 153
159impl AstBuilder<ast::WhereClause> { 154 fn from_text(text: &str) -> ast::WherePred {
160 fn from_text(text: &str) -> ast::WhereClause {
161 ast_node_from_file_text(&format!("fn f() where {} {{ }}", text)) 155 ast_node_from_file_text(&format!("fn f() where {} {{ }}", text))
162 } 156 }
157}
163 158
159impl AstBuilder<ast::WhereClause> {
164 pub fn from_predicates(preds: impl Iterator<Item = ast::WherePred>) -> ast::WhereClause { 160 pub fn from_predicates(preds: impl Iterator<Item = ast::WherePred>) -> ast::WhereClause {
165 let preds = preds.map(|p| p.syntax().to_string()).collect::<Vec<_>>().join(", "); 161 let preds = preds.map(|p| p.syntax().to_string()).collect::<Vec<_>>().join(", ");
166 Self::from_text(preds.as_str()) 162 Self::from_text(preds.as_str())
167 } 163 }
164
165 fn from_text(text: &str) -> ast::WhereClause {
166 ast_node_from_file_text(&format!("fn f() where {} {{ }}", text))
167 }
168} 168}
169 169
170fn ast_node_from_file_text<N: AstNode>(text: &str) -> N { 170fn ast_node_from_file_text<N: AstNode>(text: &str) -> N {