Skip to content

Commit

Permalink
passing the whole-ass test suite!!
Browse files Browse the repository at this point in the history
  • Loading branch information
zkat committed Oct 5, 2024
1 parent c7185fc commit 3f55cef
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 68 deletions.
31 changes: 8 additions & 23 deletions src/fmt.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,11 @@
use std::fmt::Write as _;

pub(crate) fn autoformat_leading(leading: &mut String, indent: usize, no_comments: bool) {
if leading.is_empty() {
return;
}
// TODO
let mut result = String::new();
if !no_comments {
let input = leading.trim();
if !input.is_empty() {
let (maybe_val, errs) =
crate::v2_parser::try_parse(crate::v2_parser::leading_comments, input);
let (Some(comments), true) = (maybe_val, errs.is_empty()) else {
panic!("invalid leading text");
};
for line in comments {
for line in input.lines() {
let trimmed = line.trim();
if !trimmed.is_empty() {
writeln!(result, "{:indent$}{}", "", trimmed, indent = indent).unwrap();
Expand All @@ -26,22 +17,16 @@ pub(crate) fn autoformat_leading(leading: &mut String, indent: usize, no_comment
*leading = result;
}

pub(crate) fn autoformat_trailing(decor: &mut String, _no_comments: bool) {
pub(crate) fn autoformat_trailing(decor: &mut String, no_comments: bool) {
if decor.is_empty() {
return;
}
*decor = decor.trim().to_string();
let result = String::new();
// TODO
// if !no_comments {
// let input = &*decor;
// let kdl_parser = crate::v1_parser::KdlParser { full_input: input };
// let comments = kdl_parser
// .parse(crate::v1_parser::trailing_comments(&kdl_parser))
// .expect("invalid trailing text");
// for comment in comments {
// result.push_str(comment);
// }
// }
let mut result = String::new();
if !no_comments {
for comment in decor.lines() {
writeln!(result, "{comment}").unwrap();
}
}
*decor = result;
}
4 changes: 2 additions & 2 deletions src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,7 @@ impl KdlNode {
}
trailing.push('\n');

*before_children = "".into();
*before_children = " ".into();
}
self.name.clear_format();
if let Some(ty) = self.ty.as_mut() {
Expand All @@ -544,7 +544,7 @@ impl KdlNode {
if let Some(children) = self.children.as_mut() {
children.autoformat_impl(indent + 4, no_comments);
if let Some(KdlDocumentFormat { leading, trailing }) = children.format_mut() {
leading.push('\n');
*leading = "\n".into();
trailing.push_str(format!("{:indent$}", "", indent = indent).as_str());
}
}
Expand Down
60 changes: 22 additions & 38 deletions src/v2_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,22 +212,6 @@ fn document<'s>(input: &mut Input<'s>) -> PResult<KdlDocument> {
Ok(doc)
}

// Used for formatting
pub(crate) fn leading_comments<'s>(input: &mut Input<'s>) -> PResult<Vec<&'s str>> {
terminated(
repeat(
0..,
preceded(
opt(repeat(0.., alt((newline, unicode_space)).void()).map(|()| ())),
comment,
),
)
.map(|s: Vec<&'s str>| s),
opt(repeat(0.., alt((newline, unicode_space, eof.void()))).map(|()| ())),
)
.parse_next(input)
}

/// `nodes := (line-space* node)* line-space*`
fn nodes<'s>(input: &mut Input<'s>) -> PResult<KdlDocument> {
let ((leading, nodes, trailing), _span) = (
Expand Down Expand Up @@ -1166,28 +1150,28 @@ fn ws<'s>(input: &mut Input<'s>) -> PResult<()> {
alt((unicode_space, multi_line_comment)).parse_next(input)
}

fn comment<'s>(input: &mut Input<'s>) -> PResult<&'s str> {
alt((
single_line_comment.take(),
multi_line_comment.take(),
(
"/-",
repeat(0.., plain_node_space).map(|_: ()| ()),
cut_err(node),
)
.take(),
(
"/-",
repeat(0.., plain_node_space).map(|_: ()| ()),
cut_err(alt((
node_entry.void().context(lbl("slashdashed entry")),
node_children.void().context(lbl("slashdashed children")),
))),
)
.take(),
))
.parse_next(input)
}
// fn comment<'s>(input: &mut Input<'s>) -> PResult<&'s str> {
// alt((
// single_line_comment.take(),
// multi_line_comment.take(),
// (
// "/-",
// repeat(0.., plain_node_space).map(|_: ()| ()),
// cut_err(node),
// )
// .take(),
// (
// "/-",
// repeat(0.., plain_node_space).map(|_: ()| ()),
// cut_err(alt((
// node_entry.void().context(lbl("slashdashed entry")),
// node_children.void().context(lbl("slashdashed children")),
// ))),
// )
// .take(),
// ))
// .parse_next(input)
// }

static UNICODE_SPACES: [char; 19] = [
'\u{0009}', '\u{000B}', '\u{0020}', '\u{00A0}', '\u{1680}', '\u{2000}', '\u{2001}', '\u{2002}',
Expand Down
9 changes: 4 additions & 5 deletions src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,6 @@ impl KdlValue {
consecutive = 0;
}
}
write!(f, "r")?;
write!(f, "{}", "#".repeat(maxhash))?;
write!(f, "\"{}\"", raw)?;
write!(f, "{}", "#".repeat(maxhash))?;
Expand Down Expand Up @@ -361,10 +360,10 @@ mod test {

#[test]
fn formatting() {
let raw = KdlValue::RawString(r###"r##"foor#"bar"#baz"##"###.into());
let raw = KdlValue::RawString(r###"##"foo#"bar"#baz"##"###.into());
assert_eq!(
format!("{}", raw),
r####"r###"r##"foor#"bar"#baz"##"###"####
r####"###"##"foo#"bar"#baz"##"###"####
);

let string = KdlValue::String("foo\n".into());
Expand All @@ -386,9 +385,9 @@ mod test {
assert_eq!(format!("{}", base16), "0x1234567890abcdef");

let boolean = KdlValue::Bool(true);
assert_eq!(format!("{}", boolean), "true");
assert_eq!(format!("{}", boolean), "#true");

let null = KdlValue::Null;
assert_eq!(format!("{}", null), "null");
assert_eq!(format!("{}", null), "#null");
}
}

0 comments on commit 3f55cef

Please sign in to comment.