|
1 | 1 | //! This module contains free-standing functions for creating AST fragments out |
2 | 2 | //! of smaller pieces. |
3 | 3 | use itertools::Itertools; |
| 4 | +use stdx::format_to; |
4 | 5 |
|
5 | 6 | use crate::{ast, AstNode, SourceFile, SyntaxKind, SyntaxNode, SyntaxToken}; |
6 | 7 |
|
@@ -34,14 +35,14 @@ pub fn use_tree( |
34 | 35 | let mut buf = "use ".to_string(); |
35 | 36 | buf += &path.syntax().to_string(); |
36 | 37 | if let Some(use_tree_list) = use_tree_list { |
37 | | - buf += &format!("::{}", use_tree_list); |
| 38 | + format_to!(buf, "::{}", use_tree_list); |
38 | 39 | } |
39 | 40 | if add_star { |
40 | 41 | buf += "::*"; |
41 | 42 | } |
42 | 43 |
|
43 | 44 | if let Some(alias) = alias { |
44 | | - buf += &format!(" {}", alias); |
| 45 | + format_to!(buf, " {}", alias); |
45 | 46 | } |
46 | 47 | ast_from_text(&buf) |
47 | 48 | } |
@@ -70,15 +71,15 @@ pub fn block_expr( |
70 | 71 | stmts: impl IntoIterator<Item = ast::Stmt>, |
71 | 72 | tail_expr: Option<ast::Expr>, |
72 | 73 | ) -> ast::BlockExpr { |
73 | | - let mut text = "{\n".to_string(); |
| 74 | + let mut buf = "{\n".to_string(); |
74 | 75 | for stmt in stmts.into_iter() { |
75 | | - text += &format!(" {}\n", stmt); |
| 76 | + format_to!(buf, " {}\n", stmt); |
76 | 77 | } |
77 | 78 | if let Some(tail_expr) = tail_expr { |
78 | | - text += &format!(" {}\n", tail_expr) |
| 79 | + format_to!(buf, " {}\n", tail_expr) |
79 | 80 | } |
80 | | - text += "}"; |
81 | | - ast_from_text(&format!("fn f() {}", text)) |
| 81 | + buf += "}"; |
| 82 | + ast_from_text(&format!("fn f() {}", buf)) |
82 | 83 | } |
83 | 84 |
|
84 | 85 | pub fn block_from_expr(e: ast::Expr) -> ast::Block { |
|
0 commit comments