1+ use std:: borrow:: Cow ;
2+
13use rustc_hash:: FxHashSet ;
24
35use crate :: { CowStr , MagicString , TextSize } ;
@@ -66,57 +68,37 @@ impl<'text> MagicString<'text> {
6668 }
6769 struct IndentReplacer {
6870 should_indent_next_char : bool ,
69- indent_str : String ,
71+ indentor : String ,
7072 }
7173
72- impl regex:: Replacer for & mut & mut IndentReplacer {
73- fn replace_append ( & mut self , caps : & regex:: Captures < ' _ > , dst : & mut String ) {
74- if self . should_indent_next_char {
75- dst. push_str ( & self . indent_str ) ;
76- }
77- for cap in caps. iter ( ) {
78- if let Some ( cap) = cap {
79- dst. push_str ( cap. as_str ( ) ) ;
80- }
81- }
82- }
83- }
8474
8575 fn indent_frag < ' text > (
8676 frag : & mut CowStr < ' text > ,
87- pattern : & regex:: Regex ,
88- mut indent_replacer : & mut IndentReplacer ,
77+ indent_replacer : & mut IndentReplacer ,
8978 ) {
90- match frag {
91- std:: borrow:: Cow :: Borrowed ( str) => {
92- let might_replaced = pattern. replace_all ( str, & mut indent_replacer) ;
93- * frag = might_replaced;
94- }
95- std:: borrow:: Cow :: Owned ( str) => {
96- let might_replaced = pattern. replace_all ( str, & mut indent_replacer) ;
97- match might_replaced {
98- std:: borrow:: Cow :: Owned ( replaced) => {
99- * frag = replaced. into ( ) ;
100- }
101- std:: borrow:: Cow :: Borrowed ( _) => {
102- // Since nothing was replaced, we can just use the original string.
103- }
104- }
79+ let mut indented = String :: new ( ) ;
80+ for char in frag. chars ( ) {
81+ if char == '\n' {
82+ indent_replacer. should_indent_next_char = true ;
83+ } else if char != '\r' && indent_replacer. should_indent_next_char {
84+ indent_replacer. should_indent_next_char = false ;
85+ indented. push_str ( & indent_replacer. indentor ) ;
10586 }
87+ indented. push ( char) ;
10688 }
89+ * frag = Cow :: Owned ( indented) ;
10790 }
10891
10992 let indentor = opts. indentor . unwrap_or_else ( || self . guessed_indentor ( ) ) ;
11093
111- let pattern = regex:: Regex :: new ( r"(?m)^[^\r\n]" ) . unwrap ( ) ;
11294
11395 let mut indent_replacer = IndentReplacer {
11496 should_indent_next_char : true ,
115- indent_str : indentor. to_string ( ) ,
97+ indentor : indentor. to_string ( ) ,
11698 } ;
11799
118100 for intro_frag in self . intro . iter_mut ( ) {
119- indent_frag ( intro_frag, & pattern , & mut indent_replacer)
101+ indent_frag ( intro_frag, & mut indent_replacer)
120102 }
121103 let is_excluded = {
122104 let mut is_excluded = FxHashSet :: default ( ) ;
@@ -136,7 +118,7 @@ impl<'text> MagicString<'text> {
136118 next_chunk_id = self . chunks [ chunk_idx] . next ;
137119 if let Some ( edited_content) = self . chunks [ chunk_idx] . edited_content . as_mut ( ) {
138120 if !is_excluded. contains ( & char_index) {
139- indent_frag ( edited_content, & pattern , & mut indent_replacer) ;
121+ indent_frag ( edited_content, & mut indent_replacer) ;
140122 }
141123 } else {
142124 let chunk = & self . chunks [ chunk_idx] ;
@@ -157,14 +139,14 @@ impl<'text> MagicString<'text> {
157139 char_index += char. len_utf8 ( ) as u32 ;
158140 }
159141 for line_start in line_starts {
160- self . prepend_right ( line_start, indent_replacer. indent_str . clone ( ) ) ;
142+ self . prepend_right ( line_start, indent_replacer. indentor . clone ( ) ) ;
161143 }
162144 char_index = chunk_end;
163145 }
164146 }
165147
166148 for frag in self . outro . iter_mut ( ) {
167- indent_frag ( frag, & pattern , & mut indent_replacer)
149+ indent_frag ( frag, & mut indent_replacer)
168150 }
169151
170152 self
0 commit comments