7 ms·
We're in the middle of a rewrite of the formatter, so my answer will be sort of hand-wavey to try to cover how both of them work. Basically, we take the incomi
by munificent 3y ago
We're in the middle of a rewrite of the formatter, so my answer will be sort of hand-wavey to try to cover how both of them work.
Basically, we take the incoming tree and convert it to an internal representation that has some nesting structuring and understands places where line splitting choices can be made.
When a mandatory newline (like from a line comment or multi-line string) occurs, we walk outwards to the surrounding IR nodes ("chunks" in the old formatter, "pieces" in the new one) and tell them that a newline has occurred.
In the old formatter, that tells the surrounding rules to "harden", which turns them into the maximally split form. In the new formatter, each piece can choose whether or not it allows newlines in different contexts. If it gets word that a newline occurs where not allowed, that solution is invalidated and the solver goes looking for a better one.