Implementing a whitespace formatter powered by a Lezer grammar

Hi, we wanted to implement a custom whitespace formatter powered by a Lezer grammar, for a PostgreSQL esque language. We also want this to work as a VS Code extension.

For this, is getting a parse tree, printing it recursively with appropriate whitespaces inserted, according to specified conditions for specific tokens, a possible approach to implement this? If possible, is it a good approach?

Thank you!

Should be possible. But not easy, I expect—good formatters for most languages are really hard to write, and that difficulty is mostly in the decisions about where line breaks go, not the parsing.

Thanks for the reply!

Ah okay. Actually we plan to use methods employed by sql-formatter-plus for deciding on where to insert line breaks.