Skip to content

extra space needed before : with single initializer #792

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

Closed
a14n opened this issue Mar 31, 2019 · 5 comments
Closed

extra space needed before : with single initializer #792

a14n opened this issue Mar 31, 2019 · 5 comments

Comments

@a14n
Copy link
Contributor

a14n commented Mar 31, 2019

Actual:

class PestoStyle extends TextStyle {
  const PestoStyle({
    double fontSize = 12.0,
  }) : super(
          inherit: false,
        );
}

expected:

class PestoStyle extends TextStyle {
  const PestoStyle({
    double fontSize = 12.0,
  })  : super(
          inherit: false,
        );
}

Same issue when the single initializer splits:

Actual:

  class A {
    A(
      a,
    ) : parameter =
              "some very very long param";
  }

Expected:

  class A {
    A(
      a,
    )   : parameter =
              "some very very long param";
  }
@munificent
Copy link
Member

I'm not sure if we want to do this one, unfortunately.

In general, dartfmt only does indentation at the beginning of lines and doesn't otherwise try to align intra-line content. (See #508 for a bit more discussion.) I worry that if we take this change then users will file bugs asking why there's an extra space before the :.

@munificent munificent reopened this Jun 3, 2019
@munificent
Copy link
Member

(Oops, didn't mean to click "Close".)

@a14n
Copy link
Contributor Author

a14n commented Jun 4, 2019

doesn't otherwise try to align intra-line content.

It already adds spaces for initializer list with several entries:

class A {
  final a;
  const A(
    p,
  )   : a = p,
        super();
}

This absence of space makes the split indentation the only place with an inconsistant indentation.

class A {
  final a;
  const A.c1(
    p,
  ) : super(
          a: p,
        );
//    ^^   WAT

  // with several elements in initializer list, no problem
  const A.c2(
    p,
  )   : a = p,
        super(
          a: p,
        );
}

@munificent
Copy link
Member

It already adds spaces for initializer list with several entries

Yeah, I agonized over that one. :-/

@munificent
Copy link
Member

I don't think there's an ideal way to format constructor initializer lists. They're just pretty syntactically weird. If we don't do extra padding before the : then it means a change to the parameter list (adding or removing optional parameters) might force the entire initializer list to be indented differently, which increases churn. But if we do add that padding, it looks weird and unnecessary.

I spent a lot of time inspecting how hand-formatted initializer lists in the Flutter repo are styled. It turns out they are pretty inconsistent, but I aimed to follow the most common styles I could see there. That was to not do padding and accept that the initializer list might get shifted if you change whether or not the constructor has optional parameters.

The style isn't perfect, but I think it's about as good as I can get it. With the forthcoming tall style, you get:

class PestoStyle extends TextStyle {
  const PestoStyle({
    double fontSize = 12.0, // comment to force split
  }) : super(
         inherit: false, // comment to force split
       );
}

And:

class A {
  A(
    a, // comment to force split
  ) : parameter =
          "some very very very very very very very very very long param";
}

And:

class A {
  final a;
  const A.c1(
    p, // comment to force split
  ) : super(
        a: p, // comment to force split
      );
}

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

Successfully merging a pull request may close this issue.

2 participants