Skip to content

Commit 31fa9a6

Browse files
committed
refactor: indent impl
1 parent b497528 commit 31fa9a6

File tree

3 files changed

+21
-38
lines changed

3 files changed

+21
-38
lines changed

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "string_wizard"
3-
version = "0.0.13"
3+
version = "0.0.14"
44
edition = "2021"
55
license = "MIT"
66
description = "manipulate string like wizards"
@@ -10,7 +10,7 @@ description = "manipulate string like wizards"
1010
[dependencies]
1111
index_vec = { version = "0.1.3" }
1212
rustc-hash = { version = "1.1.0" }
13-
regex = "1.10.2"
13+
# regex = "1.10.2"
1414
serde = { version = "1.0", features = ["derive"], optional = true }
1515
serde_json = { version = "1.0", optional = true }
1616
once_cell = "1.18.0"

src/magic_string/indent.rs

Lines changed: 18 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::borrow::Cow;
2+
13
use rustc_hash::FxHashSet;
24

35
use crate::{CowStr, MagicString, TextSize};
@@ -66,57 +68,37 @@ impl<'text> MagicString<'text> {
6668
}
6769
struct IndentReplacer {
6870
should_indent_next_char: bool,
69-
indent_str: String,
71+
indentor: String,
7072
}
7173

72-
impl regex::Replacer for &mut &mut IndentReplacer {
73-
fn replace_append(&mut self, caps: &regex::Captures<'_>, dst: &mut String) {
74-
if self.should_indent_next_char {
75-
dst.push_str(&self.indent_str);
76-
}
77-
for cap in caps.iter() {
78-
if let Some(cap) = cap {
79-
dst.push_str(cap.as_str());
80-
}
81-
}
82-
}
83-
}
8474

8575
fn indent_frag<'text>(
8676
frag: &mut CowStr<'text>,
87-
pattern: &regex::Regex,
88-
mut indent_replacer: &mut IndentReplacer,
77+
indent_replacer: &mut IndentReplacer,
8978
) {
90-
match frag {
91-
std::borrow::Cow::Borrowed(str) => {
92-
let might_replaced = pattern.replace_all(str, &mut indent_replacer);
93-
*frag = might_replaced;
94-
}
95-
std::borrow::Cow::Owned(str) => {
96-
let might_replaced = pattern.replace_all(str, &mut indent_replacer);
97-
match might_replaced {
98-
std::borrow::Cow::Owned(replaced) => {
99-
*frag = replaced.into();
100-
}
101-
std::borrow::Cow::Borrowed(_) => {
102-
// Since nothing was replaced, we can just use the original string.
103-
}
104-
}
79+
let mut indented = String::new();
80+
for char in frag.chars() {
81+
if char == '\n' {
82+
indent_replacer.should_indent_next_char = true;
83+
} else if char != '\r' && indent_replacer.should_indent_next_char {
84+
indent_replacer.should_indent_next_char = false;
85+
indented.push_str(&indent_replacer.indentor);
10586
}
87+
indented.push(char);
10688
}
89+
*frag = Cow::Owned(indented);
10790
}
10891

10992
let indentor = opts.indentor.unwrap_or_else(|| self.guessed_indentor());
11093

111-
let pattern = regex::Regex::new(r"(?m)^[^\r\n]").unwrap();
11294

11395
let mut indent_replacer = IndentReplacer {
11496
should_indent_next_char: true,
115-
indent_str: indentor.to_string(),
97+
indentor: indentor.to_string(),
11698
};
11799

118100
for intro_frag in self.intro.iter_mut() {
119-
indent_frag(intro_frag, &pattern, &mut indent_replacer)
101+
indent_frag(intro_frag, &mut indent_replacer)
120102
}
121103
let is_excluded = {
122104
let mut is_excluded = FxHashSet::default();
@@ -136,7 +118,7 @@ impl<'text> MagicString<'text> {
136118
next_chunk_id = self.chunks[chunk_idx].next;
137119
if let Some(edited_content) = self.chunks[chunk_idx].edited_content.as_mut() {
138120
if !is_excluded.contains(&char_index) {
139-
indent_frag(edited_content, &pattern, &mut indent_replacer);
121+
indent_frag(edited_content, &mut indent_replacer);
140122
}
141123
} else {
142124
let chunk = &self.chunks[chunk_idx];
@@ -157,14 +139,14 @@ impl<'text> MagicString<'text> {
157139
char_index += char.len_utf8() as u32;
158140
}
159141
for line_start in line_starts {
160-
self.prepend_right(line_start, indent_replacer.indent_str.clone());
142+
self.prepend_right(line_start, indent_replacer.indentor.clone());
161143
}
162144
char_index = chunk_end;
163145
}
164146
}
165147

166148
for frag in self.outro.iter_mut() {
167-
indent_frag(frag, &pattern, &mut indent_replacer)
149+
indent_frag(frag, &mut indent_replacer)
168150
}
169151

170152
self

tests/magic_string.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,7 @@ mod indent {
379379
#[test]
380380
fn should_indent_content_with_removals() {
381381
let mut s = MagicString::new("/* remove this line */\nvar foo = 1;");
382+
// remove `/* remove this line */\n`
382383
s.remove(0, 23);
383384
s.indent();
384385
assert_eq!(s.to_string(), "\tvar foo = 1;");

0 commit comments

Comments
 (0)