aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_syntax/src/ast/make.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_syntax/src/ast/make.rs')
-rw-r--r--crates/ra_syntax/src/ast/make.rs28
1 files changed, 27 insertions, 1 deletions
diff --git a/crates/ra_syntax/src/ast/make.rs b/crates/ra_syntax/src/ast/make.rs
index 0c908573d..f39559e9e 100644
--- a/crates/ra_syntax/src/ast/make.rs
+++ b/crates/ra_syntax/src/ast/make.rs
@@ -22,7 +22,8 @@ pub fn path_unqualified(segment: ast::PathSegment) -> ast::Path {
22pub fn path_qualified(qual: ast::Path, segment: ast::PathSegment) -> ast::Path { 22pub fn path_qualified(qual: ast::Path, segment: ast::PathSegment) -> ast::Path {
23 path_from_text(&format!("{}::{}", qual, segment)) 23 path_from_text(&format!("{}::{}", qual, segment))
24} 24}
25fn path_from_text(text: &str) -> ast::Path { 25
26pub fn path_from_text(text: &str) -> ast::Path {
26 ast_from_text(text) 27 ast_from_text(text)
27} 28}
28 29
@@ -269,6 +270,31 @@ pub fn unreachable_macro_call() -> ast::MacroCall {
269 ast_from_text(&format!("unreachable!()")) 270 ast_from_text(&format!("unreachable!()"))
270} 271}
271 272
273pub fn param(name: String, ty: String) -> ast::Param {
274 ast_from_text(&format!("fn f({}: {}) {{ }}", name, ty))
275}
276
277pub 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
282pub 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
293pub 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
272fn ast_from_text<N: AstNode>(text: &str) -> N { 298fn ast_from_text<N: AstNode>(text: &str) -> N {
273 let parse = SourceFile::parse(text); 299 let parse = SourceFile::parse(text);
274 let node = parse.tree().syntax().descendants().find_map(N::cast).unwrap(); 300 let node = parse.tree().syntax().descendants().find_map(N::cast).unwrap();