Skip to content
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

Incorrect formatting with function chaining. #1135

Closed
AlexanderFarkas opened this issue Sep 11, 2022 · 4 comments
Closed

Incorrect formatting with function chaining. #1135

AlexanderFarkas opened this issue Sep 11, 2022 · 4 comments

Comments

@AlexanderFarkas
Copy link

AlexanderFarkas commented Sep 11, 2022

Formatted with dart format:
Screenshot 2022-09-11 at 15 41 53

Seems, that correct formatting is:
Screenshot 2022-09-11 at 15 42 21

Dart SDK version: 2.17.6 (stable) on "macos_arm64"

@munificent
Copy link
Member

This is working more or less as intended, but I agree the results don't look great. The formatter has heuristics around when to use "block-like" indentation for function expressions that appear as function arguments and when to use normal expression indentation. Those heuristics work pretty well in most cases, but not great in some like here.

In particular, the main reason why the indentation is inconsistent in your example is because the first function expression uses => while the others use block bodies. You'll get more consistent formatting across the entire method chain if you use () { ... } for all three.

Would you mind including this code as text in the issue and not a screenshot? If I get a chance to tweak these heuristics, I would like to use this as a regression test but don't want to have to retype it all by hand.

@ghost
Copy link

ghost commented Dec 28, 2023

I captured above screenshot as text in case it is useful:
(as formatted with Dart extension in VSCode)

  file
      .readAsBytes()
      .then(
        (bytes) => ApiClient.instance.files.upload(
          ownershipType: ownershipType,
          file: MultipartFile.fromBytes(
            bytes,
            filename: filename,
          ),
        ),
      )
      .then(
    (storageFile) {
      _value = _value.add(id, FileUpload.fromStorageFile(storageFile));
      notifyListeners();
    },
  ).catchError(
    (Object e) {
      _value = _value.add(id, FileUpload.error(error: e, name: filename));
      notifyListeners();
    },
  );

@munificent
Copy link
Member

I captured above screenshot as text in case it is useful:

It is, thank you!

@munificent
Copy link
Member

I ran the example through the forthcoming tall style and it produces:

  file
      .readAsBytes()
      .then(
        (bytes) => ApiClient.instance.files.upload(
          ownershipType: ownershipType,
          file: MultipartFile.fromBytes(bytes, filename: filename),
        ),
      )
      .then((storageFile) {
        _value = _value.add(id, FileUpload.fromStorageFile(storageFile));
        notifyListeners();
      })
      .catchError((Object e) {
        _value = _value.add(id, FileUpload.error(error: e, name: filename));
        notifyListeners();
      });

I believe that's exactly the requested output, so I think we're good.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants