> Does dartfmt handle this? If not, do you have ideas on how it might?
Yes, it does! Correct indentation based for nested expressions is a vital feature for helping the reader understand the structure of the code.
The basic idea is fairly simple. Any place a line break may appear, you mark it with a number representing how deeply nested in the expression it is. So in code like:
function(outer(inner(first, second), third));
You would get chunks whose expression nesting level is like:
function( 1
outer( 2
inner( 3
first, 3
second), 2
third));
When you line break, you ensure that its nesting level gets assigned a correct indentation level. Deeper nesting means deeper indentation:
function(
outer(
inner(
first,
second),
third));
There are a bunch of interesting edge cases, though. Some times a nesting level doesn't happen on a line break, so it doesn't need to get an indentation level associated with it:
function(outer(inner(
first,
second), third));
Here, levels 1 and 2 don't appear in line breaks, so we give the first indentation level to nesting level 3. There are stranger cases where you may assign a deeper level before a shallower one like:
function(outer(inner(
first,
second),
third));
Shaking out all of the bugs requires a lot of work and a really big test suite.
> Also, how does it handle invalid input? I may want to reindent my code before it's correct.
If the code doesn't parse, it just exits with an error. Being able to run it on incomplete input would be useful, but I decided it was out of scope.
> Also, did you explore constraint solvers instead of a graph traversal?
Good question! I have a little experience with them. I think the line between the two is sort of fuzzy. I didn't approach it directly like a constraint solving problem, but it does have some features in common.