Change Dart Formatting style
Asked Answered
A

4

7

Okay, so the title may be slightly misleading, This is simply my end goal.

So, I'm just getting into the world of Dart. So far it seems great, but my biggest issue with it is the stupid default formatter. In particular that you can't get rid of it. I'm using WebStorm mainly because of how customisable these things tend to be. But I haven't found any way to change the formatter for dart (see attached screenshot). The Dart formatter enforces 2-space indentations and many other infuriating code styles which are only enforced by it.

Is it possible to somehow change either the configuration options or the formatter all together? If the change has to be made to source, that's doable, or simply playing around with some WebStorm configurations, that's fine also, I just want to change this.

Thanks

As you can see, There is no way to configure anything other than the line length

Autoharp answered 19/1, 2020 at 23:31 Comment(0)
D
3

The Dart formatter respects linter style rules as laid out in the "analysis_options.yaml" file. A description of the file can be found here. A complete list of linter rules can be found here.

You can also subscribe to common style guides, such as pedantic, a package with the style guide used internally by Google, or lint, another package that enables all the rules that aren't either contradictory or opinionated.

That being said, I don't think you can change things like the two-space indentation. There are certain style choices that dartfmt enforces in the interest of maintaining as much style consistency between Dart codebases as possible.

Dworman answered 20/1, 2020 at 0:13 Comment(5)
So the linter provides some flexibility, but not an awful lot. It's nothing like what you can do with ESLint or Prettier.Autoharp
@JacobSchneider Tools like ESLint and Prettier are so flexible because Javascript doesn't have any single style that one can look at and say "that's Javascript style". Google wanted to address that from the onset, which is why they made dartfmt so rigid and limited customizability of the style so much.Dworman
I just want new line before & after every braces, no matter what it is an opening or closing. Is there any solution for it?Plummer
@soorejbabu No there is not. The formatter is rigid in its implementation and there is no customization option. This is intentional and by design. The whole point of the formatter is to enforce a specific code style so that Dart code looks similar no matter where it is found which goes a long way toward helping people read each other's code. Yes, it's opinionated, and yes, people will disagree with this approach, but that's just the way it is.Dworman
@SoorejBabu Check out my dart_format package on pub.dev. Maybe it can help you.Christiachristian
H
4

You can't change the formatting style used by dartfmt and that was a very intentional design decision. If you look at the FAQ for package:dart_style you'll find the following under the "Why can't I configure it?" section:

The formatter supports very few tweakable settings, by design. If you look up at the list of priorities above, you'll see configurability goes directly against the first two priorities, and halfway against the third (you have to think about it, but not apply it).

This may be surprising, but the goal of dartfmt is not to automatically make your code look the way you like. It's to make everyone's Dart code look the same. The primary goal of dartfmt is to improve the quality of the Dart ecosystem. That transitively improves the live's of each Dart developer as well—you get more code to reuse, more consistent code, it's easier to read and contribute to each other's code, etc. But it does that at the expense of individual preference.

Hinge answered 20/1, 2020 at 0:2 Comment(1)
Thanks, I am aware the formatter is deliberately unconfigurable, but I'm wondering if there are alternatives that I can use?Autoharp
D
3

The Dart formatter respects linter style rules as laid out in the "analysis_options.yaml" file. A description of the file can be found here. A complete list of linter rules can be found here.

You can also subscribe to common style guides, such as pedantic, a package with the style guide used internally by Google, or lint, another package that enables all the rules that aren't either contradictory or opinionated.

That being said, I don't think you can change things like the two-space indentation. There are certain style choices that dartfmt enforces in the interest of maintaining as much style consistency between Dart codebases as possible.

Dworman answered 20/1, 2020 at 0:13 Comment(5)
So the linter provides some flexibility, but not an awful lot. It's nothing like what you can do with ESLint or Prettier.Autoharp
@JacobSchneider Tools like ESLint and Prettier are so flexible because Javascript doesn't have any single style that one can look at and say "that's Javascript style". Google wanted to address that from the onset, which is why they made dartfmt so rigid and limited customizability of the style so much.Dworman
I just want new line before & after every braces, no matter what it is an opening or closing. Is there any solution for it?Plummer
@soorejbabu No there is not. The formatter is rigid in its implementation and there is no customization option. This is intentional and by design. The whole point of the formatter is to enforce a specific code style so that Dart code looks similar no matter where it is found which goes a long way toward helping people read each other's code. Yes, it's opinionated, and yes, people will disagree with this approach, but that's just the way it is.Dworman
@SoorejBabu Check out my dart_format package on pub.dev. Maybe it can help you.Christiachristian
C
3

Looking at the source, there appear to be a few intentionally hidden options for dart format, including --line-length and --indentation.

Example:

dart format --line-length 120 --indentation 4

I am not sure if that addresses your issue, my use case was more CI related.

Convection answered 4/10, 2023 at 19:34 Comment(1)
Wow tbh I'd completely forgotten about this question. I've since rejected Dart in its entirety, mostly for reasons like these. However in my recent strides in my understanding of OS infrastructure, it seams likely to me that this would address the original question, perhaps not directly, but certainly brings it to a hackable point, where the desired result could at least be achieved. Thank u sirAutoharp
C
0

You can use third-party formatters if you don't like the built in limitations. Some are very customizable and make your code much more readable IMHO:

Unformatted

Formatted

E.g. this one for VSCode https://marketplace.visualstudio.com/items?itemName=eggnstone.DartFormat and IntelliJ/AndroidStudio https://plugins.jetbrains.com/plugin/21003-dartformat

Full disclosure: I wrote these plugins but I don't earn anything from it. I just want to help fellow developers who are as frustrated as I was.

Christiachristian answered 6/9, 2024 at 6:50 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.