Open sin-ack opened 2 months ago
there doesn't seem to be any way to automatically format a Pkl file
This isn't technically true; pkl-intellij
has a formatter. Sadly, that's far too deeply integrated with the IDE SDKs to use in CI and VCS hooks and such.
The current front-end is slightly too deeply integrated with the interpreter. We're working on improving that, so that this kind of tool is easier to build/maintain. It's certainly on our radar.
Hello!
I want to mention that we had frustrations when developing a formatter for the Nickel configuration language. Our parsing infrastructure wasn't really geared toward formatting (which requires more information in the output, i.e. a CST instead of an AST), and wiping the parser entirely for something different sounded like a lot of work.
So we developed Topiary, which is a stand-alone multi-language formatter based on tree-sitter. That is, if you already have a tree-sitter grammar lying around for editor highlighting (or you plan to write one), you can reuse it to create a formatter by writing additional specific tree sitter queries (here is the formatting queries for Nickel for example - it's 500 lines but with many comments and lists with one element per line). On caveat is that the grammar must be fine-grained enough: for highlighting, one can get away with a very coarse grammar specification, but formatting might be more demanding.
Topiary can then be used as a separate tool for formatting, or even embedded as e.g. pkl format
(this is currently the case for nickel format
which just links to Topiary under the hood). Although I must say, even if Rust is rather easy to FFI into because of its absence of runtime, I don't how well that would work with a Java codebase like Pkl (especially because Topiary also binds to tree-sitter, which means at least bringing C into the picture, and maybe C++, I don't remember exactly).
I hope it's not out of place or that it doesn't sound too much like advertising. It's just that one motivation of Topiary is to create formatters for new languages without too much hassle (by delegating parsing and pretty-printing to tree-sitter and just focusing on transforming the source tree, under the assumption that you'll have to write a tree-sitter grammar anyway for editor support), so it might be a possible direction to explore for Pkl :slightly_smiling_face:
Problem Statement
Currently, there doesn't seem to be any way to automatically format a Pkl file. This means that we cannot enforce a standard style when collaborating in a team, nor automatically flag violations in PRs.
Proposed Solution
Something like
pkl format
, replicating the CLI of Prettier. Examples:Open Questions
;
separators?Relevant Links
Discussion in February: https://github.com/apple/pkl/discussions/174