Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'crates/syntax/src/ast/syntax_factory/constructors.rs')
| -rw-r--r-- | crates/syntax/src/ast/syntax_factory/constructors.rs | 200 |
1 files changed, 199 insertions, 1 deletions
diff --git a/crates/syntax/src/ast/syntax_factory/constructors.rs b/crates/syntax/src/ast/syntax_factory/constructors.rs index 0f3b3d301c..1070af65e7 100644 --- a/crates/syntax/src/ast/syntax_factory/constructors.rs +++ b/crates/syntax/src/ast/syntax_factory/constructors.rs @@ -2,7 +2,7 @@ use either::Either; use crate::{ - AstNode, NodeOrToken, SyntaxKind, SyntaxNode, SyntaxToken, + AstNode, Edition, NodeOrToken, SyntaxElement, SyntaxKind, SyntaxNode, SyntaxToken, ast::{ self, HasArgList, HasAttrs, HasGenericArgs, HasGenericParams, HasLoopBody, HasName, HasTypeBounds, HasVisibility, Lifetime, Param, RangeItem, make, @@ -33,6 +33,10 @@ impl SyntaxFactory { make::ext::expr_self().clone_for_update() } + pub fn expr_const_value(&self, text: &str) -> ast::ConstArg { + make::expr_const_value(text).clone_for_update() + } + pub fn lifetime(&self, text: &str) -> ast::Lifetime { make::lifetime(text).clone_for_update() } @@ -63,6 +67,14 @@ impl SyntaxFactory { ast } + pub fn ty_path_from_segments( + &self, + segments: impl IntoIterator<Item = ast::PathSegment>, + is_abs: bool, + ) -> ast::Type { + ast::Type::PathType(self.ty_path(self.path_from_segments(segments, is_abs))) + } + pub fn type_bound(&self, bound: ast::Type) -> ast::TypeBound { make::type_bound(bound).clone_for_update() } @@ -131,6 +143,10 @@ impl SyntaxFactory { make::path_from_text(text).clone_for_update() } + pub fn path_from_text_with_edition(&self, text: &str, edition: Edition) -> ast::Path { + make::path_from_text_with_edition(text, edition).clone_for_update() + } + pub fn path_concat(&self, first: ast::Path, second: ast::Path) -> ast::Path { make::path_concat(first, second).clone_for_update() } @@ -491,6 +507,18 @@ impl SyntaxFactory { ast } + pub fn path_segment_self(&self) -> ast::PathSegment { + make::path_segment_self().clone_for_update() + } + + pub fn path_segment_super(&self) -> ast::PathSegment { + make::path_segment_super().clone_for_update() + } + + pub fn path_segment_crate(&self) -> ast::PathSegment { + make::path_segment_crate().clone_for_update() + } + pub fn generic_ty_path_segment( &self, name_ref: ast::NameRef, @@ -551,6 +579,25 @@ impl SyntaxFactory { make::ty_placeholder().clone_for_update() } + pub fn ty_unit(&self) -> ast::Type { + make::ty_unit().clone_for_update() + } + + pub fn ty_tuple(&self, types: impl IntoIterator<Item = ast::Type>) -> ast::Type { + let (types, input) = iterator_input(types); + let ast = make::ty_tuple(types).clone_for_update(); + + if let Some(mut mapping) = self.mappings() + && let ast::Type::TupleType(tuple_ty) = &ast + { + let mut builder = SyntaxMappingBuilder::new(tuple_ty.syntax().clone()); + builder.map_children(input, tuple_ty.fields().map(|ty| ty.syntax().clone())); + builder.finish(&mut mapping); + } + + ast + } + pub fn path_segment_generics( &self, name_ref: ast::NameRef, @@ -628,6 +675,10 @@ impl SyntaxFactory { ast } + pub fn use_tree_glob(&self) -> ast::UseTree { + make::use_tree_glob().clone_for_update() + } + pub fn path_unqualified(&self, segment: ast::PathSegment) -> ast::Path { let ast = make::path_unqualified(segment.clone()).clone_for_update(); @@ -640,6 +691,23 @@ impl SyntaxFactory { ast } + pub fn path_qualified(&self, qual: ast::Path, segment: ast::PathSegment) -> ast::Path { + let ast = make::path_qualified(qual.clone(), segment.clone()).clone_for_update(); + + if let Some(mut mapping) = self.mappings() { + let mut builder = SyntaxMappingBuilder::new(ast.syntax().clone()); + if let Some(out_qual) = ast.qualifier() { + builder.map_node(qual.syntax().clone(), out_qual.syntax().clone()); + } + if let Some(out_segment) = ast.segment() { + builder.map_node(segment.syntax().clone(), out_segment.syntax().clone()); + } + builder.finish(&mut mapping); + } + + ast + } + pub fn path_from_segments( &self, segments: impl IntoIterator<Item = ast::PathSegment>, @@ -676,6 +744,18 @@ impl SyntaxFactory { ast } + pub fn simple_ident_pat(&self, name: ast::Name) -> ast::IdentPat { + let ast = make::ext::simple_ident_pat(name.clone()).clone_for_update(); + + if let Some(mut mapping) = self.mappings() { + let mut builder = SyntaxMappingBuilder::new(ast.syntax().clone()); + builder.map_node(name.syntax().clone(), ast.name().unwrap().syntax().clone()); + builder.finish(&mut mapping); + } + + ast + } + pub fn wildcard_pat(&self) -> ast::WildcardPat { make::wildcard_pat().clone_for_update() } @@ -851,6 +931,10 @@ impl SyntaxFactory { ast } + pub fn deref_pat(&self, pat: ast::Pat) -> ast::Pat { + make::deref_pat(pat.clone()).clone_for_update() + } + pub fn paren_pat(&self, pat: ast::Pat) -> ast::ParenPat { let ast = make::paren_pat(pat.clone()).clone_for_update(); @@ -925,6 +1009,37 @@ impl SyntaxFactory { ast } + pub fn async_move_block_expr( + &self, + statements: impl IntoIterator<Item = ast::Stmt>, + tail_expr: Option<ast::Expr>, + ) -> ast::BlockExpr { + let (statements, mut input) = iterator_input(statements); + + let ast = make::async_move_block_expr(statements, tail_expr.clone()).clone_for_update(); + + if let Some(mut mapping) = self.mappings() { + let stmt_list = ast.stmt_list().unwrap(); + let mut builder = SyntaxMappingBuilder::new(stmt_list.syntax().clone()); + + if let Some(input) = tail_expr { + builder.map_node( + input.syntax().clone(), + stmt_list.tail_expr().unwrap().syntax().clone(), + ); + } else if let Some(ast_tail) = stmt_list.tail_expr() { + let last_stmt = input.pop().unwrap(); + builder.map_node(last_stmt, ast_tail.syntax().clone()); + } + + builder.map_children(input, stmt_list.statements().map(|it| it.syntax().clone())); + + builder.finish(&mut mapping); + } + + ast + } + pub fn expr_empty_block(&self) -> ast::BlockExpr { make::expr_empty_block().clone_for_update() } @@ -1075,6 +1190,27 @@ impl SyntaxFactory { ast.into() } + pub fn expr_reborrow(&self, expr: ast::Expr) -> ast::Expr { + let ast::Expr::RefExpr(ast) = make::expr_reborrow(expr.clone()).clone_for_update() else { + unreachable!() + }; + + if let Some(mut mapping) = self.mappings() { + // Layout: RefExpr(&mut, PrefixExpr(*, expr)). Map `expr` to the + // inner expr inside the synthesized PrefixExpr. + let prefix = match ast.expr() { + Some(ast::Expr::PrefixExpr(p)) => p, + _ => unreachable!("expr_reborrow always produces `&mut *expr`"), + }; + let inner = prefix.expr().unwrap(); + let mut builder = SyntaxMappingBuilder::new(prefix.syntax().clone()); + builder.map_node(expr.syntax().clone(), inner.syntax().clone()); + builder.finish(&mut mapping); + } + + ast.into() + } + pub fn expr_raw_ref(&self, expr: ast::Expr, exclusive: bool) -> ast::Expr { let ast::Expr::RefExpr(ast) = make::expr_raw_ref(expr.clone(), exclusive).clone_for_update() @@ -2113,6 +2249,21 @@ impl SyntaxFactory { make::ext::field_from_idents(parts) } + pub fn ty_name(&self, name: ast::Name) -> ast::Type { + let ast = make::ext::ty_name(name.clone()).clone_for_update(); + + if let Some(mut mapping) = self.mappings() + && let ast::Type::PathType(path_ty) = &ast + && let Some(name_ref) = path_ty.path().and_then(|path| path.segment()?.name_ref()) + { + let mut builder = SyntaxMappingBuilder::new(name_ref.syntax().parent().unwrap()); + builder.map_node(name.syntax().clone(), name_ref.syntax().clone()); + builder.finish(&mut mapping); + } + + ast + } + pub fn expr_await(&self, expr: ast::Expr) -> ast::AwaitExpr { let ast::Expr::AwaitExpr(ast) = make::expr_await(expr.clone()).clone_for_update() else { unreachable!() @@ -2127,6 +2278,53 @@ impl SyntaxFactory { ast } + pub fn expr_try(&self, expr: ast::Expr) -> ast::Expr { + let ast = make::expr_try(expr.clone()).clone_for_update(); + + if let Some(mut mapping) = self.mappings() { + let mut builder = SyntaxMappingBuilder::new(ast.syntax().clone()); + if let ast::Expr::TryExpr(try_expr) = &ast + && let Some(inner) = try_expr.expr() + { + builder.map_node(expr.syntax().clone(), inner.syntax().clone()); + } + builder.finish(&mut mapping); + } + + ast + } + + pub fn hacky_block_expr( + &self, + elements: impl IntoIterator<Item = SyntaxElement>, + tail_expr: Option<ast::Expr>, + ) -> ast::BlockExpr { + let elements = elements.into_iter().collect::<Vec<_>>(); + let ast = + make::hacky_block_expr(elements.iter().cloned(), tail_expr.clone()).clone_for_update(); + + if let Some(mut mapping) = self.mappings() + && let Some(stmt_list) = ast.stmt_list() + { + let mut builder = SyntaxMappingBuilder::new(stmt_list.syntax().clone()); + builder.map_children( + elements.into_iter().filter_map(|node_or_token| match node_or_token { + NodeOrToken::Node(node) => Some(node), + NodeOrToken::Token(_) => None, + }), + stmt_list.syntax().children(), + ); + if let Some(tail_expr) = tail_expr + && let Some(output_tail_expr) = stmt_list.tail_expr() + { + builder.map_node(tail_expr.syntax().clone(), output_tail_expr.syntax().clone()); + } + builder.finish(&mut mapping); + } + + ast + } + pub fn expr_break(&self, label: Option<Lifetime>, expr: Option<ast::Expr>) -> ast::BreakExpr { let ast::Expr::BreakExpr(ast) = make::expr_break(label.clone(), expr.clone()).clone_for_update() |