aboutsummaryrefslogtreecommitdiff
path: root/crates/syntax
diff options
context:
space:
mode:
authorLukas Wirth <[email protected]>2020-11-02 20:40:52 +0000
committerLukas Wirth <[email protected]>2020-11-02 20:40:52 +0000
commitcd349dbbc4a39342fd54e46fc9d70e3e649a2fda (patch)
treef18205102dbb27ce5cab67340942b9543104afcc /crates/syntax
parent245e1b533b5be5ea4a917957fb02d7f57e6b4661 (diff)
Make insert_use return a SyntaxRewriter
Diffstat (limited to 'crates/syntax')
-rw-r--r--crates/syntax/src/ast/make.rs37
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
354pub fn visibility_pub() -> ast::Visibility {
355 ast_from_text("pub struct S")
356}
357
358pub 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
363pub 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
354pub fn fn_( 371pub 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
393pub 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
376fn ast_from_text<N: AstNode>(text: &str) -> N { 413fn 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) {