aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_syntax/src/ast/make.rs
diff options
context:
space:
mode:
authorTimo Freiberg <[email protected]>2020-03-31 22:02:48 +0100
committerTimo Freiberg <[email protected]>2020-04-01 22:06:14 +0100
commit10667753c71242c75e70e7b8c46486f37685c186 (patch)
treee32cd2b6965f9f2ed6cda7f84b426bb8de435b35 /crates/ra_syntax/src/ast/make.rs
parente5fc42cbc114bcfa5a4fac48a16b70fce8d438ae (diff)
Use ast::make API in add_function assist
Diffstat (limited to 'crates/ra_syntax/src/ast/make.rs')
-rw-r--r--crates/ra_syntax/src/ast/make.rs25
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
272pub fn param(name: String, ty: String) -> ast::Param {
273 ast_from_text(&format!("fn f({}: {}) {{ }}", name, ty))
274}
275
276pub 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
281pub 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
292pub 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
272fn ast_from_text<N: AstNode>(text: &str) -> N { 297fn 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();