feat(clear_fmt): add methods to clear formatting and reset it to default

This commit is contained in:
Kat Marchán 2022-04-22 22:24:52 -07:00
parent fc1d0fd24b
commit 892bf06e69
4 changed files with 30 additions and 0 deletions

View File

@ -168,6 +168,13 @@ impl KdlDocument {
self.trailing = Some(trailing.into());
}
/// Clears leading and trailing text (whitespace, comments). `KdlNode`s in
/// this document will be unaffected.
pub fn clear_fmt(&mut self) {
self.leading = None;
self.trailing = None;
}
/// Auto-formats this Document.
///
/// Note: This currently removes comments as well.

View File

@ -81,6 +81,13 @@ impl KdlEntry {
self.trailing = Some(trailing.into());
}
/// Clears leading and trailing text (whitespace, comments), as well as
/// resetting this entry's value to its default representation.
pub fn clear_fmt(&mut self) {
self.leading = None;
self.trailing = None;
}
/// Gets the custom string representation for this KdlEntry's [`KdlValue`].
pub fn value_repr(&self) -> Option<&str> {
self.value_repr.as_deref()

View File

@ -31,6 +31,13 @@ impl KdlIdentifier {
self.repr = Some(repr.into());
}
/// Resets this identifier to its default representation. It will attempt
/// to make it an unquoted identifier, and fall back to a string
/// representation if that would be invalid.
pub fn clear_fmt(&mut self) {
self.repr = None;
}
/// Auto-formats this identifier.
pub fn fmt(&mut self) {
self.repr = None;

View File

@ -106,6 +106,15 @@ impl KdlNode {
self.trailing = Some(trailing.into());
}
/// Clears leading and trailing text (whitespace, comments), as well as
/// the space before the children block, if any. Individual entries and
/// their formatting will be preserved.
pub fn clear_fmt(&mut self) {
self.leading = None;
self.trailing = None;
self.before_children = None;
}
/// Fetches an entry by key. Number keys will look up arguments, strings
/// will look up properties.
pub fn get(&self, key: impl Into<NodeKey>) -> Option<&KdlEntry> {