Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Format for parsed KDL only indenting first child node #73

Open
dj95 opened this issue Apr 23, 2024 · 2 comments
Open

Format for parsed KDL only indenting first child node #73

dj95 opened this issue Apr 23, 2024 · 2 comments
Labels
bug Something isn't working

Comments

@dj95
Copy link

dj95 commented Apr 23, 2024

Hi,

I try to write a CLI, that formats KDL documents. Therefore I want to use the KdlDocument::fmt() function to achieve this.

While writing the first little unit test, I found a weird behaviour within the format output. When the following document is formatted, only the first node is indented correctly.

Input

world prop="value" {
child 1
child 2
}

Output

world prop="value" {
    child 1
child 2
}

Expected Output

world prop="value" {
    child 1
    child 2
}

Is the expected behaviour intended? For testing the issue, here is the source code for reproduction. Please feel free to message me in case you need some more details.

use kdl::KdlDocument;

pub fn format_document(input: &str) -> miette::Result<String> {
    let mut doc: KdlDocument = input.parse()?;

    doc.fmt();

    Ok(doc.to_string())
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_format_document() {
        let input = r#"
world prop="value" {
child 1
child 2
}
        "#;

        let exp = r#"world prop="value" {
    child 1
    child 2
}
"#;

        let res = format_document(input);

        assert!(res.is_ok());
        let res = res.unwrap();
        assert_eq!(res, exp);
    }
}
Screenshot 2024-04-23 at 10 37 49
@zkat zkat added the bug Something isn't working label Apr 24, 2024
@zkat
Copy link
Member

zkat commented Apr 24, 2024

oh huh. Thanks for the report. I'm rewriting the formatting stuff as part of the new kdlv2 parser rewrite, so I'll make sure this works this time around!

@dj95
Copy link
Author

dj95 commented Apr 25, 2024

Thanks a lot!

When the KdlDocument is created manually within the Rust function, it seems to format properly. So I'm not sure whether it's a bug in the parser or the formatter. Or maybe in a mix of both.

Using KdlDocument::clear_fmt_recursive() results in the correct indentation, but omits comments, as expected.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants