diff options
Diffstat (limited to 'crates/syntax')
-rw-r--r-- | crates/syntax/src/ast/make.rs | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/crates/syntax/src/ast/make.rs b/crates/syntax/src/ast/make.rs index 5b06cb767..2cf436e7a 100644 --- a/crates/syntax/src/ast/make.rs +++ b/crates/syntax/src/ast/make.rs | |||
@@ -351,6 +351,23 @@ pub fn visibility_pub_crate() -> ast::Visibility { | |||
351 | ast_from_text("pub(crate) struct S") | 351 | ast_from_text("pub(crate) struct S") |
352 | } | 352 | } |
353 | 353 | ||
354 | pub fn visibility_pub() -> ast::Visibility { | ||
355 | ast_from_text("pub struct S") | ||
356 | } | ||
357 | |||
358 | pub fn tuple_field_list(fields: impl IntoIterator<Item = ast::TupleField>) -> ast::TupleFieldList { | ||
359 | let fields = fields.into_iter().join(", "); | ||
360 | ast_from_text(&format!("struct f({});", fields)) | ||
361 | } | ||
362 | |||
363 | pub fn tuple_field(visibility: Option<ast::Visibility>, ty: ast::Type) -> ast::TupleField { | ||
364 | let visibility = match visibility { | ||
365 | None => String::new(), | ||
366 | Some(it) => format!("{} ", it), | ||
367 | }; | ||
368 | ast_from_text(&format!("struct f({}{});", visibility, ty)) | ||
369 | } | ||
370 | |||
354 | pub fn fn_( | 371 | pub fn fn_( |
355 | visibility: Option<ast::Visibility>, | 372 | visibility: Option<ast::Visibility>, |
356 | fn_name: ast::Name, | 373 | fn_name: ast::Name, |
@@ -373,6 +390,26 @@ pub fn fn_( | |||
373 | )) | 390 | )) |
374 | } | 391 | } |
375 | 392 | ||
393 | pub fn struct_( | ||
394 | visibility: Option<ast::Visibility>, | ||
395 | strukt_name: ast::Name, | ||
396 | type_params: Option<ast::GenericParamList>, | ||
397 | field_list: ast::FieldList, | ||
398 | ) -> ast::Struct { | ||
399 | let semicolon = if matches!(field_list, ast::FieldList::TupleFieldList(_)) { ";" } else { "" }; | ||
400 | let type_params = | ||
401 | if let Some(type_params) = type_params { format!("<{}>", type_params) } else { "".into() }; | ||
402 | let visibility = match visibility { | ||
403 | None => String::new(), | ||
404 | Some(it) => format!("{} ", it), | ||
405 | }; | ||
406 | |||
407 | ast_from_text(&format!( | ||
408 | "{}struct {}{}{}{}", | ||
409 | visibility, strukt_name, type_params, field_list, semicolon | ||
410 | )) | ||
411 | } | ||
412 | |||
376 | fn ast_from_text<N: AstNode>(text: &str) -> N { | 413 | fn ast_from_text<N: AstNode>(text: &str) -> N { |
377 | let parse = SourceFile::parse(text); | 414 | let parse = SourceFile::parse(text); |
378 | let node = match parse.tree().syntax().descendants().find_map(N::cast) { | 415 | let node = match parse.tree().syntax().descendants().find_map(N::cast) { |