Skip to content

Commit 2802578

Browse files
authored
feat(parser): update to 2.0.0-draft.5 (#90)
1 parent fb7c92b commit 2802578

24 files changed

+306
-155
lines changed

src/document.rs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,7 @@ second_node /* This time, the comment is here */ param=153 {
436436
/- comment
437437
/* block comment */
438438
inline /*comment*/ here
439-
another /-commend there
439+
another /-comment there
440440
441441
442442
after some whitespace
@@ -485,7 +485,7 @@ final;";
485485
);
486486

487487
let foo = doc.get("foo").expect("expected a foo node");
488-
assert_eq!(foo.format().map(|f| &f.trailing[..]), Some("\n"));
488+
assert_eq!(foo.format().map(|f| &f.terminator[..]), Some("\n"));
489489
assert_eq!(&foo[2], &"three".into());
490490
assert_eq!(&foo["bar"], &"baz".into());
491491
assert_eq!(
@@ -527,6 +527,7 @@ final;";
527527
// if you're making KdlEntries this way, you need to inject
528528
// your own whitespace (or format the node)
529529
node.push(" \"blah\"=0xDEADbeef".parse::<KdlEntry>()?);
530+
dbg!(&node);
530531
doc.nodes_mut().push(node);
531532

532533
assert_eq!(
@@ -714,7 +715,8 @@ this {
714715
}
715716
// that's
716717
nice
717-
inline { time; to; live "our" "dreams"; "y;all"; }
718+
inline { time; to; live "our" "dreams"; "y;all" }
719+
718720
"####;
719721

720722
let doc: KdlDocument = input.parse()?;
@@ -724,8 +726,9 @@ inline { time; to; live "our" "dreams"; "y;all"; }
724726

725727
// Now check some more interesting concrete spans
726728

727-
// The whole document should presumably be "the input" again?
728-
check_span(input, doc.span(), &input);
729+
// The whole document should be everything from the first node until the
730+
// last before_terminator whitespace.
731+
check_span(&input[1..(input.len() - 2)], doc.span(), &input);
729732

730733
// This one-liner node should be the whole line without leading whitespace
731734
let is_node = doc
@@ -772,13 +775,11 @@ inline { time; to; live "our" "dreams"; "y;all"; }
772775
);
773776

774777
// The child document is a little weird, it's the contents *inside* the braces
775-
// with extra newlines on both ends.
778+
// without the surrounding whitespace/comments. Just the actual contents.
776779
check_span(
777-
r####"{
778-
"it" /*shh*/ "has"="💯" ##"the"##
780+
r####""it" /*shh*/ "has"="💯" ##"the"##
779781
Best🎊est
780-
"syntax ever"
781-
}"####,
782+
"syntax ever""####,
782783
and_node.children().unwrap().span(),
783784
&input,
784785
);
@@ -807,14 +808,14 @@ inline { time; to; live "our" "dreams"; "y;all"; }
807808
// Make sure inline nodes work ok
808809
let inline_node = doc.get("inline").unwrap();
809810
check_span(
810-
r#"inline { time; to; live "our" "dreams"; "y;all"; }"#,
811+
r#"inline { time; to; live "our" "dreams"; "y;all" }"#,
811812
inline_node.span(),
812813
&input,
813814
);
814815

815816
let inline_children = inline_node.children().unwrap();
816817
check_span(
817-
r#"{ time; to; live "our" "dreams"; "y;all"; }"#,
818+
r#"time; to; live "our" "dreams"; "y;all" "#,
818819
inline_children.span(),
819820
&input,
820821
);

src/node.rs

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -515,28 +515,30 @@ impl KdlNode {
515515
pub(crate) fn autoformat_impl(&mut self, indent: usize, no_comments: bool) {
516516
if let Some(KdlNodeFormat {
517517
leading,
518+
before_terminator,
519+
terminator,
518520
trailing,
519521
before_children,
520522
..
521523
}) = self.format_mut()
522524
{
523525
crate::fmt::autoformat_leading(leading, indent, no_comments);
526+
crate::fmt::autoformat_trailing(before_terminator, no_comments);
524527
crate::fmt::autoformat_trailing(trailing, no_comments);
525528
*trailing = trailing.trim().into();
526-
if trailing.starts_with(';') {
527-
trailing.remove(0);
529+
if !terminator.starts_with('\n') {
530+
*terminator = "\n".into();
528531
}
529532
if let Some(c) = trailing.chars().next() {
530533
if !c.is_whitespace() {
531534
trailing.insert(0, ' ');
532535
}
533536
}
534-
trailing.push('\n');
535537

536538
*before_children = " ".into();
537539
} else {
538540
self.set_format(KdlNodeFormat {
539-
trailing: "\n".into(),
541+
terminator: "\n".into(),
540542
..Default::default()
541543
})
542544
}
@@ -598,8 +600,14 @@ impl KdlNode {
598600
}
599601
write!(f, "}}")?;
600602
}
601-
if let Some(KdlNodeFormat { trailing, .. }) = self.format() {
602-
write!(f, "{}", trailing)?;
603+
if let Some(KdlNodeFormat {
604+
before_terminator,
605+
terminator,
606+
trailing,
607+
..
608+
}) = self.format()
609+
{
610+
write!(f, "{before_terminator}{terminator}{trailing}")?;
603611
}
604612
Ok(())
605613
}
@@ -618,8 +626,11 @@ pub struct KdlNodeFormat {
618626
pub after_ty: String,
619627
/// Whitespace and comments preceding the node's children block.
620628
pub before_children: String,
621-
/// Whitespace and comments following the node itself, including the
622-
/// optional semicolon.
629+
/// Whitespace and comments right before the node's terminator.
630+
pub before_terminator: String,
631+
/// The terminator for the node.
632+
pub terminator: String,
633+
/// Whitespace and comments following the node itself, after the terminator.
623634
pub trailing: String,
624635
}
625636

@@ -652,7 +663,9 @@ mod test {
652663
node.format(),
653664
Some(&KdlNodeFormat {
654665
leading: "\n\t ".into(),
655-
trailing: ";\n".into(),
666+
before_terminator: "".into(),
667+
terminator: ";".into(),
668+
trailing: "\n".into(),
656669
before_ty_name: "".into(),
657670
after_ty_name: "".into(),
658671
after_ty: "".into(),

0 commit comments

Comments
 (0)