aboutsummaryrefslogtreecommitdiff
path: root/crates/syntax/src/ast/make.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/syntax/src/ast/make.rs')
-rw-r--r--crates/syntax/src/ast/make.rs64
1 files changed, 62 insertions, 2 deletions
diff --git a/crates/syntax/src/ast/make.rs b/crates/syntax/src/ast/make.rs
index 5b06cb767..b1578820f 100644
--- a/crates/syntax/src/ast/make.rs
+++ b/crates/syntax/src/ast/make.rs
@@ -110,8 +110,16 @@ pub fn record_expr_field(name: ast::NameRef, expr: Option<ast::Expr>) -> ast::Re
110 } 110 }
111} 111}
112 112
113pub fn record_field(name: ast::NameRef, ty: ast::Type) -> ast::RecordField { 113pub fn record_field(
114 ast_from_text(&format!("struct S {{ {}: {}, }}", name, ty)) 114 visibility: Option<ast::Visibility>,
115 name: ast::Name,
116 ty: ast::Type,
117) -> ast::RecordField {
118 let visibility = match visibility {
119 None => String::new(),
120 Some(it) => format!("{} ", it),
121 };
122 ast_from_text(&format!("struct S {{ {}{}: {}, }}", visibility, name, ty))
115} 123}
116 124
117pub fn block_expr( 125pub fn block_expr(
@@ -351,6 +359,38 @@ pub fn visibility_pub_crate() -> ast::Visibility {
351 ast_from_text("pub(crate) struct S") 359 ast_from_text("pub(crate) struct S")
352} 360}
353 361
362pub fn visibility_pub() -> ast::Visibility {
363 ast_from_text("pub struct S")
364}
365
366pub fn tuple_field_list(fields: impl IntoIterator<Item = ast::TupleField>) -> ast::TupleFieldList {
367 let fields = fields.into_iter().join(", ");
368 ast_from_text(&format!("struct f({});", fields))
369}
370
371pub fn record_field_list(
372 fields: impl IntoIterator<Item = ast::RecordField>,
373) -> ast::RecordFieldList {
374 let fields = fields.into_iter().join(", ");
375 ast_from_text(&format!("struct f {{ {} }}", fields))
376}
377
378pub fn tuple_field(visibility: Option<ast::Visibility>, ty: ast::Type) -> ast::TupleField {
379 let visibility = match visibility {
380 None => String::new(),
381 Some(it) => format!("{} ", it),
382 };
383 ast_from_text(&format!("struct f({}{});", visibility, ty))
384}
385
386pub fn variant(name: ast::Name, field_list: Option<ast::FieldList>) -> ast::Variant {
387 let field_list = match field_list {
388 None => String::new(),
389 Some(it) => format!("{}", it),
390 };
391 ast_from_text(&format!("enum f {{ {}{} }}", name, field_list))
392}
393
354pub fn fn_( 394pub fn fn_(
355 visibility: Option<ast::Visibility>, 395 visibility: Option<ast::Visibility>,
356 fn_name: ast::Name, 396 fn_name: ast::Name,
@@ -373,6 +413,26 @@ pub fn fn_(
373 )) 413 ))
374} 414}
375 415
416pub fn struct_(
417 visibility: Option<ast::Visibility>,
418 strukt_name: ast::Name,
419 type_params: Option<ast::GenericParamList>,
420 field_list: ast::FieldList,
421) -> ast::Struct {
422 let semicolon = if matches!(field_list, ast::FieldList::TupleFieldList(_)) { ";" } else { "" };
423 let type_params =
424 if let Some(type_params) = type_params { format!("<{}>", type_params) } else { "".into() };
425 let visibility = match visibility {
426 None => String::new(),
427 Some(it) => format!("{} ", it),
428 };
429
430 ast_from_text(&format!(
431 "{}struct {}{}{}{}",
432 visibility, strukt_name, type_params, field_list, semicolon
433 ))
434}
435
376fn ast_from_text<N: AstNode>(text: &str) -> N { 436fn ast_from_text<N: AstNode>(text: &str) -> N {
377 let parse = SourceFile::parse(text); 437 let parse = SourceFile::parse(text);
378 let node = match parse.tree().syntax().descendants().find_map(N::cast) { 438 let node = match parse.tree().syntax().descendants().find_map(N::cast) {