3 ms·
> I wasn’t convinced that the exhaustive approach with weigths was a good idea. It makes it computationally super intensive and hard to predict what the printer
by munificent 8y ago
> I wasn’t convinced that the exhaustive approach with weigths was a good idea. It makes it computationally super intensive and hard to predict what the printer would do.
Yes, you are right on both accounts. I'm still not entirely sure it's the right approach for dartfmt.
I started with a corpus of code that was hand-formatted by people who I felt had great style. And then I worked backwards to figure out what kinds of formatting mechanics I would need to be able to automate that.
Unfortunately, there are some very common formatting idioms in Dart that really just don't play nice with any simpler formalism. For example, it's common to use embedded function literals as if they were blocks:
group("test some stuff", () {
test("test a thing with a very long"
"multi-line description of the thing being tested", () {
expect(thing, isSomeThing);
});
});
Note here how the body of the innermost function literal does not respect the surrounding expression nesting, even though it is technically all an argument to test().
But in other cases, a function parameter should work like a normal indented expression:
things
.map((thing) {
...
})
.where((thing) => thing.isFoo);
.toList();
I probably could have said, "Well, these idioms are going away and this code will look different." and then designed a much simpler formatter. A huge part of software engineering is picking the right problem to solve and knowing when to push against requirement and when to take them as given.
For dartfmt, I did my best to keep the original requirements even thought it meant a much more complex formatter. That made my implementation job harder. But it made the political job of convincing everyone inside Google (and most users outside) to use dartfmt much easier because the output was closer to what they wanted.
- vjeux 8y ago> For dartfmt, I did my best to keep the original requirements even thought it meant a much more complex formatter. That made my implementation job harder. But it made the political job of convincing everyone inside Google (and most users outside) to use dartfmt much easier because the output was closer to what they wanted. I couldn't have said it better :) I added a ton of special cases in the step that takes an AST and outputs the IR to handle those two things (and many many more). For 1), I special case functions that look like `test(` and completely ignore the 80 column rule so that the name stays in one line. https://github.com/prettier/prettier/blob/3e0dceda9954658860ffdbc7e04465fcf5737d12/src/language-js/printer-estree.js#L1047-L1058 https://github.com/prettier/prettier/blob/3e0dceda9954658860... For 2), member chains is actually the most complex piece of prettier, but it was important to get right. There's plenty of comments in the implementation: https://github.com/prettier/prettier/blob/3e0dceda9954658860ffdbc7e04465fcf5737d12/src/language-js/printer-estree.js#L4222-L4584 https://github.com/prettier/prettier/blob/3e0dceda9954658860... I'm sure you did something similar as well :)
- munificent 8y agoYes! There is a whole separate class just to handle formatting method chains: https://github.com/dart-lang/dart_style/blob/master/lib/src/call_chain_visitor.dart https://github.com/dart-lang/dart_style/blob/master/lib/src/...
- Izkata 8y agoHm... It took me a good minute to understand what you're talking about with those two examples, because apparently my mental model is different than yours. For me, the indentation rules for those two examples are identical, because of a different rule: the adjacent-string concatenation means the two editor lines are one conceptual line.