2 ms·
> 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 he
by munificent 11y ago
> 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.
- ridiculous_fish 11y agoI'm not sure you understood my example. Let's say we have: while (first && second) { third } We might assign nesting levels like this: while( 1 first && 2 second) 2 third 2 which could lead to this line breaking: while (first && second) { third } This is bad because 'second' and 'third' are visually aligned, and so one might think that 'second' is in the loop body instead of the condition. We want to indent 'second' more than 'third', even though it has the same nesting level: while (first && second) { third } This is what I meant by "context dependent:" here we have two chunks at the same indentation level but that want different numbers of spaces. Does dartfmt attempt to handle this?
- munificent 11y agoAh, the style rules address this case. Bodies are indented +2 and wrapped expressions are +4. That means you will always get: while (first && second) { third // <-- } So the body is indented less than the wrapped condition. Expression nesting is considered different from block nesting.