11//! FIXME: write short doc here
22
3- use std:: {
4- fmt,
5- hash:: BuildHasherDefault ,
6- ops:: { self , RangeInclusive } ,
7- } ;
3+ use std:: { fmt, hash:: BuildHasherDefault , ops:: RangeInclusive } ;
84
95use indexmap:: IndexMap ;
106use itertools:: Itertools ;
@@ -358,107 +354,11 @@ impl fmt::Debug for SyntaxRewriter<'_> {
358354}
359355
360356impl SyntaxRewriter < ' _ > {
361- pub fn delete < T : Clone + Into < SyntaxElement > > ( & mut self , what : & T ) {
362- let what = what. clone ( ) . into ( ) ;
363- let replacement = Replacement :: Delete ;
364- self . replacements . insert ( what, replacement) ;
365- }
366- pub fn insert_before < T : Clone + Into < SyntaxElement > , U : Clone + Into < SyntaxElement > > (
367- & mut self ,
368- before : & T ,
369- what : & U ,
370- ) {
371- let before = before. clone ( ) . into ( ) ;
372- let pos = match before. prev_sibling_or_token ( ) {
373- Some ( sibling) => InsertPos :: After ( sibling) ,
374- None => match before. parent ( ) {
375- Some ( parent) => InsertPos :: FirstChildOf ( parent) ,
376- None => return ,
377- } ,
378- } ;
379- self . insertions . entry ( pos) . or_insert_with ( Vec :: new) . push ( what. clone ( ) . into ( ) ) ;
380- }
381- pub fn insert_after < T : Clone + Into < SyntaxElement > , U : Clone + Into < SyntaxElement > > (
382- & mut self ,
383- after : & T ,
384- what : & U ,
385- ) {
386- self . insertions
387- . entry ( InsertPos :: After ( after. clone ( ) . into ( ) ) )
388- . or_insert_with ( Vec :: new)
389- . push ( what. clone ( ) . into ( ) ) ;
390- }
391- pub fn insert_as_first_child < T : Clone + Into < SyntaxNode > , U : Clone + Into < SyntaxElement > > (
392- & mut self ,
393- parent : & T ,
394- what : & U ,
395- ) {
396- self . insertions
397- . entry ( InsertPos :: FirstChildOf ( parent. clone ( ) . into ( ) ) )
398- . or_insert_with ( Vec :: new)
399- . push ( what. clone ( ) . into ( ) ) ;
400- }
401- pub fn insert_many_before <
402- T : Clone + Into < SyntaxElement > ,
403- U : IntoIterator < Item = SyntaxElement > ,
404- > (
405- & mut self ,
406- before : & T ,
407- what : U ,
408- ) {
409- let before = before. clone ( ) . into ( ) ;
410- let pos = match before. prev_sibling_or_token ( ) {
411- Some ( sibling) => InsertPos :: After ( sibling) ,
412- None => match before. parent ( ) {
413- Some ( parent) => InsertPos :: FirstChildOf ( parent) ,
414- None => return ,
415- } ,
416- } ;
417- self . insertions . entry ( pos) . or_insert_with ( Vec :: new) . extend ( what) ;
418- }
419- pub fn insert_many_after <
420- T : Clone + Into < SyntaxElement > ,
421- U : IntoIterator < Item = SyntaxElement > ,
422- > (
423- & mut self ,
424- after : & T ,
425- what : U ,
426- ) {
427- self . insertions
428- . entry ( InsertPos :: After ( after. clone ( ) . into ( ) ) )
429- . or_insert_with ( Vec :: new)
430- . extend ( what) ;
431- }
432- pub fn insert_many_as_first_children <
433- T : Clone + Into < SyntaxNode > ,
434- U : IntoIterator < Item = SyntaxElement > ,
435- > (
436- & mut self ,
437- parent : & T ,
438- what : U ,
439- ) {
440- self . insertions
441- . entry ( InsertPos :: FirstChildOf ( parent. clone ( ) . into ( ) ) )
442- . or_insert_with ( Vec :: new)
443- . extend ( what)
444- }
445357 pub fn replace < T : Clone + Into < SyntaxElement > > ( & mut self , what : & T , with : & T ) {
446358 let what = what. clone ( ) . into ( ) ;
447359 let replacement = Replacement :: Single ( with. clone ( ) . into ( ) ) ;
448360 self . replacements . insert ( what, replacement) ;
449361 }
450- pub fn replace_with_many < T : Clone + Into < SyntaxElement > > (
451- & mut self ,
452- what : & T ,
453- with : Vec < SyntaxElement > ,
454- ) {
455- let what = what. clone ( ) . into ( ) ;
456- let replacement = Replacement :: Many ( with) ;
457- self . replacements . insert ( what, replacement) ;
458- }
459- pub fn replace_ast < T : AstNode > ( & mut self , what : & T , with : & T ) {
460- self . replace ( what. syntax ( ) , with. syntax ( ) )
461- }
462362
463363 pub fn rewrite ( & self , node : & SyntaxNode ) -> SyntaxNode {
464364 let _p = profile:: span ( "rewrite" ) ;
@@ -534,10 +434,6 @@ impl SyntaxRewriter<'_> {
534434 if let Some ( replacement) = self . replacement ( & element) {
535435 match replacement {
536436 Replacement :: Single ( element) => acc. push ( element_to_green ( element) ) ,
537- Replacement :: Many ( replacements) => {
538- acc. extend ( replacements. into_iter ( ) . map ( element_to_green) )
539- }
540- Replacement :: Delete => ( ) ,
541437 } ;
542438 } else {
543439 match element {
@@ -560,25 +456,9 @@ fn element_to_green(element: SyntaxElement) -> NodeOrToken<rowan::GreenNode, row
560456 }
561457}
562458
563- impl ops:: AddAssign for SyntaxRewriter < ' _ > {
564- fn add_assign ( & mut self , rhs : SyntaxRewriter ) {
565- self . replacements . extend ( rhs. replacements ) ;
566- for ( pos, insertions) in rhs. insertions . into_iter ( ) {
567- match self . insertions . entry ( pos) {
568- indexmap:: map:: Entry :: Occupied ( mut occupied) => {
569- occupied. get_mut ( ) . extend ( insertions)
570- }
571- indexmap:: map:: Entry :: Vacant ( vacant) => drop ( vacant. insert ( insertions) ) ,
572- }
573- }
574- }
575- }
576-
577459#[ derive( Clone , Debug ) ]
578460enum Replacement {
579- Delete ,
580461 Single ( SyntaxElement ) ,
581- Many ( Vec < SyntaxElement > ) ,
582462}
583463
584464fn with_children (
0 commit comments