From 892bf06e69c746ea9711fe33979f28f937329672 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kat=20March=C3=A1n?= Date: Fri, 22 Apr 2022 22:24:52 -0700 Subject: [PATCH] feat(clear_fmt): add methods to clear formatting and reset it to default --- src/document.rs | 7 +++++++ src/entry.rs | 7 +++++++ src/identifier.rs | 7 +++++++ src/node.rs | 9 +++++++++ 4 files changed, 30 insertions(+) diff --git a/src/document.rs b/src/document.rs index 73236ef..cbb780a 100644 --- a/src/document.rs +++ b/src/document.rs @@ -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. diff --git a/src/entry.rs b/src/entry.rs index ca5c2ca..4062fe4 100644 --- a/src/entry.rs +++ b/src/entry.rs @@ -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() diff --git a/src/identifier.rs b/src/identifier.rs index 5d69d41..739dbde 100644 --- a/src/identifier.rs +++ b/src/identifier.rs @@ -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; diff --git a/src/node.rs b/src/node.rs index 70adcd8..f28511c 100644 --- a/src/node.rs +++ b/src/node.rs @@ -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) -> Option<&KdlEntry> {