Skip to content

Commit 401cae6

Browse files
committed
Refactor remove using update_with_inner
1 parent 36044be commit 401cae6

File tree

4 files changed

+91
-39
lines changed

4 files changed

+91
-39
lines changed

src/chunk.rs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,22 @@ use std::collections::VecDeque;
22

33
use crate::{span::Span, ChunkIdx, CowStr, TextSize};
44

5+
#[derive(Debug)]
6+
pub struct EditOptions {
7+
/// `true` will clear the `intro` and `outro` of the [Chunk]
8+
pub overwrite: bool,
9+
pub store_name: bool,
10+
}
11+
12+
impl Default for EditOptions {
13+
fn default() -> Self {
14+
Self {
15+
overwrite: true,
16+
store_name: false,
17+
}
18+
}
19+
}
20+
521
#[derive(Debug, Default)]
622
pub struct Chunk<'str> {
723
pub intro: VecDeque<CowStr<'str>>,
@@ -58,7 +74,7 @@ impl<'str> Chunk<'str> {
5874
let last_slice_span = Span(text_index, self.end());
5975
let mut new_chunk = Chunk::new(last_slice_span);
6076
if self.is_edited() {
61-
new_chunk.edit("".into(), true, false);
77+
new_chunk.edit("".into(), Default::default());
6278
}
6379
std::mem::swap(&mut new_chunk.outro, &mut self.outro);
6480
self.span = first_slice_span;
@@ -79,12 +95,12 @@ impl<'str> Chunk<'str> {
7995
intro_iter.chain(Some(source_frag)).chain(outro_iter)
8096
}
8197

82-
pub fn edit(&mut self, content: CowStr<'str>, overwrite: bool, store_name: bool) {
83-
if overwrite {
98+
pub fn edit(&mut self, content: CowStr<'str>, opts: EditOptions) {
99+
if opts.overwrite {
84100
self.intro.clear();
85101
self.outro.clear();
86102
}
87-
self.store_name = store_name;
103+
self.store_name = opts.store_name;
88104
self.edited_content = Some(content);
89105
}
90106

src/magic_string/mod.rs

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -156,33 +156,7 @@ impl<'s> MagicString<'s> {
156156
ret
157157
}
158158

159-
pub fn remove(&mut self, start: TextSize, end: TextSize) -> &mut Self {
160-
if start == end {
161-
return self;
162-
}
163-
164-
assert!(end <= self.source.len(), "end is out of bounds");
165-
assert!(start < end, "end must be greater than start");
166-
167-
self.split_at(start);
168-
self.split_at(end);
169-
170-
let mut searched = self.chunk_by_start.get(&start).copied();
171-
172-
while let Some(chunk_idx) = searched {
173-
let chunk = &mut self.chunks[chunk_idx];
174-
chunk.intro.clear();
175-
chunk.outro.clear();
176-
chunk.edit("".into(), true, false);
177-
searched = if end == chunk.end() {
178-
None
179-
} else {
180-
self.chunk_by_start.get(&chunk.end()).copied()
181-
}
182-
}
183-
184-
self
185-
}
159+
186160

187161
// --- private
188162

src/magic_string/mutation.rs

Lines changed: 70 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::{CowStr, MagicString, TextSize};
1+
use crate::{chunk::EditOptions, CowStr, MagicString, TextSize};
22

33
#[derive(Debug, Default, Clone)]
44
pub struct UpdateOptions {
@@ -7,8 +7,21 @@ pub struct UpdateOptions {
77
}
88

99
impl<'text> MagicString<'text> {
10-
pub fn update(&mut self, start: TextSize, end: TextSize, content: impl Into<CowStr<'text>>) -> &mut Self {
11-
self.update_with(start, end, content, Default::default())
10+
pub fn update(
11+
&mut self,
12+
start: TextSize,
13+
end: TextSize,
14+
content: impl Into<CowStr<'text>>,
15+
) -> &mut Self {
16+
self.update_with(
17+
start,
18+
end,
19+
content,
20+
UpdateOptions {
21+
store_name: false,
22+
overwrite: true,
23+
},
24+
)
1225
}
1326

1427
pub fn update_with(
@@ -18,17 +31,67 @@ impl<'text> MagicString<'text> {
1831
content: impl Into<CowStr<'text>>,
1932
opts: UpdateOptions,
2033
) -> &mut Self {
34+
self.update_with_inner(start, end, content.into(), opts, true);
35+
self
36+
}
37+
38+
pub fn remove(&mut self, start: TextSize, end: TextSize) -> &mut Self {
39+
self.update_with_inner(
40+
start,
41+
end,
42+
"".into(),
43+
UpdateOptions {
44+
store_name: false,
45+
overwrite: true,
46+
},
47+
false,
48+
);
49+
50+
self
51+
}
52+
53+
// --- private
54+
55+
fn update_with_inner(
56+
&mut self,
57+
start: TextSize,
58+
end: TextSize,
59+
content: CowStr<'text>,
60+
opts: UpdateOptions,
61+
panic_if_start_equal_end: bool,
62+
) -> &mut Self {
63+
if panic_if_start_equal_end && start == end {
64+
panic!(
65+
"Cannot overwrite a zero-length range – use append_left or prepend_right instead"
66+
)
67+
}
2168
assert!(start < end);
2269
self.split_at(start);
2370
self.split_at(end);
2471

25-
let first = self.chunk_by_start.get(&start).unwrap();
26-
let end = self.chunk_by_end.get(&start).unwrap();
72+
let start_idx = self.chunk_by_start.get(&start).copied().unwrap();
73+
let end_idx = self.chunk_by_end.get(&end).copied().unwrap();
2774

28-
// let chunk_after_next =
75+
let start_chunk = &mut self.chunks[start_idx];
76+
start_chunk.edit(
77+
content.into(),
78+
EditOptions {
79+
overwrite: opts.overwrite,
80+
store_name: opts.store_name,
81+
},
82+
);
2983

30-
self.chunks[*first].edit(content.into(), opts.overwrite, opts.store_name);
84+
let mut rest_chunk_idx = if start_idx != end_idx {
85+
start_chunk.next.unwrap()
86+
} else {
87+
return self;
88+
};
3189

90+
while rest_chunk_idx != end_idx {
91+
let rest_chunk = &mut self.chunks[rest_chunk_idx];
92+
rest_chunk.edit("".into(), Default::default());
93+
rest_chunk_idx = rest_chunk.next.unwrap();
94+
}
3295
self
3396
}
3497
}

src/source_map.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ pub struct SourceMap {
1010
}
1111

1212
impl SourceMap {
13-
/// ## Generate SourceMap in JSON format
1413
pub fn to_string(&self) -> String {
1514
serde_json::to_string(self).unwrap()
1615
}

0 commit comments

Comments
 (0)