From 10667753c71242c75e70e7b8c46486f37685c186 Mon Sep 17 00:00:00 2001 From: Timo Freiberg Date: Tue, 31 Mar 2020 23:02:48 +0200 Subject: Use ast::make API in add_function assist --- crates/ra_syntax/src/ast/make.rs | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'crates/ra_syntax/src/ast') 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 { ast_from_text(&format!("unreachable!()")) } +pub fn param(name: String, ty: String) -> ast::Param { + ast_from_text(&format!("fn f({}: {}) {{ }}", name, ty)) +} + +pub fn param_list(pats: impl IntoIterator) -> ast::ParamList { + let args = pats.into_iter().join(", "); + ast_from_text(&format!("fn f({}) {{ }}", args)) +} + +pub fn fn_def( + fn_name: ast::Name, + type_params: Option, + params: ast::ParamList, + body: ast::BlockExpr, +) -> ast::FnDef { + let type_params = + if let Some(type_params) = type_params { format!("<{}>", type_params) } else { "".into() }; + ast_from_text(&format!("fn {}{}{} {}", fn_name, type_params, params, body)) +} + +pub fn add_newlines(amount_of_newlines: usize, t: impl AstNode) -> ast::SourceFile { + let newlines = "\n".repeat(amount_of_newlines); + ast_from_text(&format!("{}{}", newlines, t.syntax())) +} + fn ast_from_text(text: &str) -> N { let parse = SourceFile::parse(text); let node = parse.tree().syntax().descendants().find_map(N::cast).unwrap(); -- cgit v1.2.3