Zenko
Zenko

Reputation: 2559

How do I make Dartfmt "friendly" for Matrices?

For a Matrix that supposed to look like this:

const ColorFilter sepia = ColorFilter.matrix(<double>[
          0.393, 0.769, 0.189, 0, 0,
          0.349, 0.686, 0.168, 0, 0,
          0.272, 0.534, 0.131, 0, 0,
          0,     0,     0,     1, 0,
        ]);

But dartfmt changed it to become like this:

const ColorFilter sepia = ColorFilter.matrix(<double>[
          0.393,
          0.769,
          0.189,
          0,
          0,
          0.349,
          0.686,
          0.168,
          0,
          0,
          0.272,
          0.534,
          0.131,
          0,
          0,
          0,
          0,
          0,
          1,
          0,
        ]);

This is hard to read. Thus, how can I keep the original format so that the Matrix can be seen more "friendly". Or at least how can I make Dartfmt not to reformat any List?

Upvotes: 1

Views: 68

Answers (2)

Augustin R
Augustin R

Reputation: 7809

Whereas @julemand101's answer should be the accepted one, as it directly changes dartfmt's behavior, this spread operator workaround could be worth to mention :

final matrix = [
   ...[1, 2, 3],
   ...[4, 5, 6],
   ...[7, 8, 9],
];

Upvotes: 0

julemand101
julemand101

Reputation: 31259

This scenario is described in the FAQ of dart_style which dartfmt is based on:

https://github.com/dart-lang/dart_style/wiki/FAQ#why-does-the-formatter-mess-up-my-collection-literals

In short, you just need to add a comment somewhere in you matrix definition like:

const ColorFilter sepia = ColorFilter.matrix(<double>[
          0.393, 0.769, 0.189, 0, 0, //
          0.349, 0.686, 0.168, 0, 0,
          0.272, 0.534, 0.131, 0, 0,
          0,     0,     0,     1, 0,
        ]);

Then dartfmt will not try to format the newlines in the matrix. It will however, still fixes non-needed spaces so it will make your example into:

const ColorFilter sepia = ColorFilter.matrix(<double>[
  0.393, 0.769, 0.189, 0, 0, //
  0.349, 0.686, 0.168, 0, 0,
  0.272, 0.534, 0.131, 0, 0,
  0, 0, 0, 1, 0,
]);

Which can be fixed by changing the 0 to 0.000:

const ColorFilter sepia = ColorFilter.matrix(<double>[
  0.393, 0.769, 0.189, 0, 0, //
  0.349, 0.686, 0.168, 0, 0,
  0.272, 0.534, 0.131, 0, 0,
  0.000, 0.000, 0.000, 1, 0,
]);

Upvotes: 3

Related Questions