dart_style icon indicating copy to clipboard operation
dart_style copied to clipboard

Indentation with chained calls with long arguments is very confusing

Open Hixie opened this issue 3 years ago • 2 comments

I can't figure out what combination of commas to put in this code to make the formatting reasonable:

void assertThatImageLoadingSucceeds(NetworkImageWithRetry subject) {
  subject
      .load(
    subject,
    _ambiguate(PaintingBinding.instance)!.instantiateImageCodec,
  )
      .addListener(
    ImageStreamListener(
      expectAsync2((ImageInfo image, bool synchronousCall) {
	expect(image.image.height, 1);
	expect(image.image.width, 1);
      }),
    ),   
  );  
}

Hixie avatar Jan 08 '22 00:01 Hixie

More cases that my friend reported to me 6 months ago, https://github.com/iota9star/mikan_flutter/blob/ae591e1de035f6b845acb11b2e00b634d85b9c2a/lib/internal/http_cache_manager.dart#L46 :

final Completer<File?> completer = Completer();
_httpCacheManager
    ._(
  url,
  cacheKey: cacheKey,
  cacheDir: cacheDir,
  headers: headers,
  chunkEvents: chunkEvents,
  cancelable: cancelable,
)
    .then((value) {
  completer.complete(value);
}).catchError((dynamic error, StackTrace stackTrace) {
  completer.completeError(error, stackTrace);
}).whenComplete(() => _tasks.remove(url));

AlexV525 avatar Jan 09 '22 09:01 AlexV525

Yeah, those are both pretty bad. These are more cases where the formatting rules for argument lists with trailing commas interacts poorly with other formatting rules.

munificent avatar Jan 13 '22 17:01 munificent

Wow, yeah. These examples really hit the limitations of the old formatter. I ran them through the forthcoming tall style and got:

void assertThatImageLoadingSucceeds(NetworkImageWithRetry subject) {
  subject
      .load(
        subject,
        _ambiguate(PaintingBinding.instance)!.instantiateImageCodec,
      )
      .addListener(
        ImageStreamListener(
          expectAsync2((ImageInfo image, bool synchronousCall) {
            expect(image.image.height, 1);
            expect(image.image.width, 1);
          }),
        ),
      );
}

And:

_httpCacheManager
    ._(
      url,
      cacheKey: cacheKey,
      cacheDir: cacheDir,
      headers: headers,
      chunkEvents: chunkEvents,
      cancelable: cancelable,
    )
    .then((value) {
      completer.complete(value);
    })
    .catchError((dynamic error, StackTrace stackTrace) {
      completer.completeError(error, stackTrace);
    })
    .whenComplete(() => _tasks.remove(url));

Those look worlds better to me.

munificent avatar Aug 02 '24 21:08 munificent

That looks so much better!

Hixie avatar Aug 02 '24 22:08 Hixie