pub struct MagicString<'s> { /* private fields */ }Implementations§
Source§impl<'text> MagicString<'text>
impl<'text> MagicString<'text>
pub fn append(&mut self, source: impl Into<Cow<'text, str>>) -> &mut Self
Sourcepub fn append_left(
&mut self,
text_index: u32,
content: impl Into<Cow<'text, str>>,
) -> Result<&mut Self, String>
pub fn append_left( &mut self, text_index: u32, content: impl Into<Cow<'text, str>>, ) -> Result<&mut Self, String>
§Errors
Returns Err if text_index falls inside a chunk that has already been edited, since the
chunk cannot be split there.
§Example
use string_wizard::MagicString;
let mut s = MagicString::new("01234");
s.append_left(2, "a").unwrap();
s.append_left(2, "b").unwrap();
assert_eq!(s.to_string(), "01ab234")Sourcepub fn append_right(
&mut self,
text_index: u32,
content: impl Into<Cow<'text, str>>,
) -> Result<&mut Self, String>
pub fn append_right( &mut self, text_index: u32, content: impl Into<Cow<'text, str>>, ) -> Result<&mut Self, String>
§Errors
Returns Err if text_index falls inside a chunk that has already been edited, since the
chunk cannot be split there.
§Example
use string_wizard::MagicString;
let mut s = MagicString::new("01234");
s.append_right(2, "A").unwrap();
s.append_right(2, "B").unwrap();
s.append_left(2, "a").unwrap();
s.append_left(2, "b").unwrap();
assert_eq!(s.to_string(), "01abAB234")Source§impl MagicString<'_>
impl MagicString<'_>
pub fn get_indent_string(&self) -> &str
Sourcepub fn indent(&mut self) -> Result<&mut Self, String>
pub fn indent(&mut self) -> Result<&mut Self, String>
§Errors
Propagates the split error from Self::prepend_right. Indentation only splits chunks
that have not been edited, so this is not reachable in practice.
Sourcepub fn indent_with(
&mut self,
opts: IndentOptions<'_, '_>,
) -> Result<&mut Self, String>
pub fn indent_with( &mut self, opts: IndentOptions<'_, '_>, ) -> Result<&mut Self, String>
§Errors
See Self::indent.
Source§impl MagicString<'_>
impl MagicString<'_>
Sourcepub fn remove(&mut self, start: u32, end: u32) -> Result<&mut Self, String>
pub fn remove(&mut self, start: u32, end: u32) -> Result<&mut Self, String>
Removes characters in the range [start, end) from the generated output.
Unlike update/overwrite, this iterates by original position (via chunk_by_start)
rather than by linked-list order, so it works correctly across moved content.
Source§impl<'text> MagicString<'text>
impl<'text> MagicString<'text>
pub fn prepend(&mut self, source: impl Into<Cow<'text, str>>) -> &mut Self
Source§impl<'text> MagicString<'text>
impl<'text> MagicString<'text>
pub fn replace( &mut self, from: &str, to: impl Into<Cow<'text, str>>, ) -> Result<&mut Self, String>
pub fn replace_all( &mut self, from: &str, to: impl Into<Cow<'text, str>>, ) -> Result<&mut Self, String>
pub fn replace_with( &mut self, from: &str, to: impl Into<Cow<'text, str>>, options: ReplaceOptions, ) -> Result<&mut Self, String>
Source§impl<'text> MagicString<'text>
impl<'text> MagicString<'text>
Sourcepub fn reset(&mut self, start: u32, end: u32) -> Result<&mut Self, String>
pub fn reset(&mut self, start: u32, end: u32) -> Result<&mut Self, String>
Resets the portion of the string from start to end to its original content.
This undoes any modifications (updates, overwrites, intro/outro additions) made to that range.
§Errors
- If
startis greater than toend - If the range is out of bounds
Source§impl<'text> MagicString<'text>
impl<'text> MagicString<'text>
Source§impl MagicString<'_>
impl MagicString<'_>
pub fn source_map(&self, opts: SourceMapOptions) -> SourceMap<'static>
Source§impl<'text> MagicString<'text>
impl<'text> MagicString<'text>
Sourcepub fn trim(&mut self, char_type: Option<&str>) -> &mut Self
pub fn trim(&mut self, char_type: Option<&str>) -> &mut Self
Trims whitespace from the start and end of the string.
Sourcepub fn trim_start(&mut self, char_type: Option<&str>) -> &mut Self
pub fn trim_start(&mut self, char_type: Option<&str>) -> &mut Self
Trims whitespace from the start of the string.
Sourcepub fn trim_end(&mut self, char_type: Option<&str>) -> &mut Self
pub fn trim_end(&mut self, char_type: Option<&str>) -> &mut Self
Trims whitespace from the end of the string.
Sourcepub fn trim_lines(&mut self) -> &mut Self
pub fn trim_lines(&mut self) -> &mut Self
Trims newlines from the start and end of the string.
Source§impl<'text> MagicString<'text>
impl<'text> MagicString<'text>
Sourcepub fn update(
&mut self,
start: u32,
end: u32,
content: impl Into<Cow<'text, str>>,
) -> Result<&mut Self, String>
pub fn update( &mut self, start: u32, end: u32, content: impl Into<Cow<'text, str>>, ) -> Result<&mut Self, String>
A shorthand for update_with(start, end, content, Default::default());
pub fn update_with( &mut self, start: u32, end: u32, content: impl Into<Cow<'text, str>>, opts: UpdateOptions, ) -> Result<&mut Self, String>
Source§impl<'text> MagicString<'text>
impl<'text> MagicString<'text>
pub fn new(source: impl Into<Cow<'text, str>>) -> Self
pub fn with_options( source: impl Into<Cow<'text, str>>, options: MagicStringOptions, ) -> Self
pub fn source(&self) -> &str
pub fn filename(&self) -> Option<&str>
pub fn ignore_list(&self) -> bool
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the length in UTF-8 bytes of the content within chunks (intro + content + outro
per chunk), excluding the global intro/outro from prepend/append.
This aligns with the reference magic-string behavior.
Note this counts bytes. Callers that need the length JavaScript would report (UTF-16
code units, as the reference magic-string returns) must use Self::len_utf16.
Sourcepub fn len_utf16(&self) -> usize
pub fn len_utf16(&self) -> usize
Returns the length in UTF-16 code units of the content within chunks, excluding the
global intro/outro — the same range Self::len covers.
This is what the reference magic-string length() returns, since JavaScript string
length is measured in UTF-16 code units.
Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true if all chunk content (intro + content + outro) is whitespace or empty.
This aligns with the reference magic-string behavior where isEmpty() uses .trim().
Sourcepub fn has_changed(&self) -> bool
pub fn has_changed(&self) -> bool
Indicates if the string has been changed.
The length check is only a fast path for “definitely changed”, so it has to measure the
same span as the string comparison below it: the whole output, global intro/outro
included. Comparing against Self::len instead would report a change for edits that
cancel out, e.g. remove(0, 1) followed by prepend("a").
Sourcepub fn last_char(&self) -> Option<char>
pub fn last_char(&self) -> Option<char>
Returns the last character of the generated string, or None if empty.
Sourcepub fn last_line(&self) -> String
pub fn last_line(&self) -> String
Returns the content after the last newline in the generated string.
pub fn prepend_outro(&mut self, content: impl Into<Cow<'text, str>>)
pub fn append_intro(&mut self, content: impl Into<Cow<'text, str>>)
Sourcepub fn is_range_within_edited_chunk(&self, start: u32, end: u32) -> bool
pub fn is_range_within_edited_chunk(&self, start: u32, end: u32) -> bool
Returns true if the byte range [start, end) lies within a single chunk that has
already been edited. Callers use this for split points that cannot be expressed as a
byte index (a UTF-16 position inside a surrogate pair): the split would land strictly
inside the character spanning [start, end), so it hits an edited chunk exactly when
this returns true.
Trait Implementations§
Source§impl<'s> Clone for MagicString<'s>
impl<'s> Clone for MagicString<'s>
Source§impl<'s> Debug for MagicString<'s>
impl<'s> Debug for MagicString<'s>
Source§impl Default for MagicString<'_>
impl Default for MagicString<'_>
Auto Trait Implementations§
impl<'s> !Freeze for MagicString<'s>
impl<'s> Forget for MagicString<'s>
impl<'s> Move for MagicString<'s>
impl<'s> RefUnwindSafe for MagicString<'s>
impl<'s> Send for MagicString<'s>
impl<'s> Sync for MagicString<'s>
impl<'s> Unpin for MagicString<'s>
impl<'s> UnsafeUnpin for MagicString<'s>
impl<'s> UnwindSafe for MagicString<'s>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more