Skip to content

Commit b497528

Browse files
committed
refactor: change exclude api of indent
1 parent 0b92259 commit b497528

File tree

3 files changed

+14
-11
lines changed

3 files changed

+14
-11
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "string_wizard"
3-
version = "0.0.12"
3+
version = "0.0.13"
44
edition = "2021"
55
license = "MIT"
66
description = "manipulate string like wizards"

src/magic_string/indent.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,18 @@ pub fn guess_indentor(source: &str) -> Option<String> {
3838
pub struct IndentOptions<'a> {
3939
/// MagicString will guess the `indentor`` from lines of the source if passed `None`.
4040
pub indentor: Option<&'a str>,
41-
pub exclude: Vec<TextSize>,
41+
42+
/// The reason I use `[u32; 2]` instead of `(u32, u32)` to represent a range of text is that
43+
/// I want to emphasize that the `[u32; 2]` is the closed interval, which means both the start
44+
/// and the end are included in the range.
45+
pub exclude: Vec<[TextSize; 2]>,
4246
}
4347

4448
impl<'text> MagicString<'text> {
4549
pub fn guessed_indentor(&mut self) -> &str {
46-
let guessed_indentor = self.guessed_indentor.get_or_init(|| guess_indentor(&self.source).unwrap_or_else(|| "\t".to_string()));
50+
let guessed_indentor = self
51+
.guessed_indentor
52+
.get_or_init(|| guess_indentor(&self.source).unwrap_or_else(|| "\t".to_string()));
4753
guessed_indentor
4854
}
4955

@@ -54,7 +60,6 @@ impl<'text> MagicString<'text> {
5460
})
5561
}
5662

57-
5863
pub fn indent_with(&mut self, opts: IndentOptions<'_>) -> &mut Self {
5964
if opts.indentor.map_or(false, |s| s.is_empty()) {
6065
return self;
@@ -115,9 +120,8 @@ impl<'text> MagicString<'text> {
115120
}
116121
let is_excluded = {
117122
let mut is_excluded = FxHashSet::default();
118-
let mut exclude = opts.exclude;
119-
exclude.sort();
120-
exclude.windows(2).for_each(|s| {
123+
let exclude = opts.exclude;
124+
exclude.iter().for_each(|s| {
121125
for i in s[0]..s[1] {
122126
is_excluded.insert(i);
123127
}

tests/magic_string.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,13 @@ impl<'text> MagicStringExt<'text> for MagicString<'text> {
3333
},
3434
)
3535
}
36-
/// Shortcut for `indent_with(IndentOptions { indent_str: Some(indent_str), ..Default::default() })`
36+
/// Shortcut for `indent_with(IndentOptions { indent_str: Some(indent_str), ..Default::default() })`
3737
fn indent_str(&mut self, indent_str: &str) -> &mut Self {
3838
self.indent_with(IndentOptions {
3939
indentor: Some(indent_str),
4040
..Default::default()
4141
})
4242
}
43-
4443
}
4544

4645
mod options {
@@ -341,12 +340,12 @@ mod indent {
341340
let mut s = MagicString::new("abc\ndef\nghi\njkl");
342341
s.indent_with(IndentOptions {
343342
indentor: Some(" "),
344-
exclude: vec![7, 15],
343+
exclude: vec![[7, 15]],
345344
});
346345
assert_eq!(s.to_string(), " abc\n def\nghi\njkl");
347346
s.indent_with(IndentOptions {
348347
indentor: Some(">>"),
349-
exclude: vec![7, 15],
348+
exclude: vec![[7, 15]],
350349
});
351350
assert_eq!(s.to_string(), ">> abc\n>> def\nghi\njkl");
352351
}

0 commit comments

Comments
 (0)