-
Notifications
You must be signed in to change notification settings - Fork 122
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unsplit list literal in method chain formats poorly #731
Comments
I actually prefer the first format where every |
I could live with the same indentation but look where it put the |
I do find it puzzling to see that var placeManagerUpdateResults___________________________ = [
sl.get<PlaceManager>().updateFilterCommand.results
];
var filterUpdateSubscription = sl
.get<EventManager>()
.updateFilterCommand
.results
.mergeWith(placeManagerUpdateResults___________________________)
.listen((filterState) async {
AppKeys.mapView?.currentState?.activateMapOverlay();
await Future.delayed(Duration(milliseconds: 100));
setState(() {
filterIsActive = filterState;
});
}); |
@srawlins That would make it much better although I wished the handler function block of the listen would be indented more than the . |
Could it be a solution to give dartfmt a hint where to break a line by inserting a space before a '.' ? Like we do with trailing ',' |
It is a non-goal to let users opt out of formatting. I agree the output in this particular case is not great. :( Like @srawlins notes, it has to do with the list literal. dartfmt has some surprisingly complex rules to handle function and collection literals inside arguments in order to more gracefully handle cases like: function([
element,
element,
element,
element,
element,
element,
]).method(); Most users prefer that over: function([
element,
element,
element,
element,
element,
element,
])
.method(); Unfortunately, due to some limitations in the line splitting algorithm, it can't currently gracefully switch between allowing a trailing method call on the same line when the list literal does split while preventing it when it doesn't. So in your example, it sees the list literal, and prepares to format it like: filterUpdateSubscription =
sl.get<EventManager>().updateFilterCommand.results.mergeWith([
sl.get<PlaceManager>().updateFilterCommand.results
]).listen((filterState) async {
AppKeys.mapView?.currentState?.activateMapOverlay();
await Future.delayed(Duration(milliseconds: 100));
setState(() {
filterIsActive = filterState;
});
}); But, then, because the list literal does all fit in one line, it switches things around, but still leaves the It's something I'd really like to improve, but I haven't been able to yet. |
What about the idea to give dartfmt a hint like we can do by adding a trailing comma? So that by adding a space dartfmt knows where to break the line? Another question: Why does dartfmt not indent the block of anonymous functions so that its end brace is not on the same depth as the rest of the surrounding code? |
I consider the trailing comma "hint" to be a major problem in dartfmt. I increasingly see code that inconsistently applies trailing commas and ends up with wildly inconsistent formatting in a single file.
It looks better to format it like a block in a lot of common cases. Consider: group('textBeforeSelection', () {
test('gets substring before selection', () {
expect(selection.textBeforeSelection, equals("123"));
});
test('gets entire string if no selection', () {
expect(noSelection.textBeforeSelection, equals("123456;"));
});
}); Versus: group(
'textBeforeSelection',
() {
test(
'gets substring before selection',
() {
expect(selection.textBeforeSelection, equals("123"));
});
test(
'gets entire string if no selection',
() {
expect(noSelection.textBeforeSelection, equals("123456;"));
});
}); |
No, that would be ugly I meant just something like this group('textBeforeSelection', () {
test('gets substring before selection', () {
expect(selection.textBeforeSelection, equals("123"));
});
someOtherMethodCall();
test('gets entire string if no selection', () {
expect(noSelection.textBeforeSelection, equals("123456;"));
});
}); |
Why are the bodies of |
Sorry, the example was wrong. I was more thinginh about the block of anonymous function blocks like return Observable(_databaseService.getChatEntries(
event,
)).map((entryList) {
entryList.forEach(
(entry) => entry.isFromCurrentUser = (entry.userId == _userManager.currentUser.id));
return entryList;
}); vs. return Observable(_databaseService.getChatEntries(
event,
)).map((entryList) {
entryList.forEach(
(entry) => entry.isFromCurrentUser = (entry.userId == _userManager.currentUser.id));
return entryList;
}); |
The 2nd variant would make me very nervous. Adding
|
It's pretty common to chain higher-order functions. In your example, would they march farther and farther to the right? return Observable(_databaseService.getChatEntries(
event,
)).map((entryList) {
entryList.forEach(
(entry) => entry.isFromCurrentUser = (entry.userId == _userManager.currentUser.id));
return entryList;
}).where((entry) {
return entry != null;
}).map((entry) {
return entry.toString();
}); I don't think many users would be happy with that. |
Which is quite usual in other programming languages. Especially with the block brackets it is way better readable |
Earlier, I said:
One of my main goals with designing the new intermediate represention and line splitter was to fix this exact limitation. With the forthcoming tall style, you get: filterUpdateSubscription = sl
.get<EventManager>()
.updateFilterCommand
.results
.mergeWith([sl.get<PlaceManager>().updateFilterCommand.results])
.listen((filterState) async {
AppKeys.mapView?.currentState?.activateMapOverlay();
await Future.delayed(Duration(milliseconds: 100));
setState(() {
filterIsActive = filterState;
});
}); That looks pretty good to me. |
Whe using fluent function calls the results of dartfmt isn't optimal Especially when using RxDart this produces real ugly results like:
Where this
Would be much easier to read.
I know that dartfmt has the goal of making all dart code look the same but if this works against readability of the intent it's not a good philosophy.
I would propose a simple option to be able to tell dartfmt to not touch lines beginning with a '.'
The text was updated successfully, but these errors were encountered: