@@ -100,6 +100,10 @@ impl SyntaxEditor {
100
100
pub fn finish ( self ) -> SyntaxEdit {
101
101
edit_algo:: apply_edits ( self )
102
102
}
103
+
104
+ pub fn add_mappings ( & mut self , other : SyntaxMapping ) {
105
+ self . mappings . merge ( other) ;
106
+ }
103
107
}
104
108
105
109
/// Represents a completed [`SyntaxEditor`] operation.
@@ -319,85 +323,14 @@ fn is_ancestor_or_self_of_element(node: &SyntaxElement, ancestor: &SyntaxNode) -
319
323
#[ cfg( test) ]
320
324
mod tests {
321
325
use expect_test:: expect;
322
- use itertools:: Itertools ;
323
326
324
327
use crate :: {
325
- ast:: { self , make, HasName } ,
328
+ ast:: { self , make, syntax_factory :: SyntaxFactory } ,
326
329
AstNode ,
327
330
} ;
328
331
329
332
use super :: * ;
330
333
331
- fn make_ident_pat (
332
- editor : Option < & mut SyntaxEditor > ,
333
- ref_ : bool ,
334
- mut_ : bool ,
335
- name : ast:: Name ,
336
- ) -> ast:: IdentPat {
337
- let ast = make:: ident_pat ( ref_, mut_, name. clone ( ) ) . clone_for_update ( ) ;
338
-
339
- if let Some ( editor) = editor {
340
- let mut mapping = SyntaxMappingBuilder :: new ( ast. syntax ( ) . clone ( ) ) ;
341
- mapping. map_node ( name. syntax ( ) . clone ( ) , ast. name ( ) . unwrap ( ) . syntax ( ) . clone ( ) ) ;
342
- mapping. finish ( editor) ;
343
- }
344
-
345
- ast
346
- }
347
-
348
- fn make_let_stmt (
349
- editor : Option < & mut SyntaxEditor > ,
350
- pattern : ast:: Pat ,
351
- ty : Option < ast:: Type > ,
352
- initializer : Option < ast:: Expr > ,
353
- ) -> ast:: LetStmt {
354
- let ast =
355
- make:: let_stmt ( pattern. clone ( ) , ty. clone ( ) , initializer. clone ( ) ) . clone_for_update ( ) ;
356
-
357
- if let Some ( editor) = editor {
358
- let mut mapping = SyntaxMappingBuilder :: new ( ast. syntax ( ) . clone ( ) ) ;
359
- mapping. map_node ( pattern. syntax ( ) . clone ( ) , ast. pat ( ) . unwrap ( ) . syntax ( ) . clone ( ) ) ;
360
- if let Some ( input) = ty {
361
- mapping. map_node ( input. syntax ( ) . clone ( ) , ast. ty ( ) . unwrap ( ) . syntax ( ) . clone ( ) ) ;
362
- }
363
- if let Some ( input) = initializer {
364
- mapping
365
- . map_node ( input. syntax ( ) . clone ( ) , ast. initializer ( ) . unwrap ( ) . syntax ( ) . clone ( ) ) ;
366
- }
367
- mapping. finish ( editor) ;
368
- }
369
-
370
- ast
371
- }
372
-
373
- fn make_block_expr (
374
- editor : Option < & mut SyntaxEditor > ,
375
- stmts : impl IntoIterator < Item = ast:: Stmt > ,
376
- tail_expr : Option < ast:: Expr > ,
377
- ) -> ast:: BlockExpr {
378
- let stmts = stmts. into_iter ( ) . collect_vec ( ) ;
379
- let input = stmts. iter ( ) . map ( |it| it. syntax ( ) . clone ( ) ) . collect_vec ( ) ;
380
-
381
- let ast = make:: block_expr ( stmts, tail_expr. clone ( ) ) . clone_for_update ( ) ;
382
-
383
- if let Some ( ( editor, stmt_list) ) = editor. zip ( ast. stmt_list ( ) ) {
384
- let mut mapping = SyntaxMappingBuilder :: new ( stmt_list. syntax ( ) . clone ( ) ) ;
385
-
386
- mapping. map_children (
387
- input. into_iter ( ) ,
388
- stmt_list. statements ( ) . map ( |it| it. syntax ( ) . clone ( ) ) ,
389
- ) ;
390
-
391
- if let Some ( ( input, output) ) = tail_expr. zip ( stmt_list. tail_expr ( ) ) {
392
- mapping. map_node ( input. syntax ( ) . clone ( ) , output. syntax ( ) . clone ( ) ) ;
393
- }
394
-
395
- mapping. finish ( editor) ;
396
- }
397
-
398
- ast
399
- }
400
-
401
334
#[ test]
402
335
fn basic_usage ( ) {
403
336
let root = make:: match_arm (
@@ -417,6 +350,7 @@ mod tests {
417
350
let to_replace = root. syntax ( ) . descendants ( ) . find_map ( ast:: BinExpr :: cast) . unwrap ( ) ;
418
351
419
352
let mut editor = SyntaxEditor :: new ( root. syntax ( ) . clone ( ) ) ;
353
+ let make = SyntaxFactory :: new ( ) ;
420
354
421
355
let name = make:: name ( "var_name" ) ;
422
356
let name_ref = make:: name_ref ( "var_name" ) . clone_for_update ( ) ;
@@ -425,21 +359,20 @@ mod tests {
425
359
editor. add_annotation ( name. syntax ( ) , placeholder_snippet) ;
426
360
editor. add_annotation ( name_ref. syntax ( ) , placeholder_snippet) ;
427
361
428
- let make_ident_pat = make_ident_pat ( Some ( & mut editor) , false , false , name) ;
429
- let make_let_stmt = make_let_stmt (
430
- Some ( & mut editor) ,
431
- make_ident_pat. into ( ) ,
432
- None ,
433
- Some ( to_replace. clone ( ) . into ( ) ) ,
434
- ) ;
435
- let new_block = make_block_expr (
436
- Some ( & mut editor) ,
437
- [ make_let_stmt. into ( ) ] ,
362
+ let new_block = make. block_expr (
363
+ [ make
364
+ . let_stmt (
365
+ make. ident_pat ( false , false , name. clone ( ) ) . into ( ) ,
366
+ None ,
367
+ Some ( to_replace. clone ( ) . into ( ) ) ,
368
+ )
369
+ . into ( ) ] ,
438
370
Some ( to_wrap. clone ( ) . into ( ) ) ,
439
371
) ;
440
372
441
373
editor. replace ( to_replace. syntax ( ) , name_ref. syntax ( ) ) ;
442
374
editor. replace ( to_wrap. syntax ( ) , new_block. syntax ( ) ) ;
375
+ editor. add_mappings ( make. finish_with_mappings ( ) ) ;
443
376
444
377
let edit = editor. finish ( ) ;
445
378
@@ -473,11 +406,11 @@ mod tests {
473
406
let second_let = root. syntax ( ) . descendants ( ) . find_map ( ast:: LetStmt :: cast) . unwrap ( ) ;
474
407
475
408
let mut editor = SyntaxEditor :: new ( root. syntax ( ) . clone ( ) ) ;
409
+ let make = SyntaxFactory :: without_mappings ( ) ;
476
410
477
411
editor. insert (
478
412
Position :: first_child_of ( root. stmt_list ( ) . unwrap ( ) . syntax ( ) ) ,
479
- make_let_stmt (
480
- None ,
413
+ make. let_stmt (
481
414
make:: ext:: simple_ident_pat ( make:: name ( "first" ) ) . into ( ) ,
482
415
None ,
483
416
Some ( make:: expr_literal ( "1" ) . into ( ) ) ,
@@ -487,8 +420,7 @@ mod tests {
487
420
488
421
editor. insert (
489
422
Position :: after ( second_let. syntax ( ) ) ,
490
- make_let_stmt (
491
- None ,
423
+ make. let_stmt (
492
424
make:: ext:: simple_ident_pat ( make:: name ( "third" ) ) . into ( ) ,
493
425
None ,
494
426
Some ( make:: expr_literal ( "3" ) . into ( ) ) ,
@@ -528,19 +460,17 @@ mod tests {
528
460
let second_let = root. syntax ( ) . descendants ( ) . find_map ( ast:: LetStmt :: cast) . unwrap ( ) ;
529
461
530
462
let mut editor = SyntaxEditor :: new ( root. syntax ( ) . clone ( ) ) ;
463
+ let make = SyntaxFactory :: new ( ) ;
531
464
532
- let new_block_expr =
533
- make_block_expr ( Some ( & mut editor) , [ ] , Some ( ast:: Expr :: BlockExpr ( inner_block. clone ( ) ) ) ) ;
465
+ let new_block_expr = make. block_expr ( [ ] , Some ( ast:: Expr :: BlockExpr ( inner_block. clone ( ) ) ) ) ;
534
466
535
- let first_let = make_let_stmt (
536
- Some ( & mut editor) ,
467
+ let first_let = make. let_stmt (
537
468
make:: ext:: simple_ident_pat ( make:: name ( "first" ) ) . into ( ) ,
538
469
None ,
539
470
Some ( make:: expr_literal ( "1" ) . into ( ) ) ,
540
471
) ;
541
472
542
- let third_let = make_let_stmt (
543
- Some ( & mut editor) ,
473
+ let third_let = make. let_stmt (
544
474
make:: ext:: simple_ident_pat ( make:: name ( "third" ) ) . into ( ) ,
545
475
None ,
546
476
Some ( make:: expr_literal ( "3" ) . into ( ) ) ,
@@ -552,6 +482,7 @@ mod tests {
552
482
) ;
553
483
editor. insert ( Position :: after ( second_let. syntax ( ) ) , third_let. syntax ( ) ) ;
554
484
editor. replace ( inner_block. syntax ( ) , new_block_expr. syntax ( ) ) ;
485
+ editor. add_mappings ( make. finish_with_mappings ( ) ) ;
555
486
556
487
let edit = editor. finish ( ) ;
557
488
@@ -581,12 +512,11 @@ mod tests {
581
512
let inner_block = root. clone ( ) ;
582
513
583
514
let mut editor = SyntaxEditor :: new ( root. syntax ( ) . clone ( ) ) ;
515
+ let make = SyntaxFactory :: new ( ) ;
584
516
585
- let new_block_expr =
586
- make_block_expr ( Some ( & mut editor) , [ ] , Some ( ast:: Expr :: BlockExpr ( inner_block. clone ( ) ) ) ) ;
517
+ let new_block_expr = make. block_expr ( [ ] , Some ( ast:: Expr :: BlockExpr ( inner_block. clone ( ) ) ) ) ;
587
518
588
- let first_let = make_let_stmt (
589
- Some ( & mut editor) ,
519
+ let first_let = make. let_stmt (
590
520
make:: ext:: simple_ident_pat ( make:: name ( "first" ) ) . into ( ) ,
591
521
None ,
592
522
Some ( make:: expr_literal ( "1" ) . into ( ) ) ,
@@ -597,6 +527,7 @@ mod tests {
597
527
first_let. syntax ( ) ,
598
528
) ;
599
529
editor. replace ( inner_block. syntax ( ) , new_block_expr. syntax ( ) ) ;
530
+ editor. add_mappings ( make. finish_with_mappings ( ) ) ;
600
531
601
532
let edit = editor. finish ( ) ;
602
533
0 commit comments