pub struct Editor { /* private fields */ }
Expand description
Keeps track of edits so you can apply them correctly all-at-once with finish
See module documentation for more.
Implementations§
source§impl Editor
impl Editor
sourcepub fn new_with_ast<T: Parse>(source_code: &str) -> Result<(Self, T)>
pub fn new_with_ast<T: Parse>(source_code: &str) -> Result<(Self, T)>
Parse source_code
into the given type, and create a new editor over that source code.
sourcepub fn new(source_code: &str) -> Self
pub fn new(source_code: &str) -> Self
Create a new editor.
Syntax tree types used with this editor must be from the same source.
sourcepub fn replace(
&mut self,
node: impl Spanned,
text: impl Into<String>
) -> &mut Self
pub fn replace( &mut self, node: impl Spanned, text: impl Into<String> ) -> &mut Self
Replace node
with text
.
let (mut editor, ast) = Editor::new_with_ast::<syn::TraitItemFn>("fn shave_yaks();").unwrap();
assert_eq!(editor.replace(ast.sig.ident, "write_code").finish(), "fn write_code();")
Panics
- may panic if
node
is from a difference source text than thisEditor
. - if there is a conflict (i.e this node, or a parent of it has already been edited).
sourcepub fn prepend(
&mut self,
node: impl Spanned,
text: impl Into<String>
) -> &mut Self
pub fn prepend( &mut self, node: impl Spanned, text: impl Into<String> ) -> &mut Self
Put text
before node
.
let (mut editor, ast) = Editor::new_with_ast::<syn::ExprCast>("ate as u8").unwrap();
assert_eq!(editor.prepend(ast.expr, "i_").finish(), "i_ate as u8")
Panics
- may panic if
node
is from a difference source text than thisEditor
. - if there is a conflict (i.e this node, or a parent of it has already been edited).
sourcepub fn append(
&mut self,
node: impl Spanned,
text: impl Into<String>
) -> &mut Self
pub fn append( &mut self, node: impl Spanned, text: impl Into<String> ) -> &mut Self
Put text
after node
.
let (mut editor, ast) = Editor::new_with_ast::<syn::Expr>("maybe").unwrap();
assert_eq!(editor.append(ast, "?").finish(), "maybe?")
Panics
- may panic if
node
is from a difference source text than thisEditor
. - if there is a conflict (i.e this node, or a parent of it has already been edited).
sourcepub fn remove(&mut self, node: impl Spanned) -> &mut Self
pub fn remove(&mut self, node: impl Spanned) -> &mut Self
Remove the text for node
.
let (mut editor, ast) = Editor::new_with_ast::<syn::ExprArray>("[going, going, gone]").unwrap();
assert_eq!(editor.remove(&ast.elems[2]).finish(), "[going, going, ]")
Panics
- may panic if
node
is from a difference source text than thisEditor
. - if there is a conflict (i.e this node, or a parent of it has already been edited).
Trait Implementations§
source§impl Ord for Editor
impl Ord for Editor
source§impl PartialEq for Editor
impl PartialEq for Editor
source§impl PartialOrd for Editor
impl PartialOrd for Editor
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
This method tests less than or equal to (for
self
and other
) and is used by the <=
operator. Read moreimpl Eq for Editor
impl StructuralEq for Editor
impl StructuralPartialEq for Editor
Auto Trait Implementations§
impl RefUnwindSafe for Editor
impl Send for Editor
impl Sync for Editor
impl Unpin for Editor
impl UnwindSafe for Editor
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
Mutably borrows from an owned value. Read more