-
Notifications
You must be signed in to change notification settings - Fork 854
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
Question: Does there exist a TOML formatter? #532
Comments
The short answer is, there isn't one, for some well-founded reasons. If you want a formatter now, you can grab a TOML decoder library that works, then grab a TOML emitter library that will do what you want, and then put them together and tweak them until you've made your ideal formatter, which you can use on all your TOML files. Assuming you want that. This is where the difference kicks in. For programming code, a formatter makes good sense. Uniformity makes code more readable. You mention But what does a properly formatted configuration file look like? We don't have a canonical representation for all configuration data, and if we imposed one, the resulting equivalent TOML files could be less friendly to human eyeballs and human editors than the original TOML file. In fact, we may want certain TOML features in one application's configuration (like hex integer formats) that would look out of place in the configurations for other applications (like for astronomical data processing). We need to ask some other questions first.
Any formatter worth its salt would have detailed answers to both of these questions and would apply its principles mercilessly. And good luck finding a single set of principles that would satisfy everybody. |
Your proposed implementation would lose comments which is imho a non-starter. Programming code formatters usually do not change algorithm representation either: a language may have multiple ways to iterate over a collection, yet formatters will not change one to another (they can be coupled with linters which will emit warnings, but those have different scope). They mostly format a given syntax tree by inserting or removing blank characters. There is definitely a use for this in TOML. For example a consistent indentation style (be it “never indent”, “indent subtables”, “indent in tables” or anything one might come up with) makes reading a file much easier than having mixed style, as it does for programming code. For best practices, many formatters end with a configuration file because different peoples want different styles. One could start implementing a TOML formatter with mandatory configuration (and being open for new values), then add some default once they get user feedback. |
There's https://github.com/jumpscale7/python-pretty-toml. No clue how it is though. |
@alok has your question been answered? If so, please feel free to close this issue. :) |
All the problems with formatting that @eksortso mentions apply to formatting code too, and yet, against all odds, code formatters exist. Personally, i just want to know whether i should put spaces inside the braces of inline tables or not. I tried the Python prettytoml thing, but found that it preserved the whitespace in inline tables! Oh, well, for now i will maintain artisanal hand-formatted configuration files. |
I've created a prettier plugin for TOML. I've also implemented my own Toml parser in JavaScript to avoid the comment losing issue described above. It is still at an early stage and feedback would be highly appreciated 😄 |
I've not found one and I'd like something like the
gofmt
of TOML, if it exists.The text was updated successfully, but these errors were encountered: