11//! Wrappers over [`make`] constructors
22use crate :: {
3- ast:: { self , make, HasGenericParams , HasName , HasTypeBounds , HasVisibility } ,
3+ ast:: { self , make, HasGenericArgs , HasGenericParams , HasName , HasTypeBounds , HasVisibility } ,
44 syntax_editor:: SyntaxMappingBuilder ,
55 AstNode , NodeOrToken , SyntaxKind , SyntaxNode , SyntaxToken ,
66} ;
@@ -12,6 +12,10 @@ impl SyntaxFactory {
1212 make:: name ( name) . clone_for_update ( )
1313 }
1414
15+ pub fn name_ref ( & self , name : & str ) -> ast:: NameRef {
16+ make:: name_ref ( name) . clone_for_update ( )
17+ }
18+
1519 pub fn ty ( & self , text : & str ) -> ast:: Type {
1620 make:: ty ( text) . clone_for_update ( )
1721 }
@@ -46,6 +50,71 @@ impl SyntaxFactory {
4650 ast
4751 }
4852
53+ pub fn path_segment ( & self , name_ref : ast:: NameRef ) -> ast:: PathSegment {
54+ let ast = make:: path_segment ( name_ref. clone ( ) ) . clone_for_update ( ) ;
55+
56+ if let Some ( mut mapping) = self . mappings ( ) {
57+ let mut builder = SyntaxMappingBuilder :: new ( ast. syntax ( ) . clone ( ) ) ;
58+ builder. map_node ( name_ref. syntax ( ) . clone ( ) , ast. name_ref ( ) . unwrap ( ) . syntax ( ) . clone ( ) ) ;
59+ builder. finish ( & mut mapping) ;
60+ }
61+
62+ ast
63+ }
64+
65+ pub fn path_segment_generics (
66+ & self ,
67+ name_ref : ast:: NameRef ,
68+ generic_arg_list : ast:: GenericArgList ,
69+ ) -> ast:: PathSegment {
70+ let ast:: Type :: PathType ( path) = make:: ty ( & format ! ( "{name_ref}{generic_arg_list}" ) ) else {
71+ unreachable ! ( ) ;
72+ } ;
73+
74+ let ast = path. path ( ) . unwrap ( ) . segment ( ) . unwrap ( ) . clone_for_update ( ) ;
75+
76+ if let Some ( mut mapping) = self . mappings ( ) {
77+ let mut builder = SyntaxMappingBuilder :: new ( ast. syntax ( ) . clone ( ) ) ;
78+ builder. map_node ( name_ref. syntax ( ) . clone ( ) , ast. name_ref ( ) . unwrap ( ) . syntax ( ) . clone ( ) ) ;
79+ builder. map_node (
80+ generic_arg_list. syntax ( ) . clone ( ) ,
81+ ast. generic_arg_list ( ) . unwrap ( ) . syntax ( ) . clone ( ) ,
82+ ) ;
83+ builder. finish ( & mut mapping) ;
84+ }
85+
86+ ast
87+ }
88+
89+ pub fn path_unqualified ( & self , segment : ast:: PathSegment ) -> ast:: Path {
90+ let ast = make:: path_unqualified ( segment. clone ( ) ) . clone_for_update ( ) ;
91+
92+ if let Some ( mut mapping) = self . mappings ( ) {
93+ let mut builder = SyntaxMappingBuilder :: new ( ast. syntax ( ) . clone ( ) ) ;
94+ builder. map_node ( segment. syntax ( ) . clone ( ) , ast. segment ( ) . unwrap ( ) . syntax ( ) . clone ( ) ) ;
95+ builder. finish ( & mut mapping) ;
96+ }
97+
98+ ast
99+ }
100+
101+ pub fn path_from_segments (
102+ & self ,
103+ segments : impl IntoIterator < Item = ast:: PathSegment > ,
104+ is_abs : bool ,
105+ ) -> ast:: Path {
106+ let ( segments, input) = iterator_input ( segments) ;
107+ let ast = make:: path_from_segments ( segments, is_abs) . clone_for_update ( ) ;
108+
109+ if let Some ( mut mapping) = self . mappings ( ) {
110+ let mut builder = SyntaxMappingBuilder :: new ( ast. syntax ( ) . clone ( ) ) ;
111+ builder. map_children ( input. into_iter ( ) , ast. segments ( ) . map ( |it| it. syntax ( ) . clone ( ) ) ) ;
112+ builder. finish ( & mut mapping) ;
113+ }
114+
115+ ast
116+ }
117+
49118 pub fn ident_pat ( & self , ref_ : bool , mut_ : bool , name : ast:: Name ) -> ast:: IdentPat {
50119 let ast = make:: ident_pat ( ref_, mut_, name. clone ( ) ) . clone_for_update ( ) ;
51120
@@ -508,6 +577,13 @@ impl SyntaxFactory {
508577 }
509578}
510579
580+ // `ext` constructors
581+ impl SyntaxFactory {
582+ pub fn ident_path ( & self , ident : & str ) -> ast:: Path {
583+ self . path_unqualified ( self . path_segment ( self . name_ref ( ident) ) )
584+ }
585+ }
586+
511587// We need to collect `input` here instead of taking `impl IntoIterator + Clone`,
512588// because if we took `impl IntoIterator + Clone`, that could be something like an
513589// `Iterator::map` with a closure that also makes use of a `SyntaxFactory` constructor.
0 commit comments