From cd349dbbc4a39342fd54e46fc9d70e3e649a2fda Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Mon, 2 Nov 2020 21:40:52 +0100 Subject: Make insert_use return a SyntaxRewriter --- crates/syntax/src/ast/make.rs | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) (limited to 'crates/syntax/src') 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 { ast_from_text("pub(crate) struct S") } +pub fn visibility_pub() -> ast::Visibility { + ast_from_text("pub struct S") +} + +pub fn tuple_field_list(fields: impl IntoIterator) -> ast::TupleFieldList { + let fields = fields.into_iter().join(", "); + ast_from_text(&format!("struct f({});", fields)) +} + +pub fn tuple_field(visibility: Option, ty: ast::Type) -> ast::TupleField { + let visibility = match visibility { + None => String::new(), + Some(it) => format!("{} ", it), + }; + ast_from_text(&format!("struct f({}{});", visibility, ty)) +} + pub fn fn_( visibility: Option, fn_name: ast::Name, @@ -373,6 +390,26 @@ pub fn fn_( )) } +pub fn struct_( + visibility: Option, + strukt_name: ast::Name, + type_params: Option, + field_list: ast::FieldList, +) -> ast::Struct { + let semicolon = if matches!(field_list, ast::FieldList::TupleFieldList(_)) { ";" } else { "" }; + let type_params = + if let Some(type_params) = type_params { format!("<{}>", type_params) } else { "".into() }; + let visibility = match visibility { + None => String::new(), + Some(it) => format!("{} ", it), + }; + + ast_from_text(&format!( + "{}struct {}{}{}{}", + visibility, strukt_name, type_params, field_list, semicolon + )) +} + fn ast_from_text(text: &str) -> N { let parse = SourceFile::parse(text); let node = match parse.tree().syntax().descendants().find_map(N::cast) { -- cgit v1.2.3