Skip to content

Commit d953df3

Browse files
committed
Simplify
1 parent 0a8e65c commit d953df3

File tree

1 file changed

+28
-29
lines changed

1 file changed

+28
-29
lines changed

crates/ra_syntax/src/ast/make.rs

+28-29
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ pub fn name_ref(text: &str) -> ast::NameRef {
1313
}
1414

1515
pub 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))
1717
}
1818
pub fn path_unqualified(segment: ast::PathSegment) -> ast::Path {
19-
path_from_text(&format!("use {}", segment.syntax()))
19+
path_from_text(&format!("use {}", segment))
2020
}
2121
pub 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))
2323
}
2424
fn path_from_text(text: &str) -> ast::Path {
2525
ast_from_text(text)
@@ -33,10 +33,10 @@ pub fn use_tree(
3333
let mut buf = "use ".to_string();
3434
buf += &path.syntax().to_string();
3535
if let Some(use_tree_list) = use_tree_list {
36-
buf += &format!("::{}", use_tree_list.syntax());
36+
buf += &format!("::{}", use_tree_list);
3737
}
3838
if let Some(alias) = alias {
39-
buf += &format!(" {}", alias.syntax());
39+
buf += &format!(" {}", alias);
4040
}
4141
ast_from_text(&buf)
4242
}
@@ -47,13 +47,13 @@ pub fn use_tree_list(use_trees: impl IntoIterator<Item = ast::UseTree>) -> ast::
4747
}
4848

4949
pub 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))
5151
}
5252

5353
pub fn record_field(name: ast::NameRef, expr: Option<ast::Expr>) -> ast::RecordField {
5454
return match expr {
55-
Some(expr) => from_text(&format!("{}: {}", name.syntax(), expr.syntax())),
56-
None => from_text(&name.syntax().to_string()),
55+
Some(expr) => from_text(&format!("{}: {}", name, expr)),
56+
None => from_text(&name.to_string()),
5757
};
5858

5959
fn from_text(text: &str) -> ast::RecordField {
@@ -67,17 +67,17 @@ pub fn block_expr(
6767
) -> ast::BlockExpr {
6868
let mut text = "{\n".to_string();
6969
for stmt in stmts.into_iter() {
70-
text += &format!(" {}\n", stmt.syntax());
70+
text += &format!(" {}\n", stmt);
7171
}
7272
if let Some(tail_expr) = tail_expr {
73-
text += &format!(" {}\n", tail_expr.syntax())
73+
text += &format!(" {}\n", tail_expr)
7474
}
7575
text += "}";
7676
ast_from_text(&format!("fn f() {}", text))
7777
}
7878

7979
pub fn block_from_expr(e: ast::Expr) -> ast::Block {
80-
return from_text(&format!("{{ {} }}", e.syntax()));
80+
return from_text(&format!("{{ {} }}", e));
8181

8282
fn from_text(text: &str) -> ast::Block {
8383
ast_from_text(&format!("fn f() {}", text))
@@ -94,7 +94,7 @@ pub fn expr_unimplemented() -> ast::Expr {
9494
expr_from_text("unimplemented!()")
9595
}
9696
pub fn expr_path(path: ast::Path) -> ast::Expr {
97-
expr_from_text(&path.syntax().to_string())
97+
expr_from_text(&path.to_string())
9898
}
9999
pub fn expr_continue() -> ast::Expr {
100100
expr_from_text("continue")
@@ -106,14 +106,14 @@ pub fn expr_return() -> ast::Expr {
106106
expr_from_text("return")
107107
}
108108
pub 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))
110110
}
111111
pub 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))
113113
}
114114
pub fn expr_prefix(op: SyntaxKind, expr: ast::Expr) -> ast::Expr {
115115
let token = token(op);
116-
expr_from_text(&format!("{}{}", token, expr.syntax()))
116+
expr_from_text(&format!("{}{}", token, expr))
117117
}
118118
fn expr_from_text(text: &str) -> ast::Expr {
119119
ast_from_text(&format!("const C: () = {};", text))
@@ -157,17 +157,17 @@ pub fn tuple_struct_pat(
157157
path: ast::Path,
158158
pats: impl IntoIterator<Item = ast::Pat>,
159159
) -> ast::TupleStructPat {
160-
let pats_str = pats.into_iter().map(|p| p.syntax().to_string()).join(", ");
161-
return from_text(&format!("{}({})", path.syntax(), pats_str));
160+
let pats_str = pats.into_iter().join(", ");
161+
return from_text(&format!("{}({})", path, pats_str));
162162

163163
fn from_text(text: &str) -> ast::TupleStructPat {
164164
ast_from_text(&format!("fn f({}: ())", text))
165165
}
166166
}
167167

168168
pub 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(", ");
170-
return from_text(&format!("{} {{ {} }}", path.syntax(), pats_str));
169+
let pats_str = pats.into_iter().join(", ");
170+
return from_text(&format!("{} {{ {} }}", path, pats_str));
171171

172172
fn from_text(text: &str) -> ast::RecordPat {
173173
ast_from_text(&format!("fn f({}: ())", text))
@@ -176,16 +176,15 @@ pub fn record_pat(path: ast::Path, pats: impl IntoIterator<Item = ast::Pat>) ->
176176

177177
/// Returns a `BindPat` if the path has just one segment, a `PathPat` otherwise.
178178
pub fn path_pat(path: ast::Path) -> ast::Pat {
179-
let path_str = path.syntax().text().to_string();
180-
return from_text(path_str.as_str());
179+
return from_text(&path.to_string());
181180
fn from_text(text: &str) -> ast::Pat {
182181
ast_from_text(&format!("fn f({}: ())", text))
183182
}
184183
}
185184

186185
pub 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(" | ");
188-
return from_text(&format!("{} => {}", pats_str, expr.syntax()));
186+
let pats_str = pats.into_iter().join(" | ");
187+
return from_text(&format!("{} => {}", pats_str, expr));
189188

190189
fn from_text(text: &str) -> ast::MatchArm {
191190
ast_from_text(&format!("fn f() {{ match () {{{}}} }}", text))
@@ -212,16 +211,16 @@ pub fn where_pred(
212211
path: ast::Path,
213212
bounds: impl IntoIterator<Item = ast::TypeBound>,
214213
) -> ast::WherePred {
215-
let bounds = bounds.into_iter().map(|b| b.syntax().to_string()).join(" + ");
216-
return from_text(&format!("{}: {}", path.syntax(), bounds));
214+
let bounds = bounds.into_iter().join(" + ");
215+
return from_text(&format!("{}: {}", path, bounds));
217216

218217
fn from_text(text: &str) -> ast::WherePred {
219218
ast_from_text(&format!("fn f() where {} {{ }}", text))
220219
}
221220
}
222221

223222
pub 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(", ");
225224
return from_text(preds.as_str());
226225

227226
fn from_text(text: &str) -> ast::WhereClause {
@@ -231,13 +230,13 @@ pub fn where_clause(preds: impl IntoIterator<Item = ast::WherePred>) -> ast::Whe
231230

232231
pub fn let_stmt(pattern: ast::Pat, initializer: Option<ast::Expr>) -> ast::LetStmt {
233232
let text = match initializer {
234-
Some(it) => format!("let {} = {};", pattern.syntax(), it.syntax()),
235-
None => format!("let {};", pattern.syntax()),
233+
Some(it) => format!("let {} = {};", pattern, it),
234+
None => format!("let {};", pattern),
236235
};
237236
ast_from_text(&format!("fn f() {{ {} }}", text))
238237
}
239238
pub 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))
241240
}
242241

243242
pub fn token(kind: SyntaxKind) -> SyntaxToken {

0 commit comments

Comments
 (0)