4

I've been trying different configurations in the code-formatting settings from Android-Studio, but the results have been pretty disappointing (and triggering) so far.

This is what the reformat code does without the dartfmt tool (notice line 94 - 110): enter image description here

dartfmt does a better job of it, but the indentation is horrible (line 91 - 96) and very inconsistent with seemingly no settings to configure for it: enter image description here

This is what I would want it to look like: enter image description here

What settings do I need to change to achieve this? It's currently pretty hard to read the code.

4 Answers 4

12

You can't configure dartfmt. This is voluntary. But dartfmt uses trailing comma as it's core to determine where to go to newline.

The following :

foo({String foo, String bar}) {}

void main() {
  foo(foo: "Hello", bar: "Hello");
}

will stay unchanged when running dartfmt.

But simply changing to foo call to :

  ...bar: "Hello",);

will reformat it to :

foo({String foo, String bar}) {}

void main() {
  foo(
    foo: "Hello",
    bar: "Hello",
  );
}
Sign up to request clarification or add additional context in comments.

5 Comments

I'm not sure how this relates to the indentation of dartfmt being very inconsistent
Because the output you desire can be achieved by adding a trailing comma where you didn't put one
line 91 - 96 doesn't really have any code where a trailing comma can be placed. And I want the indentation to always be 4 spaces, not sometimes 4 and sometimes 2
You can't change such thing. But this will solve your problem with parenthesis
i can't believe, that was it. For hours I was trying to figure out why wasn't the formatting works. It's the d*rn commas. Omg, thank you
8

It is too late but it can help.

In Android Studio go to

File > Settings > Editor > Code Style > Dart

and increase the value of Line Length.

Comments

2

Deeply nested code is quite challenging to format and when the remaining space in lines become too short dartformat unindents to be able to format the rest of the code better.

It's better to avoid deeply nested code and instead refactor parts into their own function/method or if it is a Flutter build method, into widgets.

If you still think the behavior is invalid, please create an issue in dart-lang/dart_style and the maintainer will very likely respond with a profound answer.

2 Comments

I'm surprised that the internal formatter isn't able to properly format it either though, considering it has always worked for Java and PHP. But I guess there's not much else to it then
Uh, Java is so long in the past. There were no closures back then. No idea if you could write comparable code now in Java. As mentioned, if you want a more profound answer, create an issue. Bob usually explains what rules are at play and what tradeoffs were accepted for what reason. He's a bit swamped lately though, so no promises.
1

If you are an Android Developer and cannot leave Android Studio, since it's your coding home, but can't let go of flutter either.

Changing tab size won't work through Default.xml in codestyles.

Use a different theme: Visual Studio 2019 Dark Theme (this auto adjusts indentation and makes code look exactly as of Visual Studio Code). To install theme - plugins -> search "Visual Studio 2019 Dark Theme"

OR

Use a different font: I prefer

Font : "Droid Sans Mono Slashed" or "Monospaced" (You can use any that works for spacing)

Font size: 18 , Line Height 1.4 // For 14 inch screens

Font size: 14 , Line Height 1.2 // For 15.6 inch screens or larger

enter image description here

OR

A combination of above looks beautiful.

enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.