prql icon indicating copy to clipboard operation
prql copied to clipboard

feat(fmt): An attempt at aesthetic items into PL

Open max-sixty opened this issue 3 weeks ago • 3 comments

By adding comments (named "aesthetics" here, and includes linewraps) to PL, this is an attempt to get around the complications of combining lexer + parser output in prqlc fmt, which #4397 has hit in a few incarnations.

This very very nearly works — with chumsky we can create a function that wraps anything that might have a trailing or following comment, implement a trait on the AST items that contain it — and away we go. (though it did require lots of debugging in the end...). The AST would then be really easy to write back out.

This requires comments to lead or follow tokens that are part of an AST item. I think there's literally a single case where it doesn't work, which is when a comment follows the final trailing comma of a tuple or array. So apart from that case, a comment always leads or follows a token that's part of an AST item.

...so tests fail at the moment, on that case.

Next we need to consider:

  • Can we workaround that one case? We don't actually care about whether there's a trailing comma — it has no semantic meaning — and we're going to override that when we write it out, so we could likely hack around it...
  • Are there actually other cases of this model failing? I know this approach — of putting aesthetic items into AST — is not generally favored. And at a meta level we should be skeptical of the claim "there's just a single case of something not working" — bad models generally have multiple failures!

max-sixty avatar Jun 20 '24 00:06 max-sixty