For this sort of things I&#x27;m using an intermediate language (guess it&#x27;s similar to what OP is talking about) with stack semantics: there are instructions like &quot;PUSHINDENT&quot;, &#x27;POPINDENT&quot; and &quot;INCINDENT&quot; - with PUSHINDENT inserted after &#x27;(&#x27; in ifs, function calls, etc. - ensuring that if line breaks before the POP instruction, indentation would reflect the context.<p>So, the biggest problem is in assigning weights to the break candidate instructions is this stream. This can only be done by a lot of experimenting, I could not find any formal method or a passable heuristic.