Skip to content

Commit b73870d

Browse files
committed
feat: introduce ExcludeSet
1 parent 78367b7 commit b73870d

File tree

1 file changed

+19
-14
lines changed

1 file changed

+19
-14
lines changed

src/magic_string/indent.rs

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,21 @@
11
use std::borrow::Cow;
22

3-
use rustc_hash::FxHashSet;
4-
53
use crate::{CowStr, MagicString, TextSize};
64

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+
719
pub fn guess_indentor(source: &str) -> Option<String> {
820
let mut tabbed_count = 0;
921
let mut spaced_line = vec![];
@@ -100,24 +112,17 @@ impl<'text> MagicString<'text> {
100112
for intro_frag in self.intro.iter_mut() {
101113
indent_frag(intro_frag, &mut indent_replacer)
102114
}
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+
113118
let mut next_chunk_id = Some(self.first_chunk_idx);
114119
let mut char_index = 0u32;
115120
while let Some(chunk_idx) = next_chunk_id {
116121
// Make sure the `next_chunk_id` is updated before we split the chunk. Otherwise, we
117122
// might process the same chunk twice.
118123
next_chunk_id = self.chunks[chunk_idx].next;
119124
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) {
121126
indent_frag(edited_content, &mut indent_replacer);
122127
}
123128
} else {
@@ -127,7 +132,7 @@ impl<'text> MagicString<'text> {
127132
let chunk_end = chunk.end();
128133
for char in chunk.span.text(&self.source).chars() {
129134
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) {
131136
if char == '\n' {
132137
indent_replacer.should_indent_next_char = true;
133138
} else if char != '\r' && indent_replacer.should_indent_next_char {

0 commit comments

Comments
 (0)