diff options
author | Benjamin Coenen <[email protected]> | 2020-10-04 20:21:30 +0100 |
---|---|---|
committer | Benjamin Coenen <[email protected]> | 2020-10-08 19:57:18 +0100 |
commit | 3bfa3e8123dd2d652019ad270622025d10b87cc8 (patch) | |
tree | 45c5761137519e63ea26fb097924394e8e7b268b /crates/syntax | |
parent | 636b413e142e2b831ded74642c8193a0dc39b4a7 (diff) |
when generating new function, focus on return type instead of body
Signed-off-by: Benjamin Coenen <[email protected]>
Diffstat (limited to 'crates/syntax')
-rw-r--r-- | crates/syntax/src/ast/make.rs | 12 |
1 files changed, 11 insertions, 1 deletions
diff --git a/crates/syntax/src/ast/make.rs b/crates/syntax/src/ast/make.rs index 3a184094c..74dbdfaf7 100644 --- a/crates/syntax/src/ast/make.rs +++ b/crates/syntax/src/ast/make.rs | |||
@@ -320,6 +320,10 @@ pub fn param(name: String, ty: String) -> ast::Param { | |||
320 | ast_from_text(&format!("fn f({}: {}) {{ }}", name, ty)) | 320 | ast_from_text(&format!("fn f({}: {}) {{ }}", name, ty)) |
321 | } | 321 | } |
322 | 322 | ||
323 | pub fn ret_type(ty: ast::Type) -> ast::RetType { | ||
324 | ast_from_text(&format!("fn f() -> {} {{ }}", ty)) | ||
325 | } | ||
326 | |||
323 | pub fn param_list(pats: impl IntoIterator<Item = ast::Param>) -> ast::ParamList { | 327 | pub fn param_list(pats: impl IntoIterator<Item = ast::Param>) -> ast::ParamList { |
324 | let args = pats.into_iter().join(", "); | 328 | let args = pats.into_iter().join(", "); |
325 | ast_from_text(&format!("fn f({}) {{ }}", args)) | 329 | ast_from_text(&format!("fn f({}) {{ }}", args)) |
@@ -350,14 +354,20 @@ pub fn fn_( | |||
350 | type_params: Option<ast::GenericParamList>, | 354 | type_params: Option<ast::GenericParamList>, |
351 | params: ast::ParamList, | 355 | params: ast::ParamList, |
352 | body: ast::BlockExpr, | 356 | body: ast::BlockExpr, |
357 | ret_type: Option<ast::RetType>, | ||
353 | ) -> ast::Fn { | 358 | ) -> ast::Fn { |
354 | let type_params = | 359 | let type_params = |
355 | if let Some(type_params) = type_params { format!("<{}>", type_params) } else { "".into() }; | 360 | if let Some(type_params) = type_params { format!("<{}>", type_params) } else { "".into() }; |
361 | let ret_type = if let Some(ret_type) = ret_type { format!("{} ", ret_type) } else { "".into() }; | ||
356 | let visibility = match visibility { | 362 | let visibility = match visibility { |
357 | None => String::new(), | 363 | None => String::new(), |
358 | Some(it) => format!("{} ", it), | 364 | Some(it) => format!("{} ", it), |
359 | }; | 365 | }; |
360 | ast_from_text(&format!("{}fn {}{}{} {}", visibility, fn_name, type_params, params, body)) | 366 | |
367 | ast_from_text(&format!( | ||
368 | "{}fn {}{}{} {}{}", | ||
369 | visibility, fn_name, type_params, params, ret_type, body | ||
370 | )) | ||
361 | } | 371 | } |
362 | 372 | ||
363 | fn ast_from_text<N: AstNode>(text: &str) -> N { | 373 | fn ast_from_text<N: AstNode>(text: &str) -> N { |