pub struct Editor { /* private fields */ }
Expand description
Handle to a UTF-8 text file kept in memory until save
is called.
All mutating methods return &mut self
, enabling a fluent builder style.
Editor::open("Cargo.toml")?
.insert_before("[dependencies]", "regex = \"1\"\n", false)
.save()?;
Implementations§
Source§impl Editor
impl Editor
Sourcepub fn create<P: AsRef<Path>>(path: P) -> Result<Self>
pub fn create<P: AsRef<Path>>(path: P) -> Result<Self>
Create or truncate a file and return an editor over it.
Equivalent to fs::write(path, "")
followed by open
.
Sourcepub fn open<P: AsRef<Path>>(path: P) -> Result<Self>
pub fn open<P: AsRef<Path>>(path: P) -> Result<Self>
Open an existing UTF-8 file into an in-memory buffer.
Sourcepub fn rename<P: AsRef<Path>>(&mut self, new_name: P) -> Result<&mut Self>
pub fn rename<P: AsRef<Path>>(&mut self, new_name: P) -> Result<&mut Self>
Rename the underlying file on disk and update the internal path.
Sourcepub fn save(&mut self) -> Result<&mut Self>
pub fn save(&mut self) -> Result<&mut Self>
Write the in-memory buffer back to disk iff it was modified.
Returns Ok(self)
even when there was nothing to do.
Sourcepub fn insert_before(
&mut self,
marker: &str,
text: &str,
same_indent: bool,
) -> &mut Self
pub fn insert_before( &mut self, marker: &str, text: &str, same_indent: bool, ) -> &mut Self
Insert text
before the first occurrence of marker
.
- If
same_indent
istrue
, the current indentation of the line containingmarker
is copied and prepended totext
.
Sourcepub fn insert_after(
&mut self,
marker: &str,
text: &str,
same_indent: bool,
) -> &mut Self
pub fn insert_after( &mut self, marker: &str, text: &str, same_indent: bool, ) -> &mut Self
Insert text
after the first occurrence of marker
.
- If
marker
ends a line, the insertion starts on the next line. - Otherwise the insertion is in-line; a space is auto-inserted when needed.
- When
same_indent
istrue
, every subsequent line intext
is indented to match the marker line.
Sourcepub fn replace_marker(
&mut self,
marker: &str,
text: &str,
same_indent: bool,
) -> &mut Self
pub fn replace_marker( &mut self, marker: &str, text: &str, same_indent: bool, ) -> &mut Self
Replace the first occurrence of marker
with text
.
When same_indent
is true
, the replacement receives the indentation
that preceded the marker.
Sourcepub fn find_lines<'a, P>(&self, pattern: P, limit: Option<usize>) -> Vec<usize>where
P: Into<Pattern<'a>>,
pub fn find_lines<'a, P>(&self, pattern: P, limit: Option<usize>) -> Vec<usize>where
P: Into<Pattern<'a>>,
Return 1-based line numbers where pattern
occurs.
Pass limit = Some(n)
to cap the results.
Sourcepub fn erase<'a, P>(&mut self, pattern: P) -> &mut Selfwhere
P: Into<Pattern<'a>>,
pub fn erase<'a, P>(&mut self, pattern: P) -> &mut Selfwhere
P: Into<Pattern<'a>>,
Erase all occurrences of pattern
.