diff options
Diffstat (limited to 'crates/ra_syntax/src/ast/make.rs')
-rw-r--r-- | crates/ra_syntax/src/ast/make.rs | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/crates/ra_syntax/src/ast/make.rs b/crates/ra_syntax/src/ast/make.rs index 0c908573d..dbb9c8a9b 100644 --- a/crates/ra_syntax/src/ast/make.rs +++ b/crates/ra_syntax/src/ast/make.rs | |||
@@ -269,6 +269,31 @@ pub fn unreachable_macro_call() -> ast::MacroCall { | |||
269 | ast_from_text(&format!("unreachable!()")) | 269 | ast_from_text(&format!("unreachable!()")) |
270 | } | 270 | } |
271 | 271 | ||
272 | pub fn param(name: String, ty: String) -> ast::Param { | ||
273 | ast_from_text(&format!("fn f({}: {}) {{ }}", name, ty)) | ||
274 | } | ||
275 | |||
276 | pub fn param_list(pats: impl IntoIterator<Item = ast::Param>) -> ast::ParamList { | ||
277 | let args = pats.into_iter().join(", "); | ||
278 | ast_from_text(&format!("fn f({}) {{ }}", args)) | ||
279 | } | ||
280 | |||
281 | pub fn fn_def( | ||
282 | fn_name: ast::Name, | ||
283 | type_params: Option<ast::TypeParamList>, | ||
284 | params: ast::ParamList, | ||
285 | body: ast::BlockExpr, | ||
286 | ) -> ast::FnDef { | ||
287 | let type_params = | ||
288 | if let Some(type_params) = type_params { format!("<{}>", type_params) } else { "".into() }; | ||
289 | ast_from_text(&format!("fn {}{}{} {}", fn_name, type_params, params, body)) | ||
290 | } | ||
291 | |||
292 | pub fn add_newlines(amount_of_newlines: usize, t: impl AstNode) -> ast::SourceFile { | ||
293 | let newlines = "\n".repeat(amount_of_newlines); | ||
294 | ast_from_text(&format!("{}{}", newlines, t.syntax())) | ||
295 | } | ||
296 | |||
272 | fn ast_from_text<N: AstNode>(text: &str) -> N { | 297 | fn ast_from_text<N: AstNode>(text: &str) -> N { |
273 | let parse = SourceFile::parse(text); | 298 | let parse = SourceFile::parse(text); |
274 | let node = parse.tree().syntax().descendants().find_map(N::cast).unwrap(); | 299 | let node = parse.tree().syntax().descendants().find_map(N::cast).unwrap(); |