|
2 | 2 | use itertools::Itertools;
|
3 | 3 |
|
4 | 4 | use crate::{
|
5 |
| - ast::{self, make, HasName, HasTypeBounds}, |
| 5 | + ast::{self, make, HasGenericParams, HasName, HasTypeBounds, HasVisibility}, |
6 | 6 | syntax_editor::SyntaxMappingBuilder,
|
7 | 7 | AstNode, NodeOrToken, SyntaxKind, SyntaxNode, SyntaxToken,
|
8 | 8 | };
|
@@ -169,6 +169,211 @@ impl SyntaxFactory {
|
169 | 169 | ast
|
170 | 170 | }
|
171 | 171 |
|
| 172 | + pub fn record_field_list( |
| 173 | + &self, |
| 174 | + fields: impl IntoIterator<Item = ast::RecordField>, |
| 175 | + ) -> ast::RecordFieldList { |
| 176 | + let fields: Vec<ast::RecordField> = fields.into_iter().collect(); |
| 177 | + let input: Vec<_> = fields.iter().map(|it| it.syntax().clone()).collect(); |
| 178 | + let ast = make::record_field_list(fields).clone_for_update(); |
| 179 | + |
| 180 | + if let Some(mut mapping) = self.mappings() { |
| 181 | + let mut builder = SyntaxMappingBuilder::new(ast.syntax().clone()); |
| 182 | + |
| 183 | + builder.map_children(input.into_iter(), ast.fields().map(|it| it.syntax().clone())); |
| 184 | + |
| 185 | + builder.finish(&mut mapping); |
| 186 | + } |
| 187 | + |
| 188 | + ast |
| 189 | + } |
| 190 | + |
| 191 | + pub fn record_field( |
| 192 | + &self, |
| 193 | + visibility: Option<ast::Visibility>, |
| 194 | + name: ast::Name, |
| 195 | + ty: ast::Type, |
| 196 | + ) -> ast::RecordField { |
| 197 | + let ast = |
| 198 | + make::record_field(visibility.clone(), name.clone(), ty.clone()).clone_for_update(); |
| 199 | + |
| 200 | + if let Some(mut mapping) = self.mappings() { |
| 201 | + let mut builder = SyntaxMappingBuilder::new(ast.syntax().clone()); |
| 202 | + if let Some(visibility) = visibility { |
| 203 | + builder.map_node( |
| 204 | + visibility.syntax().clone(), |
| 205 | + ast.visibility().unwrap().syntax().clone(), |
| 206 | + ); |
| 207 | + } |
| 208 | + |
| 209 | + builder.map_node(name.syntax().clone(), ast.name().unwrap().syntax().clone()); |
| 210 | + builder.map_node(ty.syntax().clone(), ast.ty().unwrap().syntax().clone()); |
| 211 | + |
| 212 | + builder.finish(&mut mapping); |
| 213 | + } |
| 214 | + |
| 215 | + ast |
| 216 | + } |
| 217 | + |
| 218 | + pub fn tuple_field_list( |
| 219 | + &self, |
| 220 | + fields: impl IntoIterator<Item = ast::TupleField>, |
| 221 | + ) -> ast::TupleFieldList { |
| 222 | + let fields: Vec<ast::TupleField> = fields.into_iter().collect(); |
| 223 | + let input: Vec<_> = fields.iter().map(|it| it.syntax().clone()).collect(); |
| 224 | + let ast = make::tuple_field_list(fields).clone_for_update(); |
| 225 | + |
| 226 | + if let Some(mut mapping) = self.mappings() { |
| 227 | + let mut builder = SyntaxMappingBuilder::new(ast.syntax().clone()); |
| 228 | + |
| 229 | + builder.map_children(input.into_iter(), ast.fields().map(|it| it.syntax().clone())); |
| 230 | + |
| 231 | + builder.finish(&mut mapping); |
| 232 | + } |
| 233 | + |
| 234 | + ast |
| 235 | + } |
| 236 | + |
| 237 | + pub fn tuple_field( |
| 238 | + &self, |
| 239 | + visibility: Option<ast::Visibility>, |
| 240 | + ty: ast::Type, |
| 241 | + ) -> ast::TupleField { |
| 242 | + let ast = make::tuple_field(visibility.clone(), ty.clone()).clone_for_update(); |
| 243 | + |
| 244 | + if let Some(mut mapping) = self.mappings() { |
| 245 | + let mut builder = SyntaxMappingBuilder::new(ast.syntax().clone()); |
| 246 | + if let Some(visibility) = visibility { |
| 247 | + builder.map_node( |
| 248 | + visibility.syntax().clone(), |
| 249 | + ast.visibility().unwrap().syntax().clone(), |
| 250 | + ); |
| 251 | + } |
| 252 | + |
| 253 | + builder.map_node(ty.syntax().clone(), ast.ty().unwrap().syntax().clone()); |
| 254 | + |
| 255 | + builder.finish(&mut mapping); |
| 256 | + } |
| 257 | + |
| 258 | + ast |
| 259 | + } |
| 260 | + |
| 261 | + pub fn item_enum( |
| 262 | + &self, |
| 263 | + visibility: Option<ast::Visibility>, |
| 264 | + name: ast::Name, |
| 265 | + generic_param_list: Option<ast::GenericParamList>, |
| 266 | + where_clause: Option<ast::WhereClause>, |
| 267 | + variant_list: ast::VariantList, |
| 268 | + ) -> ast::Enum { |
| 269 | + let ast = make::enum_( |
| 270 | + visibility.clone(), |
| 271 | + name.clone(), |
| 272 | + generic_param_list.clone(), |
| 273 | + where_clause.clone(), |
| 274 | + variant_list.clone(), |
| 275 | + ) |
| 276 | + .clone_for_update(); |
| 277 | + |
| 278 | + if let Some(mut mapping) = self.mappings() { |
| 279 | + let mut builder = SyntaxMappingBuilder::new(ast.syntax().clone()); |
| 280 | + if let Some(visibility) = visibility { |
| 281 | + builder.map_node( |
| 282 | + visibility.syntax().clone(), |
| 283 | + ast.visibility().unwrap().syntax().clone(), |
| 284 | + ); |
| 285 | + } |
| 286 | + |
| 287 | + builder.map_node(name.syntax().clone(), ast.name().unwrap().syntax().clone()); |
| 288 | + |
| 289 | + if let Some(generic_param_list) = generic_param_list { |
| 290 | + builder.map_node( |
| 291 | + generic_param_list.syntax().clone(), |
| 292 | + ast.generic_param_list().unwrap().syntax().clone(), |
| 293 | + ); |
| 294 | + } |
| 295 | + |
| 296 | + if let Some(where_clause) = where_clause { |
| 297 | + builder.map_node( |
| 298 | + where_clause.syntax().clone(), |
| 299 | + ast.where_clause().unwrap().syntax().clone(), |
| 300 | + ); |
| 301 | + } |
| 302 | + |
| 303 | + builder.map_node( |
| 304 | + variant_list.syntax().clone(), |
| 305 | + ast.variant_list().unwrap().syntax().clone(), |
| 306 | + ); |
| 307 | + |
| 308 | + builder.finish(&mut mapping); |
| 309 | + } |
| 310 | + |
| 311 | + ast |
| 312 | + } |
| 313 | + |
| 314 | + pub fn variant_list( |
| 315 | + &self, |
| 316 | + variants: impl IntoIterator<Item = ast::Variant>, |
| 317 | + ) -> ast::VariantList { |
| 318 | + let variants: Vec<ast::Variant> = variants.into_iter().collect(); |
| 319 | + let input: Vec<_> = variants.iter().map(|it| it.syntax().clone()).collect(); |
| 320 | + let ast = make::variant_list(variants).clone_for_update(); |
| 321 | + |
| 322 | + if let Some(mut mapping) = self.mappings() { |
| 323 | + let mut builder = SyntaxMappingBuilder::new(ast.syntax().clone()); |
| 324 | + |
| 325 | + builder.map_children(input.into_iter(), ast.variants().map(|it| it.syntax().clone())); |
| 326 | + |
| 327 | + builder.finish(&mut mapping); |
| 328 | + } |
| 329 | + |
| 330 | + ast |
| 331 | + } |
| 332 | + |
| 333 | + pub fn variant( |
| 334 | + &self, |
| 335 | + visibility: Option<ast::Visibility>, |
| 336 | + name: ast::Name, |
| 337 | + field_list: Option<ast::FieldList>, |
| 338 | + discriminant: Option<ast::Expr>, |
| 339 | + ) -> ast::Variant { |
| 340 | + let ast = make::variant( |
| 341 | + visibility.clone(), |
| 342 | + name.clone(), |
| 343 | + field_list.clone(), |
| 344 | + discriminant.clone(), |
| 345 | + ) |
| 346 | + .clone_for_update(); |
| 347 | + |
| 348 | + if let Some(mut mapping) = self.mappings() { |
| 349 | + let mut builder = SyntaxMappingBuilder::new(ast.syntax().clone()); |
| 350 | + if let Some(visibility) = visibility { |
| 351 | + builder.map_node( |
| 352 | + visibility.syntax().clone(), |
| 353 | + ast.visibility().unwrap().syntax().clone(), |
| 354 | + ); |
| 355 | + } |
| 356 | + |
| 357 | + builder.map_node(name.syntax().clone(), ast.name().unwrap().syntax().clone()); |
| 358 | + |
| 359 | + if let Some(field_list) = field_list { |
| 360 | + builder.map_node( |
| 361 | + field_list.syntax().clone(), |
| 362 | + ast.field_list().unwrap().syntax().clone(), |
| 363 | + ); |
| 364 | + } |
| 365 | + |
| 366 | + if let Some(discriminant) = discriminant { |
| 367 | + builder |
| 368 | + .map_node(discriminant.syntax().clone(), ast.expr().unwrap().syntax().clone()); |
| 369 | + } |
| 370 | + |
| 371 | + builder.finish(&mut mapping); |
| 372 | + } |
| 373 | + |
| 374 | + ast |
| 375 | + } |
| 376 | + |
172 | 377 | pub fn token_tree(
|
173 | 378 | &self,
|
174 | 379 | delimiter: SyntaxKind,
|
|
0 commit comments