aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--crates/ra_syntax/src/ast/make.rs57
1 files changed, 28 insertions, 29 deletions
diff --git a/crates/ra_syntax/src/ast/make.rs b/crates/ra_syntax/src/ast/make.rs
index e29600439..2cc9ff153 100644
--- a/crates/ra_syntax/src/ast/make.rs
+++ b/crates/ra_syntax/src/ast/make.rs
@@ -13,13 +13,13 @@ pub fn name_ref(text: &str) -> ast::NameRef {
13} 13}
14 14
15pub fn path_segment(name_ref: ast::NameRef) -> ast::PathSegment { 15pub fn path_segment(name_ref: ast::NameRef) -> ast::PathSegment {
16 ast_from_text(&format!("use {};", name_ref.syntax())) 16 ast_from_text(&format!("use {};", name_ref))
17} 17}
18pub fn path_unqualified(segment: ast::PathSegment) -> ast::Path { 18pub fn path_unqualified(segment: ast::PathSegment) -> ast::Path {
19 path_from_text(&format!("use {}", segment.syntax())) 19 path_from_text(&format!("use {}", segment))
20} 20}
21pub fn path_qualified(qual: ast::Path, segment: ast::PathSegment) -> ast::Path { 21pub fn path_qualified(qual: ast::Path, segment: ast::PathSegment) -> ast::Path {
22 path_from_text(&format!("{}::{}", qual.syntax(), segment.syntax())) 22 path_from_text(&format!("{}::{}", qual, segment))
23} 23}
24fn path_from_text(text: &str) -> ast::Path { 24fn path_from_text(text: &str) -> ast::Path {
25 ast_from_text(text) 25 ast_from_text(text)
@@ -33,10 +33,10 @@ pub fn use_tree(
33 let mut buf = "use ".to_string(); 33 let mut buf = "use ".to_string();
34 buf += &path.syntax().to_string(); 34 buf += &path.syntax().to_string();
35 if let Some(use_tree_list) = use_tree_list { 35 if let Some(use_tree_list) = use_tree_list {
36 buf += &format!("::{}", use_tree_list.syntax()); 36 buf += &format!("::{}", use_tree_list);
37 } 37 }
38 if let Some(alias) = alias { 38 if let Some(alias) = alias {
39 buf += &format!(" {}", alias.syntax()); 39 buf += &format!(" {}", alias);
40 } 40 }
41 ast_from_text(&buf) 41 ast_from_text(&buf)
42} 42}
@@ -47,13 +47,13 @@ pub fn use_tree_list(use_trees: impl IntoIterator<Item = ast::UseTree>) -> ast::
47} 47}
48 48
49pub fn use_item(use_tree: ast::UseTree) -> ast::UseItem { 49pub fn use_item(use_tree: ast::UseTree) -> ast::UseItem {
50 ast_from_text(&format!("use {};", use_tree.syntax())) 50 ast_from_text(&format!("use {};", use_tree))
51} 51}
52 52
53pub fn record_field(name: ast::NameRef, expr: Option<ast::Expr>) -> ast::RecordField { 53pub fn record_field(name: ast::NameRef, expr: Option<ast::Expr>) -> ast::RecordField {
54 return match expr { 54 return match expr {
55 Some(expr) => from_text(&format!("{}: {}", name.syntax(), expr.syntax())), 55 Some(expr) => from_text(&format!("{}: {}", name, expr)),
56 None => from_text(&name.syntax().to_string()), 56 None => from_text(&name.to_string()),
57 }; 57 };
58 58
59 fn from_text(text: &str) -> ast::RecordField { 59 fn from_text(text: &str) -> ast::RecordField {
@@ -67,17 +67,17 @@ pub fn block_expr(
67) -> ast::BlockExpr { 67) -> ast::BlockExpr {
68 let mut text = "{\n".to_string(); 68 let mut text = "{\n".to_string();
69 for stmt in stmts.into_iter() { 69 for stmt in stmts.into_iter() {
70 text += &format!(" {}\n", stmt.syntax()); 70 text += &format!(" {}\n", stmt);
71 } 71 }
72 if let Some(tail_expr) = tail_expr { 72 if let Some(tail_expr) = tail_expr {
73 text += &format!(" {}\n", tail_expr.syntax()) 73 text += &format!(" {}\n", tail_expr)
74 } 74 }
75 text += "}"; 75 text += "}";
76 ast_from_text(&format!("fn f() {}", text)) 76 ast_from_text(&format!("fn f() {}", text))
77} 77}
78 78
79pub fn block_from_expr(e: ast::Expr) -> ast::Block { 79pub fn block_from_expr(e: ast::Expr) -> ast::Block {
80 return from_text(&format!("{{ {} }}", e.syntax())); 80 return from_text(&format!("{{ {} }}", e));
81 81
82 fn from_text(text: &str) -> ast::Block { 82 fn from_text(text: &str) -> ast::Block {
83 ast_from_text(&format!("fn f() {}", text)) 83 ast_from_text(&format!("fn f() {}", text))
@@ -94,7 +94,7 @@ pub fn expr_unimplemented() -> ast::Expr {
94 expr_from_text("unimplemented!()") 94 expr_from_text("unimplemented!()")
95} 95}
96pub fn expr_path(path: ast::Path) -> ast::Expr { 96pub fn expr_path(path: ast::Path) -> ast::Expr {
97 expr_from_text(&path.syntax().to_string()) 97 expr_from_text(&path.to_string())
98} 98}
99pub fn expr_continue() -> ast::Expr { 99pub fn expr_continue() -> ast::Expr {
100 expr_from_text("continue") 100 expr_from_text("continue")
@@ -106,14 +106,14 @@ pub fn expr_return() -> ast::Expr {
106 expr_from_text("return") 106 expr_from_text("return")
107} 107}
108pub fn expr_match(expr: ast::Expr, match_arm_list: ast::MatchArmList) -> ast::Expr { 108pub fn expr_match(expr: ast::Expr, match_arm_list: ast::MatchArmList) -> ast::Expr {
109 expr_from_text(&format!("match {} {}", expr.syntax(), match_arm_list.syntax())) 109 expr_from_text(&format!("match {} {}", expr, match_arm_list))
110} 110}
111pub fn expr_if(condition: ast::Expr, then_branch: ast::BlockExpr) -> ast::Expr { 111pub fn expr_if(condition: ast::Expr, then_branch: ast::BlockExpr) -> ast::Expr {
112 expr_from_text(&format!("if {} {}", condition.syntax(), then_branch.syntax())) 112 expr_from_text(&format!("if {} {}", condition, then_branch))
113} 113}
114pub fn expr_prefix(op: SyntaxKind, expr: ast::Expr) -> ast::Expr { 114pub fn expr_prefix(op: SyntaxKind, expr: ast::Expr) -> ast::Expr {
115 let token = token(op); 115 let token = token(op);
116 expr_from_text(&format!("{}{}", token, expr.syntax())) 116 expr_from_text(&format!("{}{}", token, expr))
117} 117}
118fn expr_from_text(text: &str) -> ast::Expr { 118fn expr_from_text(text: &str) -> ast::Expr {
119 ast_from_text(&format!("const C: () = {};", text)) 119 ast_from_text(&format!("const C: () = {};", text))
@@ -157,8 +157,8 @@ pub fn tuple_struct_pat(
157 path: ast::Path, 157 path: ast::Path,
158 pats: impl IntoIterator<Item = ast::Pat>, 158 pats: impl IntoIterator<Item = ast::Pat>,
159) -> ast::TupleStructPat { 159) -> ast::TupleStructPat {
160 let pats_str = pats.into_iter().map(|p| p.syntax().to_string()).join(", "); 160 let pats_str = pats.into_iter().join(", ");
161 return from_text(&format!("{}({})", path.syntax(), pats_str)); 161 return from_text(&format!("{}({})", path, pats_str));
162 162
163 fn from_text(text: &str) -> ast::TupleStructPat { 163 fn from_text(text: &str) -> ast::TupleStructPat {
164 ast_from_text(&format!("fn f({}: ())", text)) 164 ast_from_text(&format!("fn f({}: ())", text))
@@ -166,8 +166,8 @@ pub fn tuple_struct_pat(
166} 166}
167 167
168pub fn record_pat(path: ast::Path, pats: impl IntoIterator<Item = ast::Pat>) -> ast::RecordPat { 168pub fn record_pat(path: ast::Path, pats: impl IntoIterator<Item = ast::Pat>) -> ast::RecordPat {
169 let pats_str = pats.into_iter().map(|p| p.syntax().to_string()).join(", "); 169 let pats_str = pats.into_iter().join(", ");
170 return from_text(&format!("{} {{ {} }}", path.syntax(), pats_str)); 170 return from_text(&format!("{} {{ {} }}", path, pats_str));
171 171
172 fn from_text(text: &str) -> ast::RecordPat { 172 fn from_text(text: &str) -> ast::RecordPat {
173 ast_from_text(&format!("fn f({}: ())", text)) 173 ast_from_text(&format!("fn f({}: ())", text))
@@ -176,16 +176,15 @@ pub fn record_pat(path: ast::Path, pats: impl IntoIterator<Item = ast::Pat>) ->
176 176
177/// Returns a `BindPat` if the path has just one segment, a `PathPat` otherwise. 177/// Returns a `BindPat` if the path has just one segment, a `PathPat` otherwise.
178pub fn path_pat(path: ast::Path) -> ast::Pat { 178pub fn path_pat(path: ast::Path) -> ast::Pat {
179 let path_str = path.syntax().text().to_string(); 179 return from_text(&path.to_string());
180 return from_text(path_str.as_str());
181 fn from_text(text: &str) -> ast::Pat { 180 fn from_text(text: &str) -> ast::Pat {
182 ast_from_text(&format!("fn f({}: ())", text)) 181 ast_from_text(&format!("fn f({}: ())", text))
183 } 182 }
184} 183}
185 184
186pub fn match_arm(pats: impl IntoIterator<Item = ast::Pat>, expr: ast::Expr) -> ast::MatchArm { 185pub fn match_arm(pats: impl IntoIterator<Item = ast::Pat>, expr: ast::Expr) -> ast::MatchArm {
187 let pats_str = pats.into_iter().map(|p| p.syntax().to_string()).join(" | "); 186 let pats_str = pats.into_iter().join(" | ");
188 return from_text(&format!("{} => {}", pats_str, expr.syntax())); 187 return from_text(&format!("{} => {}", pats_str, expr));
189 188
190 fn from_text(text: &str) -> ast::MatchArm { 189 fn from_text(text: &str) -> ast::MatchArm {
191 ast_from_text(&format!("fn f() {{ match () {{{}}} }}", text)) 190 ast_from_text(&format!("fn f() {{ match () {{{}}} }}", text))
@@ -212,8 +211,8 @@ pub fn where_pred(
212 path: ast::Path, 211 path: ast::Path,
213 bounds: impl IntoIterator<Item = ast::TypeBound>, 212 bounds: impl IntoIterator<Item = ast::TypeBound>,
214) -> ast::WherePred { 213) -> ast::WherePred {
215 let bounds = bounds.into_iter().map(|b| b.syntax().to_string()).join(" + "); 214 let bounds = bounds.into_iter().join(" + ");
216 return from_text(&format!("{}: {}", path.syntax(), bounds)); 215 return from_text(&format!("{}: {}", path, bounds));
217 216
218 fn from_text(text: &str) -> ast::WherePred { 217 fn from_text(text: &str) -> ast::WherePred {
219 ast_from_text(&format!("fn f() where {} {{ }}", text)) 218 ast_from_text(&format!("fn f() where {} {{ }}", text))
@@ -221,7 +220,7 @@ pub fn where_pred(
221} 220}
222 221
223pub fn where_clause(preds: impl IntoIterator<Item = ast::WherePred>) -> ast::WhereClause { 222pub fn where_clause(preds: impl IntoIterator<Item = ast::WherePred>) -> ast::WhereClause {
224 let preds = preds.into_iter().map(|p| p.syntax().to_string()).join(", "); 223 let preds = preds.into_iter().join(", ");
225 return from_text(preds.as_str()); 224 return from_text(preds.as_str());
226 225
227 fn from_text(text: &str) -> ast::WhereClause { 226 fn from_text(text: &str) -> ast::WhereClause {
@@ -231,13 +230,13 @@ pub fn where_clause(preds: impl IntoIterator<Item = ast::WherePred>) -> ast::Whe
231 230
232pub fn let_stmt(pattern: ast::Pat, initializer: Option<ast::Expr>) -> ast::LetStmt { 231pub fn let_stmt(pattern: ast::Pat, initializer: Option<ast::Expr>) -> ast::LetStmt {
233 let text = match initializer { 232 let text = match initializer {
234 Some(it) => format!("let {} = {};", pattern.syntax(), it.syntax()), 233 Some(it) => format!("let {} = {};", pattern, it),
235 None => format!("let {};", pattern.syntax()), 234 None => format!("let {};", pattern),
236 }; 235 };
237 ast_from_text(&format!("fn f() {{ {} }}", text)) 236 ast_from_text(&format!("fn f() {{ {} }}", text))
238} 237}
239pub fn expr_stmt(expr: ast::Expr) -> ast::ExprStmt { 238pub fn expr_stmt(expr: ast::Expr) -> ast::ExprStmt {
240 ast_from_text(&format!("fn f() {{ {}; }}", expr.syntax())) 239 ast_from_text(&format!("fn f() {{ {}; }}", expr))
241} 240}
242 241
243pub fn token(kind: SyntaxKind) -> SyntaxToken { 242pub fn token(kind: SyntaxKind) -> SyntaxToken {