Skip to main content

MagicString

Struct MagicString 

Source
pub struct MagicString<'s> { /* private fields */ }

Implementations§

Source§

impl<'text> MagicString<'text>

Source

pub fn append(&mut self, source: impl Into<Cow<'text, str>>) -> &mut Self

Source

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")
Source

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<'_>

Source

pub fn get_indent_string(&self) -> &str

Source

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.

Source

pub fn indent_with( &mut self, opts: IndentOptions<'_, '_>, ) -> Result<&mut Self, String>

§Errors

See Self::indent.

Source§

impl MagicString<'_>

Source

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

pub fn relocate( &mut self, start: u32, end: u32, to: u32, ) -> Result<&mut Self, String>

Moves the characters from start and end to index. Returns this.

Source

pub fn snip(&self, start: u32, end: u32) -> Result<Self, String>

Returns a clone with content outside the specified range removed. This is equivalent to clone().remove(0, start).remove(end, original.len()).

Source§

impl<'text> MagicString<'text>

Source

pub fn prepend(&mut self, source: impl Into<Cow<'text, str>>) -> &mut Self

Source

pub fn prepend_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.

Source

pub fn prepend_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.

Source§

impl<'text> MagicString<'text>

Source

pub fn replace( &mut self, from: &str, to: impl Into<Cow<'text, str>>, ) -> Result<&mut Self, String>

Source

pub fn replace_all( &mut self, from: &str, to: impl Into<Cow<'text, str>>, ) -> Result<&mut Self, String>

Source

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>

Source

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 start is greater than to end
  • If the range is out of bounds
Source§

impl<'text> MagicString<'text>

Source

pub fn slice(&self, start: u32, end: Option<u32>) -> Result<String, String>

Returns the content of the generated string that corresponds to the original positions from start to end.

If end is None, it defaults to the original string length.

Source§

impl MagicString<'_>

Source

pub fn source_map(&self, opts: SourceMapOptions) -> SourceMap<'static>

Source§

impl<'text> MagicString<'text>

Source

pub fn trim(&mut self, char_type: Option<&str>) -> &mut Self

Trims whitespace from the start and end of the string.

Source

pub fn trim_start(&mut self, char_type: Option<&str>) -> &mut Self

Trims whitespace from the start of the string.

Source

pub fn trim_end(&mut self, char_type: Option<&str>) -> &mut Self

Trims whitespace from the end of the string.

Source

pub fn trim_lines(&mut self) -> &mut Self

Trims newlines from the start and end of the string.

Source§

impl<'text> MagicString<'text>

Source

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());

Source

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>

Source

pub fn new(source: impl Into<Cow<'text, str>>) -> Self

Source

pub fn with_options( source: impl Into<Cow<'text, str>>, options: MagicStringOptions, ) -> Self

Source

pub fn source(&self) -> &str

Source

pub fn filename(&self) -> Option<&str>

Source

pub fn ignore_list(&self) -> bool

Source

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.

Source

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.

Source

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().

Source

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").

Source

pub fn last_char(&self) -> Option<char>

Returns the last character of the generated string, or None if empty.

Source

pub fn last_line(&self) -> String

Returns the content after the last newline in the generated string.

Source

pub fn prepend_outro(&mut self, content: impl Into<Cow<'text, str>>)

Source

pub fn append_intro(&mut self, content: impl Into<Cow<'text, str>>)

Source

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>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<'s> Debug for MagicString<'s>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for MagicString<'_>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl ToString for MagicString<'_>

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.