dart_style icon indicating copy to clipboard operation
dart_style copied to clipboard

Better code formatting for flow controls in Maps

Open escamoteur opened this issue 10 months ago • 3 comments

I recently stumbled upon this piece of code from a colleague of mine and had real problems at first to understand what is going on here:

grafik

One reason for sure was that I wasn't aware that we can use if and for not only inside Lists but also for Maps which is indeed a pretty cool feature :-) the bigger reason IMHO is that the way dartformat currently treats this makes it hard to spot where the key of the created/inserted pair is. To be honest I don't have a good idea of how to do this better. Maybe outdent the key? Maybe @munificent has an idea of how to improve this.

escamoteur avatar Apr 18 '24 08:04 escamoteur

Maybe the issue should be moved to: https://github.com/dart-lang/dart_style/issues ?

julemand101 avatar Apr 18 '24 12:04 julemand101

Oh definitely, I didn't know that there is a separate repository for the formatted. So please anyone who can move an issue please move it there?

escamoteur avatar Apr 18 '24 12:04 escamoteur

I agree! I would like to insert a new line after a conditional or loop statement and indent the key. Here's an example of how it could look:

final current = {
  if (true) 'a': 1,
  for (final i in [1, 2, 3]) 'b$i': i,
  for (final nested in foo1)
    for (final i in nested) 'c$i': i,
  for (final nested in foo2) ...nested,
};
final proposed = {
  if (true)
    'a': 1,
  for (var i in [1, 2, 3])
    'b$i': i,
  for (final nested in foo1)
    for (final i in nested)
      'c$i': i,
  for (final nested in foo2)
    ...nested,
};

VojtaStanek avatar Apr 24 '24 09:04 VojtaStanek