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

Configurable autoformat with FormatConfig #95

Merged
merged 4 commits into from
Dec 6, 2024

Conversation

Tamschi
Copy link
Contributor

@Tamschi Tamschi commented Dec 6, 2024

This implements the discussion in #85, i.e. (KdlDocument|KdlNode)::autoformat_config(&mut self, config: &FormatConfig) (replacing the respective internal autoformat_impl functions), with small changes to the FormatConfig fields.

The first commit adds just the configurable formatting with tests, the second one adds a builder API to FormatConfig.

pub indent_level: usize,

/// The indentation to use at each level. Defaults to four spaces.
pub indent: &'a str,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I went with just indent for the field name here, and a &str for repetitions of the same character.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alternatively, this could be an enum of Tab and Spaces(usize) to constrain it to valid options 🤔

Cargo.toml Outdated
@@ -16,6 +16,7 @@ default = ["span"]
span = []

[dependencies]
bon = { version = "2.1.0", default-features = false } # 3.2.0 requires rustc 1.59.0
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This pulls in quite a few dependencies for the procedural macro.
Unfortunately, making it optional would turn the code in fmt quite messy.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, if it pulls in a lot, maybe it's better to hand-write the builder. It's really not a lot of trouble, even if it feels like a bit of boilerplate, imo.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or actually, if the config is defaultable, just don't bother with the builder at all, tbh.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will do 👍

Yes, it pulls in Syn. (I think it would be helpful to commit Cargo.lock to the repository, in part to see those changes in PRs.)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or actually, if the config is defaultable, just don't bother with the builder at all, tbh.

Oh, I missed this comment. Should I remove the builder?

I think it might be convenient to have the defaults const-available though.

@Tamschi
Copy link
Contributor Author

Tamschi commented Dec 6, 2024

I allowed the caller to control outer indentation here (which may be useful for nodes.
I recently wrote a JSON formatter that collapses certain paths, for example).

Is that expected or would you prefer if only the indentation per level can be chosen?

…actually yeah I misunderstood how this works, I'll fix it.

@Tamschi Tamschi marked this pull request as draft December 6, 2024 12:12
@Tamschi Tamschi marked this pull request as ready for review December 6, 2024 12:20
@Tamschi
Copy link
Contributor Author

Tamschi commented Dec 6, 2024

I misunderstood how I misunderstood 😅
Yes, I think as long as autoformat_config should replace autoformat_impl, it's most straightforward to expose that.

I could do an alternative approach where autoformat_impl is retained, if needed.

@zkat
Copy link
Member

zkat commented Dec 6, 2024

This looks really good tbh. Like I said in a separate comment, I honestly don't think we need a builder at all for this. Bon definitely does not seem worth it for this tiny config struct, either.

Also: how would you feel about addressing #64 while you're at it? It can be done in a separate PR by whoever if you want to keep this one more focused, though.

Fortunately really not too bad here, since every field is defaulted.
This is less validating than what Bon generates though, since now there are no safeguards against repeating settings. On the plus side, it's completely `const`.
@Tamschi
Copy link
Contributor Author

Tamschi commented Dec 6, 2024

I replaced the builder implementation with a manual one. It now doesn't validate that the fields don't repeat (because that would be messy enough to warrant the dependency), but on the plus side it's all-const fn now.

Also: how would you feel about addressing #64 while you're at it? It can be done in a separate PR by whoever if you want to keep this one more focused, though.

It would be more complicated since it definitely should auto-detect if you add options there.
Most popular (programming-related) Windows tooling leaves \n alone now when it detects that, even notepad.exe iinm.

I don't think I'll implement that 😅

Copy link
Member

@zkat zkat left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good then! Thanks a bunch!

@zkat zkat merged commit 014c7c5 into kdl-org:main Dec 6, 2024
@Tamschi Tamschi deleted the Tamschi/configurable-autoformat branch December 8, 2024 10:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants