Skip to content

Commit

Permalink
fix formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
zkat committed Oct 5, 2024
1 parent 3f55cef commit 792f3a2
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 12 deletions.
2 changes: 1 addition & 1 deletion examples/insert-node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ words {
let word_node = KdlNode::new(identifier);
word_nodes.push(word_node);
word_nodes.sort_by(sort_by_name);
words_section.fmt();
words_section.autoformat();

println!("{}", doc);

Expand Down
10 changes: 4 additions & 6 deletions src/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -345,9 +345,6 @@ impl KdlDocument {
}
for node in &self.nodes {
node.stringify(f, indent)?;
if node.format().is_none() {
writeln!(f)?;
}
}
if let Some(KdlDocumentFormat { trailing, .. }) = self.format() {
write!(f, "{}", trailing)?;
Expand Down Expand Up @@ -549,9 +546,11 @@ final;";
doc.nodes_mut().push(bar);
doc.nodes_mut().push(KdlNode::new("baz"));

doc.autoformat();

assert_eq!(
r#"foo
bar prop="value" 1 2 false null {
bar prop=value 1 2 #false #null {
barchild
}
baz
Expand Down Expand Up @@ -598,7 +597,6 @@ baz

KdlDocument::autoformat(&mut doc);

print!("{}", doc);
assert_eq!(
doc.to_string(),
r#"/* x */
Expand All @@ -611,7 +609,7 @@ foo 1 bar=0xdeadbeef {
multiline*/
inner1 r"value"
inner1 value
inner2 {
inner3
}
Expand Down
16 changes: 11 additions & 5 deletions src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ use crate::{
v2_parser, KdlDocument, KdlDocumentFormat, KdlEntry, KdlIdentifier, KdlParseFailure, KdlValue,
};

static INDENT: usize = 4;

/// Represents an individual KDL
/// [`Node`](https://github.com/kdl-org/kdl/blob/main/SPEC.md#node) inside a
/// KDL Document.
Expand Down Expand Up @@ -56,7 +58,7 @@ impl KdlNode {
ty: None,
entries: Vec::new(),
children: None,
format: None,
format: Some(KdlNodeFormat { trailing: "\n".into(), ..Default::default() }),
#[cfg(feature = "span")]
span: SourceSpan::from(0..0),
}
Expand Down Expand Up @@ -382,12 +384,12 @@ impl KdlNode {
self.format = Some(format);
}
/// Auto-formats this node and its contents.
pub fn fmt(&mut self) {
pub fn autoformat(&mut self) {
self.autoformat_impl(0, false);
}

/// Auto-formats this node and its contents, stripping comments.
pub fn fmt_no_comments(&mut self) {
pub fn autoformat_no_comments(&mut self) {
self.autoformat_impl(0, true);
}

Expand Down Expand Up @@ -530,9 +532,12 @@ impl KdlNode {
trailing.insert(0, ' ');
}
}
*trailing = trailing.trim().into();
trailing.push('\n');

*before_children = " ".into();
} else {
self.set_format(KdlNodeFormat { trailing: "\n".into(), ..Default::default() })
}
self.name.clear_format();
if let Some(ty) = self.ty.as_mut() {
Expand All @@ -542,9 +547,10 @@ impl KdlNode {
entry.autoformat();
}
if let Some(children) = self.children.as_mut() {
children.autoformat_impl(indent + 4, no_comments);
children.autoformat_impl(indent + INDENT, no_comments);
if let Some(KdlDocumentFormat { leading, trailing }) = children.format_mut() {
*leading = "\n".into();
*leading = leading.trim().into();
leading.push('\n');
trailing.push_str(format!("{:indent$}", "", indent = indent).as_str());
}
}
Expand Down

0 comments on commit 792f3a2

Please sign in to comment.