1
1
use std:: borrow:: Cow ;
2
2
3
- use rustc_hash:: FxHashSet ;
4
-
5
3
use crate :: { CowStr , MagicString , TextSize } ;
6
4
5
+ struct ExcludeSet {
6
+ exclude : Vec < [ TextSize ; 2 ] > ,
7
+ }
8
+
9
+ impl ExcludeSet {
10
+ fn new ( exclude : Vec < [ TextSize ; 2 ] > ) -> Self {
11
+ Self { exclude }
12
+ }
13
+
14
+ fn contains ( & self , index : TextSize ) -> bool {
15
+ self . exclude . iter ( ) . any ( |s| s[ 0 ] <= index && index <= s[ 1 ] )
16
+ }
17
+ }
18
+
7
19
pub fn guess_indentor ( source : & str ) -> Option < String > {
8
20
let mut tabbed_count = 0 ;
9
21
let mut spaced_line = vec ! [ ] ;
@@ -100,24 +112,17 @@ impl<'text> MagicString<'text> {
100
112
for intro_frag in self . intro . iter_mut ( ) {
101
113
indent_frag ( intro_frag, & mut indent_replacer)
102
114
}
103
- let is_excluded = {
104
- let mut is_excluded = FxHashSet :: default ( ) ;
105
- let exclude = opts. exclude ;
106
- exclude. iter ( ) . for_each ( |s| {
107
- for i in s[ 0 ] ..s[ 1 ] {
108
- is_excluded. insert ( i) ;
109
- }
110
- } ) ;
111
- is_excluded
112
- } ;
115
+
116
+ let exclude_set = ExcludeSet :: new ( opts. exclude ) ;
117
+
113
118
let mut next_chunk_id = Some ( self . first_chunk_idx ) ;
114
119
let mut char_index = 0u32 ;
115
120
while let Some ( chunk_idx) = next_chunk_id {
116
121
// Make sure the `next_chunk_id` is updated before we split the chunk. Otherwise, we
117
122
// might process the same chunk twice.
118
123
next_chunk_id = self . chunks [ chunk_idx] . next ;
119
124
if let Some ( edited_content) = self . chunks [ chunk_idx] . edited_content . as_mut ( ) {
120
- if !is_excluded . contains ( & char_index) {
125
+ if !exclude_set . contains ( char_index) {
121
126
indent_frag ( edited_content, & mut indent_replacer) ;
122
127
}
123
128
} else {
@@ -127,7 +132,7 @@ impl<'text> MagicString<'text> {
127
132
let chunk_end = chunk. end ( ) ;
128
133
for char in chunk. span . text ( & self . source ) . chars ( ) {
129
134
debug_assert ! ( self . source. is_char_boundary( char_index as usize ) ) ;
130
- if !is_excluded . contains ( & char_index) {
135
+ if !exclude_set . contains ( char_index) {
131
136
if char == '\n' {
132
137
indent_replacer. should_indent_next_char = true ;
133
138
} else if char != '\r' && indent_replacer. should_indent_next_char {
0 commit comments