Assign document leading format to the first node instead

This commit is contained in:
Dion Dokter 2025-09-03 20:05:10 +02:00
parent 439aa63bfc
commit 3055e1ec7a
2 changed files with 17 additions and 3 deletions

View File

@ -671,10 +671,14 @@ final;";
);
assert_eq!(
doc.format().map(|f| &f.leading[..]),
Some("\n// This is the first node\n")
Some("")
);
let foo = doc.get("foo").expect("expected a foo node");
assert_eq!(
foo.format().map(|f| &f.leading[..]),
Some("\n// This is the first node\n")
);
assert_eq!(foo.format().map(|f| &f.terminator[..]), Some("\n"));
assert_eq!(&foo[2], &"three".into());
assert_eq!(&foo["bar"], &"baz".into());

View File

@ -282,12 +282,12 @@ pub(crate) fn document(input: &mut Input<'_>) -> PResult<KdlDocument> {
/// `nodes := (line-space* node)* line-space*`
fn nodes(input: &mut Input<'_>) -> PResult<KdlDocument> {
let leading = repeat(0.., alt((line_space.void(), (slashdash, base_node).void())))
let mut leading = repeat(0.., alt((line_space.void(), (slashdash, base_node).void())))
.map(|()| ())
.take()
.parse_next(input)?;
let _start = input.checkpoint();
let ns: Vec<KdlNode> = separated(
let mut ns: Vec<KdlNode> = separated(
0..,
node,
alt((node_terminator.void(), (eof.void(), any.void()).void())),
@ -299,6 +299,16 @@ fn nodes(input: &mut Input<'_>) -> PResult<KdlDocument> {
.map(|()| ())
.take()
.parse_next(input)?;
// If there is a node, let it have the leading format
// This gives more consistent behavior
if let Some(first_node) = ns.get_mut(0) {
if let Some(first_node_format) = first_node.format_mut() {
first_node_format.leading = leading.into();
leading = "";
}
}
Ok(KdlDocument {
nodes: ns,
format: Some(KdlDocumentFormat {