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