//! Wrappers over [`make`] constructors
use either::Either;

use crate::{
    AstNode, Edition, NodeOrToken, SyntaxElement, SyntaxKind, SyntaxNode, SyntaxToken,
    ast::{
        self, HasArgList, HasAttrs, HasGenericArgs, HasGenericParams, HasLoopBody, HasModuleItem,
        HasName, HasTypeBounds, HasVisibility, Lifetime, Param, RangeItem, make,
    },
    syntax_editor::SyntaxMappingBuilder,
};

use super::SyntaxFactory;

impl SyntaxFactory {
    pub fn name(&self, name: &str) -> ast::Name {
        make::name(name)
    }

    pub fn name_ref(&self, name: &str) -> ast::NameRef {
        make::name_ref(name)
    }

    pub fn name_ref_self_ty(&self) -> ast::NameRef {
        make::name_ref_self_ty()
    }

    pub fn expr_todo(&self) -> ast::Expr {
        make::ext::expr_todo()
    }

    pub fn expr_self(&self) -> ast::Expr {
        make::ext::expr_self()
    }

    pub fn expr_const_value(&self, text: &str) -> ast::ConstArg {
        make::expr_const_value(text)
    }

    pub fn lifetime(&self, text: &str) -> ast::Lifetime {
        make::lifetime(text)
    }

    pub fn ty(&self, text: &str) -> ast::Type {
        make::ty(text)
    }

    pub fn ty_infer(&self) -> ast::InferType {
        let ast::Type::InferType(ast) = make::ty_placeholder() else { unreachable!() };

        ast
    }

    pub fn ty_path(&self, path: ast::Path) -> ast::PathType {
        let ast::Type::PathType(ast) = make::ty_path(path.clone()) else { unreachable!() };

        if let Some(mut mapping) = self.mappings() {
            let mut builder = SyntaxMappingBuilder::new(ast.syntax().clone());
            builder.map_node(path.syntax().clone(), ast.path().unwrap().syntax().clone());
            builder.finish(&mut mapping);
        }

        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)
    }

    pub fn type_bound_text(&self, bound: &str) -> ast::TypeBound {
        make::type_bound_text(bound)
    }

    pub fn use_tree_list(
        &self,
        use_trees: impl IntoIterator<Item = ast::UseTree>,
    ) -> ast::UseTreeList {
        let (use_trees, input) = iterator_input(use_trees);
        let ast = make::use_tree_list(use_trees);

        if let Some(mut mapping) = self.mappings() {
            let mut builder = SyntaxMappingBuilder::new(ast.syntax().clone());
            builder.map_children(input, ast.use_trees().map(|b| b.syntax().clone()));
            builder.finish(&mut mapping);
        }

        ast
    }

    pub fn type_bound_list(
        &self,
        bounds: impl IntoIterator<Item = ast::TypeBound>,
    ) -> Option<ast::TypeBoundList> {
        let (bounds, input) = iterator_input(bounds);
        let ast = make::type_bound_list(bounds)?;

        if let Some(mut mapping) = self.mappings() {
            let mut builder = SyntaxMappingBuilder::new(ast.syntax().clone());
            for (input_node, output_bound) in input.into_iter().zip(ast.bounds()) {
                builder.map_node(input_node, output_bound.syntax().clone());
            }
            builder.finish(&mut mapping);
        }

        Some(ast)
    }

    pub fn type_param(
        &self,
        name: ast::Name,
        bounds: Option<ast::TypeBoundList>,
    ) -> ast::TypeParam {
        let ast = make::type_param(name.clone(), bounds.clone());

        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());
            if let Some(input) = bounds {
                builder.map_node(
                    input.syntax().clone(),
                    ast.type_bound_list().unwrap().syntax().clone(),
                );
            }
            builder.finish(&mut mapping);
        }

        ast
    }

    pub fn path_from_text(&self, text: &str) -> ast::Path {
        make::path_from_text(text)
    }

    pub fn path_from_text_with_edition(&self, text: &str, edition: Edition) -> ast::Path {
        make::path_from_text_with_edition(text, edition)
    }

    pub fn path_concat(&self, first: ast::Path, second: ast::Path) -> ast::Path {
        make::path_concat(first, second)
    }

    pub fn visibility_pub_crate(&self) -> ast::Visibility {
        make::visibility_pub_crate()
    }

    pub fn visibility_pub(&self) -> ast::Visibility {
        make::visibility_pub()
    }

    pub fn struct_(
        &self,
        visibility: Option<ast::Visibility>,
        strukt_name: ast::Name,
        generic_param_list: Option<ast::GenericParamList>,
        field_list: ast::FieldList,
    ) -> ast::Struct {
        let ast = make::struct_(
            visibility.clone(),
            strukt_name.clone(),
            generic_param_list.clone(),
            field_list.clone(),
        );

        if let Some(mut mapping) = self.mappings() {
            let mut builder = SyntaxMappingBuilder::new(ast.syntax().clone());
            if let Some(visibility) = visibility {
                builder.map_node(
                    visibility.syntax().clone(),
                    ast.visibility().unwrap().syntax().clone(),
                );
            }
            builder.map_node(strukt_name.syntax().clone(), ast.name().unwrap().syntax().clone());
            if let Some(generic_param_list) = generic_param_list {
                builder.map_node(
                    generic_param_list.syntax().clone(),
                    ast.generic_param_list().unwrap().syntax().clone(),
                );
            }
            builder
                .map_node(field_list.syntax().clone(), ast.field_list().unwrap().syntax().clone());
            builder.finish(&mut mapping);
        }

        ast
    }

    pub fn unnamed_param(&self, ty: ast::Type) -> ast::Param {
        let ast = make::unnamed_param(ty.clone());

        if let Some(mut mapping) = self.mappings() {
            let mut builder = SyntaxMappingBuilder::new(ast.syntax().clone());
            builder.map_node(ty.syntax().clone(), ast.ty().unwrap().syntax().clone());
            builder.finish(&mut mapping);
        }

        ast
    }

    pub fn untyped_param(&self, pat: ast::Pat) -> ast::Param {
        let ast = make::untyped_param(pat.clone());

        if let Some(mut mapping) = self.mappings() {
            let mut builder = SyntaxMappingBuilder::new(ast.syntax().clone());
            builder.map_node(pat.syntax().clone(), ast.pat().unwrap().syntax().clone());
            builder.finish(&mut mapping);
        }

        ast
    }

    pub fn ty_fn_ptr<I: Iterator<Item = Param>>(
        &self,
        is_unsafe: bool,
        abi: Option<ast::Abi>,
        params: I,
        ret_type: Option<ast::RetType>,
    ) -> ast::FnPtrType {
        let (params, params_input) = iterator_input(params);
        let ast = make::ty_fn_ptr(is_unsafe, abi.clone(), params.into_iter(), ret_type.clone());

        if let Some(mut mapping) = self.mappings() {
            let mut builder = SyntaxMappingBuilder::new(ast.syntax().clone());
            if let Some(abi) = abi {
                builder.map_node(abi.syntax().clone(), ast.abi().unwrap().syntax().clone());
            }
            builder.map_children(
                params_input,
                ast.syntax().children().filter(|c| ast::Param::can_cast(c.kind())),
            );
            if let Some(ret_type) = ret_type {
                builder
                    .map_node(ret_type.syntax().clone(), ast.ret_type().unwrap().syntax().clone());
            }
            builder.finish(&mut mapping);
        }

        ast
    }

    pub fn where_pred(
        &self,
        path: Either<ast::Lifetime, ast::Type>,
        bounds: impl IntoIterator<Item = ast::TypeBound>,
    ) -> ast::WherePred {
        let (bounds, bounds_input) = iterator_input(bounds);
        let ast = make::where_pred(path.clone(), bounds);

        if let Some(mut mapping) = self.mappings() {
            let mut builder = SyntaxMappingBuilder::new(ast.syntax().clone());
            match &path {
                Either::Left(lifetime) => {
                    builder.map_node(
                        lifetime.syntax().clone(),
                        ast.lifetime().unwrap().syntax().clone(),
                    );
                }
                Either::Right(ty) => {
                    builder.map_node(ty.syntax().clone(), ast.ty().unwrap().syntax().clone());
                }
            }
            builder.finish(&mut mapping);

            if let Some(type_bound_list) = ast.type_bound_list() {
                let mut bounds_builder =
                    SyntaxMappingBuilder::new(type_bound_list.syntax().clone());
                bounds_builder.map_children(
                    bounds_input,
                    type_bound_list.bounds().map(|b| b.syntax().clone()),
                );
                bounds_builder.finish(&mut mapping);
            }
        }

        ast
    }

    pub fn where_clause(
        &self,
        predicates: impl IntoIterator<Item = ast::WherePred>,
    ) -> ast::WhereClause {
        let (predicates, input) = iterator_input(predicates);
        let ast = make::where_clause(predicates);

        if let Some(mut mapping) = self.mappings() {
            let mut builder = SyntaxMappingBuilder::new(ast.syntax().clone());
            builder.map_children(input, ast.predicates().map(|p| p.syntax().clone()));
            builder.finish(&mut mapping);
        }

        ast
    }

    pub fn impl_trait_type(&self, bounds: ast::TypeBoundList) -> ast::ImplTraitType {
        let ast = make::impl_trait_type(bounds.clone());

        if let Some(mut mapping) = self.mappings() {
            let mut builder = SyntaxMappingBuilder::new(ast.syntax().clone());
            builder
                .map_node(bounds.syntax().clone(), ast.type_bound_list().unwrap().syntax().clone());
            builder.finish(&mut mapping);
        }

        ast
    }

    pub fn expr_field(&self, receiver: ast::Expr, field: &str) -> ast::FieldExpr {
        let ast::Expr::FieldExpr(ast) = make::expr_field(receiver.clone(), field) else {
            unreachable!()
        };

        if let Some(mut mapping) = self.mappings() {
            let mut builder = SyntaxMappingBuilder::new(ast.syntax().clone());
            builder.map_node(receiver.syntax().clone(), ast.expr().unwrap().syntax().clone());
            builder.finish(&mut mapping);
        }

        ast
    }

    pub fn impl_trait(
        &self,
        attrs: impl IntoIterator<Item = ast::Attr>,
        is_unsafe: bool,
        trait_gen_params: Option<ast::GenericParamList>,
        trait_gen_args: Option<ast::GenericArgList>,
        type_gen_params: Option<ast::GenericParamList>,
        type_gen_args: Option<ast::GenericArgList>,
        is_negative: bool,
        path_type: ast::Type,
        ty: ast::Type,
        trait_where_clause: Option<ast::WhereClause>,
        ty_where_clause: Option<ast::WhereClause>,
        body: Option<ast::AssocItemList>,
    ) -> ast::Impl {
        let (attrs, attrs_input) = iterator_input(attrs);
        let ast = make::impl_trait(
            attrs,
            is_unsafe,
            trait_gen_params.clone(),
            trait_gen_args.clone(),
            type_gen_params.clone(),
            type_gen_args.clone(),
            is_negative,
            path_type.clone(),
            ty.clone(),
            trait_where_clause.clone(),
            ty_where_clause.clone(),
            body.clone(),
        );

        if let Some(mut mapping) = self.mappings() {
            let mut builder = SyntaxMappingBuilder::new(ast.syntax().clone());
            builder.map_children(attrs_input, ast.attrs().map(|attr| attr.syntax().clone()));
            if let Some(trait_gen_params) = trait_gen_params {
                builder.map_node(
                    trait_gen_params.syntax().clone(),
                    ast.generic_param_list().unwrap().syntax().clone(),
                );
            }
            builder.map_node(path_type.syntax().clone(), ast.trait_().unwrap().syntax().clone());
            builder.map_node(ty.syntax().clone(), ast.self_ty().unwrap().syntax().clone());
            if let Some(ty_where_clause) = ty_where_clause {
                builder.map_node(
                    ty_where_clause.syntax().clone(),
                    ast.where_clause().unwrap().syntax().clone(),
                );
            }
            if let Some(body) = body {
                builder.map_node(
                    body.syntax().clone(),
                    ast.assoc_item_list().unwrap().syntax().clone(),
                );
            }
            builder.finish(&mut mapping);
        }

        ast
    }

    pub fn ty_alias(
        &self,
        attrs: impl IntoIterator<Item = ast::Attr>,
        ident: &str,
        generic_param_list: Option<ast::GenericParamList>,
        type_param_bounds: Option<ast::TypeParam>,
        where_clause: Option<ast::WhereClause>,
        assignment: Option<(ast::Type, Option<ast::WhereClause>)>,
    ) -> ast::TypeAlias {
        let (attrs, attrs_input) = iterator_input(attrs);
        let ast = make::ty_alias(
            attrs,
            ident,
            generic_param_list.clone(),
            type_param_bounds.clone(),
            where_clause.clone(),
            assignment.clone(),
        );

        if let Some(mut mapping) = self.mappings() {
            let mut builder = SyntaxMappingBuilder::new(ast.syntax().clone());
            builder.map_children(attrs_input, ast.attrs().map(|attr| attr.syntax().clone()));
            if let Some(generic_param_list) = generic_param_list {
                builder.map_node(
                    generic_param_list.syntax().clone(),
                    ast.generic_param_list().unwrap().syntax().clone(),
                );
            }
            if let Some(type_param_bounds) = type_param_bounds {
                builder.map_node(
                    type_param_bounds.syntax().clone(),
                    ast.type_bound_list().unwrap().syntax().clone(),
                );
            }
            if let Some(where_clause) = where_clause {
                builder.map_node(
                    where_clause.syntax().clone(),
                    ast.where_clause().unwrap().syntax().clone(),
                );
            }
            if let Some((ty, _)) = assignment {
                builder.map_node(ty.syntax().clone(), ast.ty().unwrap().syntax().clone());
            }
            builder.finish(&mut mapping);
        }

        ast
    }

    pub fn param_list(
        &self,
        self_param: Option<ast::SelfParam>,
        params: impl IntoIterator<Item = ast::Param>,
    ) -> ast::ParamList {
        let (params, input) = iterator_input(params);
        let ast = make::param_list(self_param.clone(), params);

        if let Some(mut mapping) = self.mappings() {
            let mut builder = SyntaxMappingBuilder::new(ast.syntax().clone());
            if let Some(self_param) = self_param
                && let Some(new_self_param) = ast.self_param()
            {
                builder.map_node(self_param.syntax().clone(), new_self_param.syntax().clone());
            }
            builder.map_children(input, ast.params().map(|p| p.syntax().clone()));
            builder.finish(&mut mapping);
        }

        ast
    }

    pub fn const_param(&self, name: ast::Name, ty: ast::Type) -> ast::ConstParam {
        let ast = make::const_param(name.clone(), ty.clone());

        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.map_node(ty.syntax().clone(), ast.ty().unwrap().syntax().clone());
            builder.finish(&mut mapping);
        }

        ast
    }

    pub fn lifetime_param(&self, lifetime: ast::Lifetime) -> ast::LifetimeParam {
        let ast = make::lifetime_param(lifetime.clone());

        if let Some(mut mapping) = self.mappings() {
            let mut builder = SyntaxMappingBuilder::new(ast.syntax().clone());
            builder.map_node(lifetime.syntax().clone(), ast.lifetime().unwrap().syntax().clone());
            builder.finish(&mut mapping);
        }

        ast
    }

    pub fn generic_param_list(
        &self,
        params: impl IntoIterator<Item = ast::GenericParam>,
    ) -> ast::GenericParamList {
        let (params, input) = iterator_input(params);
        let ast = make::generic_param_list(params);

        if let Some(mut mapping) = self.mappings() {
            let mut builder = SyntaxMappingBuilder::new(ast.syntax().clone());
            builder.map_children(input, ast.generic_params().map(|p| p.syntax().clone()));
            builder.finish(&mut mapping);
        }

        ast
    }

    pub fn path_segment(&self, name_ref: ast::NameRef) -> ast::PathSegment {
        let ast = make::path_segment(name_ref.clone());

        if let Some(mut mapping) = self.mappings() {
            let mut builder = SyntaxMappingBuilder::new(ast.syntax().clone());
            builder.map_node(name_ref.syntax().clone(), ast.name_ref().unwrap().syntax().clone());
            builder.finish(&mut mapping);
        }

        ast
    }

    pub fn path_segment_self(&self) -> ast::PathSegment {
        make::path_segment_self()
    }

    pub fn path_segment_super(&self) -> ast::PathSegment {
        make::path_segment_super()
    }

    pub fn path_segment_crate(&self) -> ast::PathSegment {
        make::path_segment_crate()
    }

    pub fn generic_ty_path_segment(
        &self,
        name_ref: ast::NameRef,
        generic_args: impl IntoIterator<Item = ast::GenericArg>,
    ) -> ast::PathSegment {
        let (generic_args, input) = iterator_input(generic_args);
        let ast = make::generic_ty_path_segment(name_ref.clone(), generic_args);

        if let Some(mut mapping) = self.mappings() {
            let mut builder = SyntaxMappingBuilder::new(ast.syntax().clone());
            builder.map_node(name_ref.syntax().clone(), ast.name_ref().unwrap().syntax().clone());
            builder.finish(&mut mapping);

            let generic_arg_list = ast.generic_arg_list().unwrap();
            let mut arg_builder = SyntaxMappingBuilder::new(generic_arg_list.syntax().clone());
            arg_builder
                .map_children(input, generic_arg_list.generic_args().map(|a| a.syntax().clone()));
            arg_builder.finish(&mut mapping);
        }

        ast
    }

    pub fn tail_only_block_expr(&self, tail_expr: ast::Expr) -> ast::BlockExpr {
        let ast = make::tail_only_block_expr(tail_expr.clone());

        if let Some(mut mapping) = self.mappings() {
            let stmt_list = ast.stmt_list().unwrap();
            let mut builder = SyntaxMappingBuilder::new(stmt_list.syntax().clone());
            builder.map_node(
                tail_expr.syntax().clone(),
                stmt_list.tail_expr().unwrap().syntax().clone(),
            );
            builder.finish(&mut mapping);
        }

        ast
    }

    pub fn expr_bin_op(&self, lhs: ast::Expr, op: ast::BinaryOp, rhs: ast::Expr) -> ast::Expr {
        let ast::Expr::BinExpr(ast) = make::expr_bin_op(lhs.clone(), op, rhs.clone()) else {
            unreachable!()
        };

        if let Some(mut mapping) = self.mappings() {
            let mut builder = SyntaxMappingBuilder::new(ast.syntax().clone());
            builder.map_node(lhs.syntax().clone(), ast.lhs().unwrap().syntax().clone());
            builder.map_node(rhs.syntax().clone(), ast.rhs().unwrap().syntax().clone());
            builder.finish(&mut mapping);
        }

        ast.into()
    }

    pub fn ty_placeholder(&self) -> ast::Type {
        make::ty_placeholder()
    }

    pub fn ty_unit(&self) -> ast::Type {
        make::ty_unit()
    }

    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);

        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 ty_paren(&self, ty: ast::Type) -> ast::Type {
        let ast::Type::ParenType(paren_ty) = make::ty_paren(ty.clone()) else { unreachable!() };

        if let Some(mut mapping) = self.mappings() {
            let mut builder = SyntaxMappingBuilder::new(paren_ty.syntax().clone());
            builder.map_node(ty.syntax().clone(), paren_ty.ty().unwrap().syntax().clone());
            builder.finish(&mut mapping);
        }

        paren_ty.into()
    }

    pub fn path_segment_generics(
        &self,
        name_ref: ast::NameRef,
        generic_arg_list: ast::GenericArgList,
    ) -> ast::PathSegment {
        let ast::Type::PathType(path) = make::ty(&format!("{name_ref}{generic_arg_list}")) else {
            unreachable!();
        };

        let ast = path.path().unwrap().segment().unwrap();

        if let Some(mut mapping) = self.mappings() {
            let mut builder = SyntaxMappingBuilder::new(ast.syntax().clone());
            builder.map_node(name_ref.syntax().clone(), ast.name_ref().unwrap().syntax().clone());
            builder.map_node(
                generic_arg_list.syntax().clone(),
                ast.generic_arg_list().unwrap().syntax().clone(),
            );
            builder.finish(&mut mapping);
        }

        ast
    }

    pub fn use_(
        &self,
        attrs: impl IntoIterator<Item = ast::Attr>,
        visibility: Option<ast::Visibility>,
        use_tree: ast::UseTree,
    ) -> ast::Use {
        let (attrs, attrs_input) = iterator_input(attrs);
        let ast = make::use_(attrs, visibility.clone(), use_tree.clone());

        if let Some(mut mapping) = self.mappings() {
            let mut builder = SyntaxMappingBuilder::new(ast.syntax().clone());
            builder.map_children(attrs_input, ast.attrs().map(|attr| attr.syntax().clone()));
            if let Some(visibility) = visibility {
                builder.map_node(
                    visibility.syntax().clone(),
                    ast.visibility().unwrap().syntax().clone(),
                );
            }
            builder.map_node(use_tree.syntax().clone(), ast.use_tree().unwrap().syntax().clone());
            builder.finish(&mut mapping);
        }

        ast
    }

    pub fn use_tree(
        &self,
        path: ast::Path,
        use_tree_list: Option<ast::UseTreeList>,
        alias: Option<ast::Rename>,
        add_star: bool,
    ) -> ast::UseTree {
        let ast = make::use_tree(path.clone(), use_tree_list.clone(), alias.clone(), add_star);

        if let Some(mut mapping) = self.mappings() {
            let mut builder = SyntaxMappingBuilder::new(ast.syntax().clone());
            builder.map_node(path.syntax().clone(), ast.path().unwrap().syntax().clone());
            if let Some(use_tree_list) = use_tree_list {
                builder.map_node(
                    use_tree_list.syntax().clone(),
                    ast.use_tree_list().unwrap().syntax().clone(),
                );
            }
            if let Some(alias) = alias {
                builder.map_node(alias.syntax().clone(), ast.rename().unwrap().syntax().clone());
            }
            builder.finish(&mut mapping);
        }

        ast
    }

    pub fn use_tree_glob(&self) -> ast::UseTree {
        make::use_tree_glob()
    }

    pub fn path_unqualified(&self, segment: ast::PathSegment) -> ast::Path {
        let ast = make::path_unqualified(segment.clone());

        if let Some(mut mapping) = self.mappings() {
            let mut builder = SyntaxMappingBuilder::new(ast.syntax().clone());
            builder.map_node(segment.syntax().clone(), ast.segment().unwrap().syntax().clone());
            builder.finish(&mut mapping);
        }

        ast
    }

    pub fn path_qualified(&self, qual: ast::Path, segment: ast::PathSegment) -> ast::Path {
        let ast = make::path_qualified(qual.clone(), segment.clone());

        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>,
        is_abs: bool,
    ) -> ast::Path {
        let (segments, input) = iterator_input(segments);
        let ast = make::path_from_segments(segments, is_abs);

        if let Some(mut mapping) = self.mappings() {
            let mut current_path = Some(ast.clone());
            for input_segment in input.iter().rev() {
                let Some(path) = current_path else { break };
                if let Some(segment) = path.segment() {
                    let mut builder = SyntaxMappingBuilder::new(path.syntax().clone());
                    builder.map_node(input_segment.clone(), segment.syntax().clone());
                    builder.finish(&mut mapping);
                }
                current_path = path.qualifier();
            }
        }

        ast
    }

    pub fn ident_pat(&self, ref_: bool, mut_: bool, name: ast::Name) -> ast::IdentPat {
        let ast = make::ident_pat(ref_, mut_, name.clone());

        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 simple_ident_pat(&self, name: ast::Name) -> ast::IdentPat {
        let ast = make::ext::simple_ident_pat(name.clone());

        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()
    }

    pub fn literal_pat(&self, text: &str) -> ast::LiteralPat {
        make::literal_pat(text)
    }

    pub fn slice_pat(&self, pats: impl IntoIterator<Item = ast::Pat>) -> ast::SlicePat {
        let (pats, input) = iterator_input(pats);
        let ast = make::slice_pat(pats);

        if let Some(mut mapping) = self.mappings() {
            let mut builder = SyntaxMappingBuilder::new(ast.syntax().clone());
            builder.map_children(input, ast.pats().map(|it| it.syntax().clone()));
            builder.finish(&mut mapping);
        }

        ast
    }

    pub fn tuple_pat(&self, pats: impl IntoIterator<Item = ast::Pat>) -> ast::TuplePat {
        let (pats, input) = iterator_input(pats);
        let ast = make::tuple_pat(pats);

        if let Some(mut mapping) = self.mappings() {
            let mut builder = SyntaxMappingBuilder::new(ast.syntax().clone());
            builder.map_children(input, ast.fields().map(|it| it.syntax().clone()));
            builder.finish(&mut mapping);
        }

        ast
    }

    pub fn tuple_struct_pat(
        &self,
        path: ast::Path,
        fields: impl IntoIterator<Item = ast::Pat>,
    ) -> ast::TupleStructPat {
        let (fields, input) = iterator_input(fields);
        let ast = make::tuple_struct_pat(path.clone(), fields);

        if let Some(mut mapping) = self.mappings() {
            let mut builder = SyntaxMappingBuilder::new(ast.syntax().clone());
            builder.map_node(path.syntax().clone(), ast.path().unwrap().syntax().clone());
            builder.map_children(input, ast.fields().map(|it| it.syntax().clone()));
            builder.finish(&mut mapping);
        }

        ast
    }

    pub fn record_pat_with_fields(
        &self,
        path: ast::Path,
        fields: ast::RecordPatFieldList,
    ) -> ast::RecordPat {
        let ast = make::record_pat_with_fields(path.clone(), fields.clone());

        if let Some(mut mapping) = self.mappings() {
            let mut builder = SyntaxMappingBuilder::new(ast.syntax().clone());
            builder.map_node(path.syntax().clone(), ast.path().unwrap().syntax().clone());
            builder.map_node(
                fields.syntax().clone(),
                ast.record_pat_field_list().unwrap().syntax().clone(),
            );
            builder.finish(&mut mapping);
        }

        ast
    }

    pub fn record_pat_field_list(
        &self,
        fields: impl IntoIterator<Item = ast::RecordPatField>,
        rest_pat: Option<ast::RestPat>,
    ) -> ast::RecordPatFieldList {
        let (fields, input) = iterator_input(fields);
        let ast = make::record_pat_field_list(fields, rest_pat.clone());

        if let Some(mut mapping) = self.mappings() {
            let mut builder = SyntaxMappingBuilder::new(ast.syntax().clone());
            builder.map_children(input, ast.fields().map(|it| it.syntax().clone()));
            if let Some(rest_pat) = rest_pat {
                builder
                    .map_node(rest_pat.syntax().clone(), ast.rest_pat().unwrap().syntax().clone());
            }
            builder.finish(&mut mapping);
        }

        ast
    }

    pub fn record_pat_field(&self, name_ref: ast::NameRef, pat: ast::Pat) -> ast::RecordPatField {
        let ast = make::record_pat_field(name_ref.clone(), pat.clone());

        if let Some(mut mapping) = self.mappings() {
            let mut builder = SyntaxMappingBuilder::new(ast.syntax().clone());
            builder.map_node(name_ref.syntax().clone(), ast.name_ref().unwrap().syntax().clone());
            builder.map_node(pat.syntax().clone(), ast.pat().unwrap().syntax().clone());
            builder.finish(&mut mapping);
        }

        ast
    }

    pub fn record_pat_field_shorthand(&self, pat: ast::Pat) -> ast::RecordPatField {
        let ast = make::record_pat_field_shorthand(pat.clone());

        if let Some(mut mapping) = self.mappings() {
            let mut builder = SyntaxMappingBuilder::new(ast.syntax().clone());
            builder.map_node(pat.syntax().clone(), ast.pat().unwrap().syntax().clone());
            builder.finish(&mut mapping);
        }

        ast
    }

    pub fn path_pat(&self, path: ast::Path) -> ast::Pat {
        let ast = make::path_pat(path.clone());

        match &ast {
            ast::Pat::PathPat(ast) => {
                if let Some(mut mapping) = self.mappings() {
                    let mut builder = SyntaxMappingBuilder::new(ast.syntax().clone());
                    builder.map_node(path.syntax().clone(), ast.path().unwrap().syntax().clone());
                    builder.finish(&mut mapping)
                }
            }
            ast::Pat::IdentPat(ast) => {
                if let Some(mut mapping) = self.mappings() {
                    let mut builder = SyntaxMappingBuilder::new(ast.syntax().clone());
                    builder.map_node(path.syntax().clone(), ast.name().unwrap().syntax().clone());
                    builder.finish(&mut mapping)
                }
            }
            _ => unreachable!(),
        }

        ast
    }

    pub fn rest_pat(&self) -> ast::RestPat {
        make::rest_pat()
    }

    pub fn or_pat(
        &self,
        pats: impl IntoIterator<Item = ast::Pat>,
        leading_pipe: bool,
    ) -> ast::OrPat {
        let (pats, input) = iterator_input(pats);
        let ast = make::or_pat(pats, leading_pipe);

        if let Some(mut mapping) = self.mappings() {
            let mut builder = SyntaxMappingBuilder::new(ast.syntax().clone());
            builder.map_children(input, ast.pats().map(|it| it.syntax().clone()));
            builder.finish(&mut mapping);
        }

        ast
    }

    pub fn box_pat(&self, pat: ast::Pat) -> ast::BoxPat {
        let ast = make::box_pat(pat.clone());

        if let Some(mut mapping) = self.mappings() {
            let mut builder = SyntaxMappingBuilder::new(ast.syntax().clone());
            builder.map_node(pat.syntax().clone(), ast.pat().unwrap().syntax().clone());
            builder.finish(&mut mapping);
        }

        ast
    }

    pub fn deref_pat(&self, pat: ast::Pat) -> ast::Pat {
        make::deref_pat(pat.clone())
    }

    pub fn paren_pat(&self, pat: ast::Pat) -> ast::ParenPat {
        let ast = make::paren_pat(pat.clone());

        if let Some(mut mapping) = self.mappings() {
            let mut builder = SyntaxMappingBuilder::new(ast.syntax().clone());
            builder.map_node(pat.syntax().clone(), ast.pat().unwrap().syntax().clone());
            builder.finish(&mut mapping);
        }

        ast
    }

    pub fn range_pat(&self, start: Option<ast::Pat>, end: Option<ast::Pat>) -> ast::RangePat {
        let ast = make::range_pat(start.clone(), end.clone());

        if let Some(mut mapping) = self.mappings() {
            let mut builder = SyntaxMappingBuilder::new(ast.syntax().clone());
            if let Some(start) = start {
                builder.map_node(start.syntax().clone(), ast.start().unwrap().syntax().clone());
            }
            if let Some(end) = end {
                builder.map_node(end.syntax().clone(), ast.end().unwrap().syntax().clone());
            }
            builder.finish(&mut mapping);
        }

        ast
    }

    pub fn ref_pat(&self, pat: ast::Pat) -> ast::RefPat {
        let ast = make::ref_pat(pat.clone());

        if let Some(mut mapping) = self.mappings() {
            let mut builder = SyntaxMappingBuilder::new(ast.syntax().clone());
            builder.map_node(pat.syntax().clone(), ast.pat().unwrap().syntax().clone());
            builder.finish(&mut mapping);
        }

        ast
    }

    pub fn 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::block_expr(statements, tail_expr.clone());

        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() {
                // The parser interpreted the last statement (probably a statement with a block) as an 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 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());

        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()
    }

    pub fn expr_paren(&self, expr: ast::Expr) -> ast::ParenExpr {
        let ast = make::expr_paren(expr.clone());

        if let Some(mut mapping) = self.mappings() {
            let mut builder = SyntaxMappingBuilder::new(ast.syntax().clone());
            builder.map_node(expr.syntax().clone(), ast.expr().unwrap().syntax().clone());
            builder.finish(&mut mapping);
        }

        ast
    }

    pub fn expr_tuple(&self, fields: impl IntoIterator<Item = ast::Expr>) -> ast::TupleExpr {
        let (fields, input) = iterator_input(fields);
        let ast = make::expr_tuple(fields);

        if let Some(mut mapping) = self.mappings() {
            let mut builder = SyntaxMappingBuilder::new(ast.syntax().clone());
            builder.map_children(input, ast.fields().map(|it| it.syntax().clone()));
            builder.finish(&mut mapping);
        }

        ast
    }

    pub fn expr_assignment(&self, lhs: ast::Expr, rhs: ast::Expr) -> ast::BinExpr {
        let ast = make::expr_assignment(lhs.clone(), rhs.clone());

        if let Some(mut mapping) = self.mappings() {
            let mut builder = SyntaxMappingBuilder::new(ast.syntax().clone());
            builder.map_node(lhs.syntax().clone(), ast.lhs().unwrap().syntax().clone());
            builder.map_node(rhs.syntax().clone(), ast.rhs().unwrap().syntax().clone());
            builder.finish(&mut mapping);
        }

        ast
    }

    pub fn expr_bin(&self, lhs: ast::Expr, op: ast::BinaryOp, rhs: ast::Expr) -> ast::BinExpr {
        let ast::Expr::BinExpr(ast) = make::expr_bin_op(lhs.clone(), op, rhs.clone()) else {
            unreachable!()
        };

        if let Some(mut mapping) = self.mappings() {
            let mut builder = SyntaxMappingBuilder::new(ast.syntax().clone());
            builder.map_node(lhs.syntax().clone(), ast.lhs().unwrap().syntax().clone());
            builder.map_node(rhs.syntax().clone(), ast.rhs().unwrap().syntax().clone());
            builder.finish(&mut mapping);
        }

        ast
    }

    pub fn expr_literal(&self, text: &str) -> ast::Literal {
        make::expr_literal(text)
    }

    pub fn expr_path(&self, path: ast::Path) -> ast::Expr {
        let ast::Expr::PathExpr(ast) = make::expr_path(path.clone()) else { unreachable!() };

        if let Some(mut mapping) = self.mappings() {
            let mut builder = SyntaxMappingBuilder::new(ast.syntax().clone());
            builder.map_node(path.syntax().clone(), ast.path().unwrap().syntax().clone());
            builder.finish(&mut mapping);
        }

        ast.into()
    }

    pub fn expr_prefix(&self, op: SyntaxKind, expr: ast::Expr) -> ast::PrefixExpr {
        let ast = make::expr_prefix(op, expr.clone());

        if let Some(mut mapping) = self.mappings() {
            let mut builder = SyntaxMappingBuilder::new(ast.syntax().clone());
            builder.map_node(expr.syntax().clone(), ast.expr().unwrap().syntax().clone());
            builder.finish(&mut mapping);
        }

        ast
    }

    pub fn expr_call(&self, expr: ast::Expr, arg_list: ast::ArgList) -> ast::CallExpr {
        let ast = make::expr_call(expr.clone(), arg_list.clone());

        if let Some(mut mapping) = self.mappings() {
            let mut builder = SyntaxMappingBuilder::new(ast.syntax().clone());
            builder.map_node(expr.syntax().clone(), ast.expr().unwrap().syntax().clone());
            builder.map_node(arg_list.syntax().clone(), ast.arg_list().unwrap().syntax().clone());
            builder.finish(&mut mapping);
        }

        ast
    }

    pub fn expr_method_call(
        &self,
        receiver: ast::Expr,
        method: ast::NameRef,
        arg_list: ast::ArgList,
    ) -> ast::MethodCallExpr {
        let ast = make::expr_method_call(receiver.clone(), method.clone(), arg_list.clone());

        if let Some(mut mapping) = self.mappings() {
            let mut builder = SyntaxMappingBuilder::new(ast.syntax().clone());
            builder.map_node(receiver.syntax().clone(), ast.receiver().unwrap().syntax().clone());
            builder.map_node(method.syntax().clone(), ast.name_ref().unwrap().syntax().clone());
            builder.map_node(arg_list.syntax().clone(), ast.arg_list().unwrap().syntax().clone());
            builder.finish(&mut mapping);
        }

        ast
    }

    pub fn arg_list(&self, args: impl IntoIterator<Item = ast::Expr>) -> ast::ArgList {
        let (args, input) = iterator_input(args);
        let ast = make::arg_list(args);

        if let Some(mut mapping) = self.mappings() {
            let mut builder = SyntaxMappingBuilder::new(ast.syntax.clone());
            builder.map_children(input, ast.args().map(|it| it.syntax().clone()));
            builder.finish(&mut mapping);
        }

        ast
    }

    pub fn expr_ref(&self, expr: ast::Expr, exclusive: bool) -> ast::Expr {
        let ast::Expr::RefExpr(ast) = make::expr_ref(expr.clone(), exclusive) else {
            unreachable!()
        };

        if let Some(mut mapping) = self.mappings() {
            let mut builder = SyntaxMappingBuilder::new(ast.syntax().clone());
            builder.map_node(expr.syntax().clone(), ast.expr().unwrap().syntax().clone());
            builder.finish(&mut mapping);
        }

        ast.into()
    }

    pub fn expr_reborrow(&self, expr: ast::Expr) -> ast::Expr {
        let ast::Expr::RefExpr(ast) = make::expr_reborrow(expr.clone()) 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) else {
            unreachable!()
        };

        if let Some(mut mapping) = self.mappings() {
            let mut builder = SyntaxMappingBuilder::new(ast.syntax().clone());
            builder.map_node(expr.syntax().clone(), ast.expr().unwrap().syntax().clone());
            builder.finish(&mut mapping);
        }

        ast.into()
    }

    pub fn expr_closure(
        &self,
        pats: impl IntoIterator<Item = ast::Param>,
        expr: ast::Expr,
    ) -> ast::ClosureExpr {
        let (args, input) = iterator_input(pats);
        let ast = make::expr_closure(args, expr.clone());

        if let Some(mut mapping) = self.mappings() {
            let mut builder = SyntaxMappingBuilder::new(ast.syntax().clone());
            builder.map_node(expr.syntax().clone(), ast.body().unwrap().syntax().clone());
            builder.finish(&mut mapping);

            let param_list = ast.param_list().unwrap();
            let mut params_builder = SyntaxMappingBuilder::new(param_list.syntax().clone());
            params_builder
                .map_children(input, param_list.params().map(|param| param.syntax().clone()));
            params_builder.finish(&mut mapping);
        }

        ast
    }

    pub fn expr_return(&self, expr: Option<ast::Expr>) -> ast::ReturnExpr {
        let ast::Expr::ReturnExpr(ast) = make::expr_return(expr.clone()) else { unreachable!() };

        if let Some(mut mapping) = self.mappings() {
            let mut builder = SyntaxMappingBuilder::new(ast.syntax().clone());
            if let Some(input) = expr {
                builder.map_node(input.syntax().clone(), ast.expr().unwrap().syntax().clone());
            }
            builder.finish(&mut mapping);
        }

        ast
    }

    pub fn expr_underscore(&self) -> ast::UnderscoreExpr {
        let ast::Expr::UnderscoreExpr(ast) = make::ext::expr_underscore() else { unreachable!() };

        ast
    }

    pub fn expr_if(
        &self,
        condition: ast::Expr,
        then_branch: ast::BlockExpr,
        else_branch: Option<ast::ElseBranch>,
    ) -> ast::IfExpr {
        let ast = make::expr_if(condition.clone(), then_branch.clone(), else_branch.clone());

        if let Some(mut mapping) = self.mappings() {
            let mut builder = SyntaxMappingBuilder::new(ast.syntax().clone());
            builder.map_node(condition.syntax().clone(), ast.condition().unwrap().syntax().clone());
            builder.map_node(
                then_branch.syntax().clone(),
                ast.then_branch().unwrap().syntax().clone(),
            );

            if let Some(else_branch) = else_branch {
                builder.map_node(
                    else_branch.syntax().clone(),
                    ast.else_branch().unwrap().syntax().clone(),
                );
            }
            builder.finish(&mut mapping);
        }

        ast
    }

    pub fn expr_loop(&self, body: ast::BlockExpr) -> ast::LoopExpr {
        let ast::Expr::LoopExpr(ast) = make::expr_loop(body.clone()) else { unreachable!() };

        if let Some(mut mapping) = self.mappings() {
            let mut builder = SyntaxMappingBuilder::new(ast.syntax().clone());
            builder.map_node(body.syntax().clone(), ast.loop_body().unwrap().syntax().clone());
            builder.finish(&mut mapping);
        }

        ast
    }

    pub fn expr_while_loop(&self, condition: ast::Expr, body: ast::BlockExpr) -> ast::WhileExpr {
        let ast = make::expr_while_loop(condition.clone(), body.clone());

        if let Some(mut mapping) = self.mappings() {
            let mut builder = SyntaxMappingBuilder::new(ast.syntax().clone());
            builder.map_node(condition.syntax().clone(), ast.condition().unwrap().syntax().clone());
            builder.map_node(body.syntax().clone(), ast.loop_body().unwrap().syntax().clone());
            builder.finish(&mut mapping);
        }

        ast
    }

    pub fn expr_for_loop(
        &self,
        pat: ast::Pat,
        iterable: ast::Expr,
        body: ast::BlockExpr,
    ) -> ast::ForExpr {
        let ast = make::expr_for_loop(pat.clone(), iterable.clone(), body.clone());

        if let Some(mut mapping) = self.mappings() {
            let mut builder = SyntaxMappingBuilder::new(ast.syntax().clone());
            builder.map_node(pat.syntax().clone(), ast.pat().unwrap().syntax().clone());
            builder.map_node(iterable.syntax().clone(), ast.iterable().unwrap().syntax().clone());
            builder.map_node(body.syntax().clone(), ast.loop_body().unwrap().syntax().clone());
            builder.finish(&mut mapping);
        }

        ast
    }

    pub fn expr_let(&self, pattern: ast::Pat, expr: ast::Expr) -> ast::LetExpr {
        let ast = make::expr_let(pattern.clone(), expr.clone());

        if let Some(mut mapping) = self.mappings() {
            let mut builder = SyntaxMappingBuilder::new(ast.syntax().clone());
            builder.map_node(pattern.syntax().clone(), ast.pat().unwrap().syntax().clone());
            builder.map_node(expr.syntax().clone(), ast.expr().unwrap().syntax().clone());
            builder.finish(&mut mapping);
        }

        ast
    }

    pub fn expr_stmt(&self, expr: ast::Expr) -> ast::ExprStmt {
        let ast = make::expr_stmt(expr.clone());

        if let Some(mut mapping) = self.mappings() {
            let mut builder = SyntaxMappingBuilder::new(ast.syntax().clone());
            builder.map_node(expr.syntax().clone(), ast.expr().unwrap().syntax().clone());
            builder.finish(&mut mapping);
        }

        ast
    }

    pub fn expr_match(&self, expr: ast::Expr, match_arm_list: ast::MatchArmList) -> ast::MatchExpr {
        let ast = make::expr_match(expr.clone(), match_arm_list.clone());

        if let Some(mut mapping) = self.mappings() {
            let mut builder = SyntaxMappingBuilder::new(ast.syntax().clone());
            builder.map_node(expr.syntax().clone(), ast.expr().unwrap().syntax().clone());
            builder.map_node(
                match_arm_list.syntax().clone(),
                ast.match_arm_list().unwrap().syntax().clone(),
            );
            builder.finish(&mut mapping);
        }

        ast
    }

    pub fn expr_macro(&self, path: ast::Path, tt: ast::TokenTree) -> ast::MacroExpr {
        let ast = make::expr_macro(path.clone(), tt.clone());

        if let Some(mut mapping) = self.mappings() {
            let macro_call = ast.macro_call().unwrap();
            let mut builder = SyntaxMappingBuilder::new(macro_call.syntax().clone());
            builder.map_node(path.syntax().clone(), macro_call.path().unwrap().syntax().clone());
            builder
                .map_node(tt.syntax().clone(), macro_call.token_tree().unwrap().syntax().clone());
            builder.finish(&mut mapping);
        }

        ast
    }

    pub fn match_arm(
        &self,
        pat: ast::Pat,
        guard: Option<ast::MatchGuard>,
        expr: ast::Expr,
    ) -> ast::MatchArm {
        let ast = make::match_arm(pat.clone(), guard.clone(), expr.clone());

        if let Some(mut mapping) = self.mappings() {
            let mut builder = SyntaxMappingBuilder::new(ast.syntax().clone());
            builder.map_node(pat.syntax().clone(), ast.pat().unwrap().syntax().clone());
            if let Some(guard) = guard {
                builder.map_node(guard.syntax().clone(), ast.guard().unwrap().syntax().clone());
            }
            builder.map_node(expr.syntax().clone(), ast.expr().unwrap().syntax().clone());
            builder.finish(&mut mapping);
        }

        ast
    }

    pub fn match_guard(&self, condition: ast::Expr) -> ast::MatchGuard {
        let ast = make::match_guard(condition.clone());

        if let Some(mut mapping) = self.mappings() {
            let mut builder = SyntaxMappingBuilder::new(ast.syntax().clone());
            builder.map_node(condition.syntax().clone(), ast.condition().unwrap().syntax().clone());
            builder.finish(&mut mapping);
        }

        ast
    }

    pub fn match_arm_list(
        &self,
        match_arms: impl IntoIterator<Item = ast::MatchArm>,
    ) -> ast::MatchArmList {
        let (match_arms, input) = iterator_input(match_arms);
        let ast = make::match_arm_list(match_arms);

        if let Some(mut mapping) = self.mappings() {
            let mut builder = SyntaxMappingBuilder::new(ast.syntax().clone());
            builder.map_children(input, ast.arms().map(|it| it.syntax().clone()));
            builder.finish(&mut mapping);
        }

        ast
    }

    pub fn let_stmt(
        &self,
        pattern: ast::Pat,
        ty: Option<ast::Type>,
        initializer: Option<ast::Expr>,
    ) -> ast::LetStmt {
        let ast = make::let_stmt(pattern.clone(), ty.clone(), initializer.clone());

        if let Some(mut mapping) = self.mappings() {
            let mut builder = SyntaxMappingBuilder::new(ast.syntax().clone());
            builder.map_node(pattern.syntax().clone(), ast.pat().unwrap().syntax().clone());
            if let Some(input) = ty {
                builder.map_node(input.syntax().clone(), ast.ty().unwrap().syntax().clone());
            }
            if let Some(input) = initializer {
                builder
                    .map_node(input.syntax().clone(), ast.initializer().unwrap().syntax().clone());
            }
            builder.finish(&mut mapping);
        }

        ast
    }

    pub fn let_else_stmt(
        &self,
        pattern: ast::Pat,
        ty: Option<ast::Type>,
        initializer: ast::Expr,
        diverging: ast::BlockExpr,
    ) -> ast::LetStmt {
        let ast = make::let_else_stmt(
            pattern.clone(),
            ty.clone(),
            initializer.clone(),
            diverging.clone(),
        );

        if let Some(mut mapping) = self.mappings() {
            let mut builder = SyntaxMappingBuilder::new(ast.syntax().clone());
            builder.map_node(pattern.syntax().clone(), ast.pat().unwrap().syntax().clone());
            if let Some(input) = ty {
                builder.map_node(input.syntax().clone(), ast.ty().unwrap().syntax().clone());
            }
            builder.map_node(
                initializer.syntax().clone(),
                ast.initializer().unwrap().syntax().clone(),
            );
            builder.map_node(diverging.syntax().clone(), ast.let_else().unwrap().syntax().clone());
            builder.finish(&mut mapping);
        }

        ast
    }

    pub fn type_arg(&self, ty: ast::Type) -> ast::TypeArg {
        let ast = make::type_arg(ty.clone());

        if let Some(mut mapping) = self.mappings() {
            let mut builder = SyntaxMappingBuilder::new(ast.syntax().clone());
            builder.map_node(ty.syntax().clone(), ast.ty().unwrap().syntax().clone());
            builder.finish(&mut mapping);
        }

        ast
    }

    pub fn lifetime_arg(&self, lifetime: ast::Lifetime) -> ast::LifetimeArg {
        let ast = make::lifetime_arg(lifetime.clone());

        if let Some(mut mapping) = self.mappings() {
            let mut builder = SyntaxMappingBuilder::new(ast.syntax().clone());
            builder.map_node(lifetime.syntax().clone(), ast.lifetime().unwrap().syntax().clone());
            builder.finish(&mut mapping);
        }

        ast
    }

    pub fn item_const(
        &self,
        attrs: impl IntoIterator<Item = ast::Attr>,
        visibility: Option<ast::Visibility>,
        name: ast::Name,
        ty: ast::Type,
        expr: ast::Expr,
    ) -> ast::Const {
        let (attrs, attrs_input) = iterator_input(attrs);
        let ast =
            make::item_const(attrs, visibility.clone(), name.clone(), ty.clone(), expr.clone());

        if let Some(mut mapping) = self.mappings() {
            let mut builder = SyntaxMappingBuilder::new(ast.syntax().clone());
            builder.map_children(attrs_input, ast.attrs().map(|attr| attr.syntax().clone()));
            if let Some(visibility) = visibility {
                builder.map_node(
                    visibility.syntax().clone(),
                    ast.visibility().unwrap().syntax().clone(),
                );
            }
            builder.map_node(name.syntax().clone(), ast.name().unwrap().syntax().clone());
            builder.map_node(ty.syntax().clone(), ast.ty().unwrap().syntax().clone());
            builder.map_node(expr.syntax().clone(), ast.body().unwrap().syntax().clone());
            builder.finish(&mut mapping);
        }

        ast
    }

    pub fn item_static(
        &self,
        visibility: Option<ast::Visibility>,
        is_unsafe: bool,
        is_mut: bool,
        name: ast::Name,
        ty: ast::Type,
        expr: Option<ast::Expr>,
    ) -> ast::Static {
        let ast = make::item_static(
            visibility.clone(),
            is_unsafe,
            is_mut,
            name.clone(),
            ty.clone(),
            expr.clone(),
        );

        if let Some(mut mapping) = self.mappings() {
            let mut builder = SyntaxMappingBuilder::new(ast.syntax().clone());
            if let Some(visibility) = visibility {
                builder.map_node(
                    visibility.syntax().clone(),
                    ast.visibility().unwrap().syntax().clone(),
                );
            }

            builder.map_node(name.syntax().clone(), ast.name().unwrap().syntax().clone());
            builder.map_node(ty.syntax().clone(), ast.ty().unwrap().syntax().clone());

            if let Some(expr) = expr {
                builder.map_node(expr.syntax().clone(), ast.body().unwrap().syntax().clone());
            }
            builder.finish(&mut mapping);
        }

        ast
    }

    pub fn param(&self, pat: ast::Pat, ty: ast::Type) -> ast::Param {
        let ast = make::param(pat.clone(), ty.clone());

        if let Some(mut mapping) = self.mappings() {
            let mut builder = SyntaxMappingBuilder::new(ast.syntax().clone());
            builder.map_node(pat.syntax().clone(), ast.pat().unwrap().syntax().clone());
            builder.map_node(ty.syntax().clone(), ast.ty().unwrap().syntax().clone());
            builder.finish(&mut mapping);
        }

        ast
    }

    pub fn generic_arg_list(
        &self,
        generic_args: impl IntoIterator<Item = ast::GenericArg>,
        is_turbo: bool,
    ) -> ast::GenericArgList {
        let (generic_args, input) = iterator_input(generic_args);
        let ast = if is_turbo {
            make::turbofish_generic_arg_list(generic_args)
        } else {
            make::generic_arg_list(generic_args)
        };

        if let Some(mut mapping) = self.mappings() {
            let mut builder = SyntaxMappingBuilder::new(ast.syntax().clone());
            builder.map_children(input, ast.generic_args().map(|arg| arg.syntax().clone()));
            builder.finish(&mut mapping);
        }

        ast
    }

    pub fn record_expr(
        &self,
        path: ast::Path,
        fields: ast::RecordExprFieldList,
    ) -> ast::RecordExpr {
        let ast = make::record_expr(path.clone(), fields.clone());
        if let Some(mut mapping) = self.mappings() {
            let mut builder = SyntaxMappingBuilder::new(ast.syntax().clone());
            builder.map_node(path.syntax().clone(), ast.path().unwrap().syntax().clone());
            builder.map_node(
                fields.syntax().clone(),
                ast.record_expr_field_list().unwrap().syntax().clone(),
            );
            builder.finish(&mut mapping);
        }
        ast
    }

    pub fn record_expr_field_list(
        &self,
        fields: impl IntoIterator<Item = ast::RecordExprField>,
    ) -> ast::RecordExprFieldList {
        let (fields, input) = iterator_input(fields);
        let ast = make::record_expr_field_list(fields);

        if let Some(mut mapping) = self.mappings() {
            let mut builder = SyntaxMappingBuilder::new(ast.syntax().clone());
            builder.map_children(input, ast.fields().map(|f| f.syntax().clone()));
            builder.finish(&mut mapping);
        }

        ast
    }

    pub fn record_expr_field(
        &self,
        name: ast::NameRef,
        expr: Option<ast::Expr>,
    ) -> ast::RecordExprField {
        let ast = make::record_expr_field(name.clone(), expr.clone());

        if let Some(mut mapping) = self.mappings() {
            let mut builder = SyntaxMappingBuilder::new(ast.syntax().clone());

            if let Some(ast_name_ref) = ast.name_ref() {
                // NameRef is a direct child
                builder.map_node(name.syntax().clone(), ast_name_ref.syntax().clone());
            } else {
                // NameRef is nested inside PathExpr > Path > PathSegment.
                // map_node requires the output to be a direct child of the builder's parent, so
                // we need a separate builder scoped to PathSegment.
                let ast::Expr::PathExpr(path_expr) = ast.expr().unwrap() else { unreachable!() };
                let path_segment = path_expr.path().unwrap().segment().unwrap();
                let inner_name_ref = path_segment.name_ref().unwrap();
                let mut inner_builder = SyntaxMappingBuilder::new(path_segment.syntax().clone());
                inner_builder.map_node(name.syntax().clone(), inner_name_ref.syntax().clone());
                inner_builder.finish(&mut mapping);
            }
            if let Some(expr) = expr {
                builder.map_node(expr.syntax().clone(), ast.expr().unwrap().syntax().clone());
            }

            builder.finish(&mut mapping);
        }

        ast
    }

    pub fn record_field_list(
        &self,
        fields: impl IntoIterator<Item = ast::RecordField>,
    ) -> ast::RecordFieldList {
        let (fields, input) = iterator_input(fields);
        let ast = make::record_field_list(fields);

        if let Some(mut mapping) = self.mappings() {
            let mut builder = SyntaxMappingBuilder::new(ast.syntax().clone());

            builder.map_children(input, ast.fields().map(|it| it.syntax().clone()));

            builder.finish(&mut mapping);
        }

        ast
    }

    pub fn record_field(
        &self,
        visibility: Option<ast::Visibility>,
        name: ast::Name,
        ty: ast::Type,
    ) -> ast::RecordField {
        let ast = make::record_field(visibility.clone(), name.clone(), ty.clone());

        if let Some(mut mapping) = self.mappings() {
            let mut builder = SyntaxMappingBuilder::new(ast.syntax().clone());
            if let Some(visibility) = visibility {
                builder.map_node(
                    visibility.syntax().clone(),
                    ast.visibility().unwrap().syntax().clone(),
                );
            }

            builder.map_node(name.syntax().clone(), ast.name().unwrap().syntax().clone());
            builder.map_node(ty.syntax().clone(), ast.ty().unwrap().syntax().clone());

            builder.finish(&mut mapping);
        }

        ast
    }

    pub fn tuple_field_list(
        &self,
        fields: impl IntoIterator<Item = ast::TupleField>,
    ) -> ast::TupleFieldList {
        let (fields, input) = iterator_input(fields);
        let ast = make::tuple_field_list(fields);

        if let Some(mut mapping) = self.mappings() {
            let mut builder = SyntaxMappingBuilder::new(ast.syntax().clone());

            builder.map_children(input, ast.fields().map(|it| it.syntax().clone()));

            builder.finish(&mut mapping);
        }

        ast
    }

    pub fn tuple_field(
        &self,
        visibility: Option<ast::Visibility>,
        ty: ast::Type,
    ) -> ast::TupleField {
        let ast = make::tuple_field(visibility.clone(), ty.clone());

        if let Some(mut mapping) = self.mappings() {
            let mut builder = SyntaxMappingBuilder::new(ast.syntax().clone());
            if let Some(visibility) = visibility {
                builder.map_node(
                    visibility.syntax().clone(),
                    ast.visibility().unwrap().syntax().clone(),
                );
            }

            builder.map_node(ty.syntax().clone(), ast.ty().unwrap().syntax().clone());

            builder.finish(&mut mapping);
        }

        ast
    }

    pub fn item_enum(
        &self,
        attrs: impl IntoIterator<Item = ast::Attr>,
        visibility: Option<ast::Visibility>,
        name: ast::Name,
        generic_param_list: Option<ast::GenericParamList>,
        where_clause: Option<ast::WhereClause>,
        variant_list: ast::VariantList,
    ) -> ast::Enum {
        let ast = make::enum_(
            attrs,
            visibility.clone(),
            name.clone(),
            generic_param_list.clone(),
            where_clause.clone(),
            variant_list.clone(),
        );

        if let Some(mut mapping) = self.mappings() {
            let mut builder = SyntaxMappingBuilder::new(ast.syntax().clone());
            if let Some(visibility) = visibility {
                builder.map_node(
                    visibility.syntax().clone(),
                    ast.visibility().unwrap().syntax().clone(),
                );
            }

            builder.map_node(name.syntax().clone(), ast.name().unwrap().syntax().clone());

            if let Some(generic_param_list) = generic_param_list {
                builder.map_node(
                    generic_param_list.syntax().clone(),
                    ast.generic_param_list().unwrap().syntax().clone(),
                );
            }

            if let Some(where_clause) = where_clause {
                builder.map_node(
                    where_clause.syntax().clone(),
                    ast.where_clause().unwrap().syntax().clone(),
                );
            }

            builder.map_node(
                variant_list.syntax().clone(),
                ast.variant_list().unwrap().syntax().clone(),
            );

            builder.finish(&mut mapping);
        }

        ast
    }

    pub fn variant_list(
        &self,
        variants: impl IntoIterator<Item = ast::Variant>,
    ) -> ast::VariantList {
        let (variants, input) = iterator_input(variants);
        let ast = make::variant_list(variants);

        if let Some(mut mapping) = self.mappings() {
            let mut builder = SyntaxMappingBuilder::new(ast.syntax().clone());

            builder.map_children(input, ast.variants().map(|it| it.syntax().clone()));

            builder.finish(&mut mapping);
        }

        ast
    }

    pub fn variant(
        &self,
        visibility: Option<ast::Visibility>,
        name: ast::Name,
        field_list: Option<ast::FieldList>,
        discriminant: Option<ast::Expr>,
    ) -> ast::Variant {
        let ast = make::variant(
            visibility.clone(),
            name.clone(),
            field_list.clone(),
            discriminant.clone(),
        );

        if let Some(mut mapping) = self.mappings() {
            let mut builder = SyntaxMappingBuilder::new(ast.syntax().clone());
            if let Some(visibility) = visibility {
                builder.map_node(
                    visibility.syntax().clone(),
                    ast.visibility().unwrap().syntax().clone(),
                );
            }

            builder.map_node(name.syntax().clone(), ast.name().unwrap().syntax().clone());

            if let Some(field_list) = field_list {
                builder.map_node(
                    field_list.syntax().clone(),
                    ast.field_list().unwrap().syntax().clone(),
                );
            }

            if let Some(discriminant) = discriminant {
                builder.map_node(
                    discriminant.syntax().clone(),
                    ast.const_arg().unwrap().syntax().clone(),
                );
            }

            builder.finish(&mut mapping);
        }

        ast
    }

    pub fn fn_(
        &self,
        attrs: impl IntoIterator<Item = ast::Attr>,
        visibility: Option<ast::Visibility>,
        fn_name: ast::Name,
        type_params: Option<ast::GenericParamList>,
        where_clause: Option<ast::WhereClause>,
        params: ast::ParamList,
        body: ast::BlockExpr,
        ret_type: Option<ast::RetType>,
        is_async: bool,
        is_const: bool,
        is_unsafe: bool,
        is_gen: bool,
    ) -> ast::Fn {
        let (attrs, input) = iterator_input(attrs);
        let ast = make::fn_(
            attrs,
            visibility.clone(),
            fn_name.clone(),
            type_params.clone(),
            where_clause.clone(),
            params.clone(),
            body.clone(),
            ret_type.clone(),
            is_async,
            is_const,
            is_unsafe,
            is_gen,
        );

        if let Some(mut mapping) = self.mappings() {
            let mut builder = SyntaxMappingBuilder::new(ast.syntax().clone());
            builder.map_children(input, ast.attrs().map(|attr| attr.syntax().clone()));

            if let Some(visibility) = visibility {
                builder.map_node(
                    visibility.syntax().clone(),
                    ast.visibility().unwrap().syntax().clone(),
                );
            }
            builder.map_node(fn_name.syntax().clone(), ast.name().unwrap().syntax().clone());
            if let Some(type_params) = type_params {
                builder.map_node(
                    type_params.syntax().clone(),
                    ast.generic_param_list().unwrap().syntax().clone(),
                );
            }
            if let Some(where_clause) = where_clause {
                builder.map_node(
                    where_clause.syntax().clone(),
                    ast.where_clause().unwrap().syntax().clone(),
                );
            }
            builder.map_node(params.syntax().clone(), ast.param_list().unwrap().syntax().clone());
            builder.map_node(body.syntax().clone(), ast.body().unwrap().syntax().clone());
            if let Some(ret_type) = ret_type {
                builder
                    .map_node(ret_type.syntax().clone(), ast.ret_type().unwrap().syntax().clone());
            }

            builder.finish(&mut mapping);
        }

        ast
    }

    pub fn assoc_item_list(
        &self,
        items: impl IntoIterator<Item = ast::AssocItem>,
    ) -> ast::AssocItemList {
        let (items, input) = iterator_input(items);
        let items_vec: Vec<_> = items.into_iter().collect();
        let ast = make::assoc_item_list(Some(items_vec));

        if let Some(mut mapping) = self.mappings() {
            let mut builder = SyntaxMappingBuilder::new(ast.syntax().clone());
            builder.map_children(input, ast.assoc_items().map(|item| item.syntax().clone()));
            builder.finish(&mut mapping);
        }

        ast
    }

    pub fn assoc_item_list_empty(&self) -> ast::AssocItemList {
        make::assoc_item_list(None)
    }

    pub fn item_list(&self, items: impl IntoIterator<Item = ast::Item>) -> ast::ItemList {
        let (items, input) = iterator_input(items);
        let items_vec: Vec<_> = items.into_iter().collect();
        let ast = make::item_list(Some(items_vec));

        if let Some(mut mapping) = self.mappings() {
            let mut builder = SyntaxMappingBuilder::new(ast.syntax().clone());
            builder.map_children(input, ast.items().map(|item: ast::Item| item.syntax().clone()));
            builder.finish(&mut mapping);
        }

        ast
    }

    pub fn mod_(&self, name: ast::Name, body: Option<ast::ItemList>) -> ast::Module {
        let ast = make::mod_(name.clone(), body.clone());

        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());
            if let Some(body) = body {
                builder.map_node(body.syntax().clone(), ast.item_list().unwrap().syntax().clone());
            }
            builder.finish(&mut mapping);
        }

        ast
    }

    pub fn attr_outer(&self, meta: ast::Meta) -> ast::Attr {
        let ast = make::attr_outer(meta.clone());

        if let Some(mut mapping) = self.mappings() {
            let mut builder = SyntaxMappingBuilder::new(ast.syntax().clone());
            builder.map_node(meta.syntax().clone(), ast.meta().unwrap().syntax().clone());
            builder.finish(&mut mapping);
        }

        ast
    }

    pub fn attr_inner(&self, meta: ast::Meta) -> ast::Attr {
        let ast = make::attr_inner(meta.clone());

        if let Some(mut mapping) = self.mappings() {
            let mut builder = SyntaxMappingBuilder::new(ast.syntax().clone());
            builder.map_node(meta.syntax().clone(), ast.meta().unwrap().syntax().clone());
            builder.finish(&mut mapping);
        }

        ast
    }

    pub fn meta_token_tree(&self, path: ast::Path, tt: ast::TokenTree) -> ast::Meta {
        let ast = make::meta_token_tree(path.clone(), tt.clone());

        if let Some(mut mapping) = self.mappings() {
            let mut builder = SyntaxMappingBuilder::new(ast.syntax().clone());
            builder.map_node(path.syntax().clone(), ast.path().unwrap().syntax().clone());
            let ast::Meta::TokenTreeMeta(meta) = &ast else { unreachable!() };
            builder.map_node(tt.syntax().clone(), meta.token_tree().unwrap().syntax().clone());
            builder.finish(&mut mapping);
        }

        ast
    }

    pub fn cfg_flag(&self, flag: &str) -> ast::CfgPredicate {
        make::cfg_flag(flag)
    }

    pub fn cfg_attr_meta(
        &self,
        predicate: ast::CfgPredicate,
        inner: impl IntoIterator<Item = ast::Meta>,
    ) -> ast::CfgAttrMeta {
        let inner = Vec::from_iter(inner);
        let ast = make::cfg_attr_meta(predicate.clone(), inner.iter().cloned());

        if let Some(mut mapping) = self.mappings() {
            let mut builder = SyntaxMappingBuilder::new(ast.syntax().clone());
            builder.map_node(
                predicate.syntax().clone(),
                ast.cfg_predicate().unwrap().syntax().clone(),
            );
            builder.map_children(
                inner.iter().map(|it| it.syntax().clone()),
                ast.metas().map(|it| it.syntax().clone()),
            );
            builder.finish(&mut mapping);
        }

        ast
    }

    pub fn token_tree(
        &self,
        delimiter: SyntaxKind,
        tt: impl IntoIterator<Item = NodeOrToken<ast::TokenTree, SyntaxToken>>,
    ) -> ast::TokenTree {
        let tt: Vec<_> = tt.into_iter().collect();
        let input: Vec<_> = tt.iter().cloned().filter_map(only_nodes).collect();

        let ast = make::token_tree(delimiter, tt);

        if let Some(mut mapping) = self.mappings() {
            let mut builder = SyntaxMappingBuilder::new(ast.syntax().clone());
            builder.map_children(input, ast.token_trees_and_tokens().filter_map(only_nodes));
            builder.finish(&mut mapping);
        }

        return ast;

        fn only_nodes(element: NodeOrToken<ast::TokenTree, SyntaxToken>) -> Option<SyntaxNode> {
            element.as_node().map(|it| it.syntax().clone())
        }
    }

    pub fn token(&self, kind: SyntaxKind) -> SyntaxToken {
        make::token(kind)
    }

    pub fn whitespace(&self, text: &str) -> SyntaxToken {
        make::tokens::whitespace(text)
    }

    pub fn ident(&self, text: &str) -> SyntaxToken {
        make::tokens::ident(text)
    }

    pub fn mut_self_param(&self) -> ast::SelfParam {
        let ast = make::mut_self_param();

        if let Some(mut mapping) = self.mappings() {
            let builder = SyntaxMappingBuilder::new(ast.syntax().clone());
            builder.finish(&mut mapping);
        }

        ast
    }

    pub fn self_param(&self) -> ast::SelfParam {
        let ast = make::self_param();

        if let Some(mut mapping) = self.mappings() {
            let builder = SyntaxMappingBuilder::new(ast.syntax().clone());
            builder.finish(&mut mapping);
        }

        ast
    }

    pub fn impl_(
        &self,
        attrs: impl IntoIterator<Item = ast::Attr>,
        generic_params: Option<ast::GenericParamList>,
        generic_args: Option<ast::GenericArgList>,
        path_type: ast::Type,
        where_clause: Option<ast::WhereClause>,
        body: Option<ast::AssocItemList>,
    ) -> ast::Impl {
        let (attrs, attrs_input) = iterator_input(attrs);
        let ast = make::impl_(
            attrs,
            generic_params.clone(),
            generic_args.clone(),
            path_type.clone(),
            where_clause.clone(),
            body.clone(),
        );

        if let Some(mut mapping) = self.mappings() {
            let mut builder = SyntaxMappingBuilder::new(ast.syntax().clone());
            builder.map_children(attrs_input, ast.attrs().map(|attr| attr.syntax().clone()));
            if let Some(generic_params) = generic_params {
                builder.map_node(
                    generic_params.syntax().clone(),
                    ast.generic_param_list().unwrap().syntax().clone(),
                );
            }
            builder.map_node(path_type.syntax().clone(), ast.self_ty().unwrap().syntax().clone());
            if let Some(where_clause) = where_clause {
                builder.map_node(
                    where_clause.syntax().clone(),
                    ast.where_clause().unwrap().syntax().clone(),
                );
            }
            if let Some(body) = body {
                builder.map_node(
                    body.syntax().clone(),
                    ast.assoc_item_list().unwrap().syntax().clone(),
                );
            }
            builder.finish(&mut mapping);
        }

        ast
    }

    pub fn trait_(
        &self,
        is_unsafe: bool,
        ident: &str,
        generic_param_list: Option<ast::GenericParamList>,
        where_clause: Option<ast::WhereClause>,
        assoc_items: ast::AssocItemList,
    ) -> ast::Trait {
        let ast = make::trait_(
            is_unsafe,
            ident,
            generic_param_list.clone(),
            where_clause.clone(),
            assoc_items.clone(),
        );

        if let Some(mut mapping) = self.mappings() {
            let mut builder = SyntaxMappingBuilder::new(ast.syntax().clone());
            if let Some(generic_param_list) = generic_param_list {
                builder.map_node(
                    generic_param_list.syntax().clone(),
                    ast.generic_param_list().unwrap().syntax().clone(),
                );
            }
            if let Some(where_clause) = where_clause {
                builder.map_node(
                    where_clause.syntax().clone(),
                    ast.where_clause().unwrap().syntax().clone(),
                );
            }
            builder.map_node(
                assoc_items.syntax().clone(),
                ast.assoc_item_list().unwrap().syntax().clone(),
            );
            builder.finish(&mut mapping);
        }

        ast
    }

    pub fn ret_type(&self, ty: ast::Type) -> ast::RetType {
        let ast = make::ret_type(ty.clone());

        if let Some(mut mapping) = self.mappings() {
            let mut builder = SyntaxMappingBuilder::new(ast.syntax().clone());
            builder.map_node(ty.syntax().clone(), ast.ty().unwrap().syntax().clone());
            builder.finish(&mut mapping);
        }
        ast
    }

    pub fn ty_ref(&self, ty: ast::Type, is_mut: bool) -> ast::Type {
        let ast = make::ty_ref(ty.clone(), is_mut);

        if let Some(mut mapping) = self.mappings() {
            let mut builder = SyntaxMappingBuilder::new(ast.syntax().clone());
            match &ast {
                ast::Type::RefType(ref_ty) => {
                    builder.map_node(ty.syntax().clone(), ref_ty.ty().unwrap().syntax().clone());
                }
                _ => unreachable!(),
            }
            builder.finish(&mut mapping);
        }
        ast
    }

    pub fn field_from_idents<'a>(
        &self,
        parts: impl std::iter::IntoIterator<Item = &'a str>,
    ) -> Option<ast::Expr> {
        make::ext::field_from_idents(parts)
    }

    pub fn ty_name(&self, name: ast::Name) -> ast::Type {
        let ast = make::ext::ty_name(name.clone());

        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()) else { unreachable!() };

        if let Some(mut mapping) = self.mappings() {
            let mut builder = SyntaxMappingBuilder::new(ast.syntax().clone());
            builder.map_node(expr.syntax().clone(), ast.expr().unwrap().syntax().clone());
            builder.finish(&mut mapping);
        }

        ast
    }

    pub fn expr_try(&self, expr: ast::Expr) -> ast::Expr {
        let ast = make::expr_try(expr.clone());

        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());

        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()) else {
            unreachable!()
        };

        if let Some(mut mapping) = self.mappings() {
            let mut builder = SyntaxMappingBuilder::new(ast.syntax().clone());
            if let Some(label) = label {
                builder.map_node(label.syntax().clone(), ast.lifetime().unwrap().syntax().clone());
            }
            if let Some(expr) = expr {
                builder.map_node(expr.syntax().clone(), ast.expr().unwrap().syntax().clone());
            }
            builder.finish(&mut mapping);
        }

        ast
    }

    pub fn expr_continue(&self, label: Option<Lifetime>) -> ast::ContinueExpr {
        let ast::Expr::ContinueExpr(ast) = make::expr_continue(label.clone()) else {
            unreachable!()
        };

        if let Some(mut mapping) = self.mappings() {
            let mut builder = SyntaxMappingBuilder::new(ast.syntax().clone());
            if let Some(label) = label {
                builder.map_node(label.syntax().clone(), ast.lifetime().unwrap().syntax().clone());
            }
            builder.finish(&mut mapping);
        }

        ast
    }
}

// `ext` constructors
impl SyntaxFactory {
    pub fn ident_path(&self, ident: &str) -> ast::Path {
        self.path_unqualified(self.path_segment(self.name_ref(ident)))
    }

    pub fn path_from_idents<'a>(
        &self,
        parts: impl IntoIterator<Item = &'a str>,
    ) -> Option<ast::Path> {
        make::ext::path_from_idents(parts)
    }

    pub fn token_tree_from_node(&self, node: &SyntaxNode) -> ast::TokenTree {
        make::ext::token_tree_from_node(node)
    }

    pub fn expr_unit(&self) -> ast::Expr {
        self.expr_tuple([]).into()
    }

    pub fn ty_option(&self, t: ast::Type) -> ast::PathType {
        let generic_arg_list = self.generic_arg_list([self.type_arg(t).into()], false);
        let path = self.path_unqualified(
            self.path_segment_generics(self.name_ref("Option"), generic_arg_list),
        );

        self.ty_path(path)
    }

    pub fn ty_result(&self, t: ast::Type, e: ast::Type) -> ast::PathType {
        let generic_arg_list =
            self.generic_arg_list([self.type_arg(t).into(), self.type_arg(e).into()], false);
        let path = self.path_unqualified(
            self.path_segment_generics(self.name_ref("Result"), generic_arg_list),
        );

        self.ty_path(path)
    }
}

// We need to collect `input` here instead of taking `impl IntoIterator + Clone`,
// because if we took `impl IntoIterator + Clone`, that could be something like an
// `Iterator::map` with a closure that also makes use of a `SyntaxFactory` constructor.
//
// In that case, the iterator would be evaluated inside of the call to `map_children`,
// and the inner constructor would try to take a mutable borrow of the mappings `RefCell`,
// which would panic since it's already being mutably borrowed in the outer constructor.
fn iterator_input<N: AstNode>(input: impl IntoIterator<Item = N>) -> (Vec<N>, Vec<SyntaxNode>) {
    input
        .into_iter()
        .map(|it| {
            let syntax = it.syntax().clone();
            (it, syntax)
        })
        .collect()
}
