A secondary problem is what to do when the line length limit is exceeded, such as for long expressions.I think that's problem #1.
The usual way is to associate cost with various cesures and run something Dijkstra (or A*) like to find the best cesures.