Skip to content

Commit 00ff3ef

Browse files
committed
fix(indent): should ignore the end of each exclude range
1 parent ee30b12 commit 00ff3ef

File tree

3 files changed

+25
-20
lines changed

3 files changed

+25
-20
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.16"
3+
version = "0.0.17"
44
edition = "2021"
55
license = "MIT"
66
description = "manipulate string like wizards"

src/magic_string/indent.rs

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@ use std::borrow::Cow;
22

33
use crate::{CowStr, MagicString, TextSize};
44

5-
struct ExcludeSet {
6-
exclude: Vec<[TextSize; 2]>,
5+
struct ExcludeSet<'a> {
6+
exclude: &'a [(TextSize, TextSize)],
77
}
88

9-
impl ExcludeSet {
10-
fn new(exclude: Vec<[TextSize; 2]>) -> Self {
9+
impl<'a> ExcludeSet<'a> {
10+
fn new(exclude: &'a [(TextSize, TextSize)]) -> Self {
1111
Self { exclude }
1212
}
1313

1414
fn contains(&self, index: TextSize) -> bool {
15-
self.exclude.iter().any(|s| s[0] <= index && index <= s[1])
15+
self.exclude.iter().any(|s| s.0 <= index && index < s.1)
1616
}
1717
}
1818

@@ -49,14 +49,14 @@ pub fn guess_indentor(source: &str) -> Option<String> {
4949
}
5050

5151
#[derive(Debug, Default)]
52-
pub struct IndentOptions<'a> {
52+
pub struct IndentOptions<'a, 'b> {
5353
/// MagicString will guess the `indentor`` from lines of the source if passed `None`.
5454
pub indentor: Option<&'a str>,
5555

5656
/// The reason I use `[u32; 2]` instead of `(u32, u32)` to represent a range of text is that
5757
/// I want to emphasize that the `[u32; 2]` is the closed interval, which means both the start
5858
/// and the end are included in the range.
59-
pub exclude: Vec<[TextSize; 2]>,
59+
pub exclude: &'b [(TextSize, TextSize)],
6060
}
6161

6262
impl<'text> MagicString<'text> {
@@ -74,7 +74,7 @@ impl<'text> MagicString<'text> {
7474
})
7575
}
7676

77-
pub fn indent_with(&mut self, opts: IndentOptions<'_>) -> &mut Self {
77+
pub fn indent_with(&mut self, opts: IndentOptions) -> &mut Self {
7878
if opts.indentor.map_or(false, |s| s.is_empty()) {
7979
return self;
8080
}
@@ -83,11 +83,7 @@ impl<'text> MagicString<'text> {
8383
indentor: String,
8484
}
8585

86-
87-
fn indent_frag<'text>(
88-
frag: &mut CowStr<'text>,
89-
indent_replacer: &mut IndentReplacer,
90-
) {
86+
fn indent_frag<'text>(frag: &mut CowStr<'text>, indent_replacer: &mut IndentReplacer) {
9187
let mut indented = String::new();
9288
for char in frag.chars() {
9389
if char == '\n' {
@@ -103,7 +99,6 @@ impl<'text> MagicString<'text> {
10399

104100
let indentor = opts.indentor.unwrap_or_else(|| self.guessed_indentor());
105101

106-
107102
let mut indent_replacer = IndentReplacer {
108103
should_indent_next_char: true,
109104
indentor: indentor.to_string(),
@@ -114,7 +109,7 @@ impl<'text> MagicString<'text> {
114109
}
115110

116111
let exclude_set = ExcludeSet::new(opts.exclude);
117-
112+
118113
let mut next_chunk_id = Some(self.first_chunk_idx);
119114
let mut char_index = 0u32;
120115
while let Some(chunk_idx) = next_chunk_id {
@@ -123,7 +118,7 @@ impl<'text> MagicString<'text> {
123118
next_chunk_id = self.chunks[chunk_idx].next;
124119
if let Some(edited_content) = self.chunks[chunk_idx].edited_content.as_mut() {
125120
if !exclude_set.contains(char_index) {
126-
indent_frag(edited_content, &mut indent_replacer);
121+
indent_frag(edited_content, &mut indent_replacer);
127122
}
128123
} else {
129124
let chunk = &self.chunks[chunk_idx];
@@ -151,7 +146,7 @@ impl<'text> MagicString<'text> {
151146
}
152147

153148
for frag in self.outro.iter_mut() {
154-
indent_frag(frag, &mut indent_replacer)
149+
indent_frag(frag, &mut indent_replacer)
155150
}
156151

157152
self

tests/magic_string.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -340,12 +340,12 @@ mod indent {
340340
let mut s = MagicString::new("abc\ndef\nghi\njkl");
341341
s.indent_with(IndentOptions {
342342
indentor: Some(" "),
343-
exclude: vec![[7, 15]],
343+
exclude: &[(7, 15)],
344344
});
345345
assert_eq!(s.to_string(), " abc\n def\nghi\njkl");
346346
s.indent_with(IndentOptions {
347347
indentor: Some(">>"),
348-
exclude: vec![[7, 15]],
348+
exclude: &[(7, 15)],
349349
});
350350
assert_eq!(s.to_string(), ">> abc\n>> def\nghi\njkl");
351351
}
@@ -393,6 +393,16 @@ mod indent {
393393
s.indent();
394394
assert_eq!(s.to_string(), "\tclass Foo extends Baz {}");
395395
}
396+
397+
#[test]
398+
fn should_ignore_the_end_of_each_exclude_range() {
399+
let mut s = MagicString::new("012\n456\n89a\nbcd");
400+
s.indent_with(IndentOptions {
401+
indentor: Some(">"),
402+
exclude: &[(0, 3)],
403+
});
404+
assert_eq!(s.to_string(), "012\n>456\n>89a\n>bcd");
405+
}
396406
}
397407

398408
mod misc {

0 commit comments

Comments
 (0)