Skip to content

"Add curly braces" does not appear as a Quick-fix for always_put_control_body_on_new_line #3981

Closed

Description

@guidezpl

Describe the bug

Single quick fixes don't always appear. In this case, for always_put_control_body_on_new_line.

✅ Inline lightbulb
✅ cmd.
❌ Hovercard ⇒ click "Quick fix"

quickfix.mp4

❌ lightbulb in Problems panel
Screen Shot 2022-05-22 at 16 32 14 1

  • VS Code version: Version: 1.67.2 (Universal)
  • Dart Code extension: 3.40.0
  • Flutter extension: 3.41.20220516 (activated)

There seems to be a separate issue for multi-fixes ("everywhere in this file") that also applies to Android Studio, so filed dart-lang/sdk#49079.

Activity

  1. DanTup commented on May 23, 2022

    @DanTup
    Member

    I suspect this is because "Add curly braces" seen in the menu here is an Assist (and not a Fix) - eg. it'll show up even without the lint warning. The other menus you're opening are filtered to only show Fixes.

    I think it should be possible to wire it up as a fix for that lint that would solve this.

  2. changed the title [-]Quick fix doesn't appear in all cases[/-] [+]"Add curly braces" does not appear as a Quick-fix for always_put_control_body_on_new_line[/+] on May 23, 2022
  3. added this to the v3.42.0 milestone on May 23, 2022
  4. added
    is enhancementAn enhancement or improvement that should be listed in release notes but is not a bug fix.
    in lsp/analysis serverSomething to be fixed in the Dart analysis server
    in editorRelates to code editing or language features
    and removed on May 23, 2022
  5. DanTup commented on May 24, 2022

    @DanTup
    Member

    Re-reading this lint, I'm now wondering whether the "Add curly braces" fix is the obvious fix here. The lint isn't about using curly braces, but rather about having the code on a new line.

    If you're using the formatter, then these things are quite tied together, but if you're not using the formatter you might expect a fix for "move the body to a new line" rather than adding curly braces.

    There is another lint named curly_braces_in_flow_control_structures that I believe does what you want - it generates a similar warning but it does provide "add curly braces" as a fix.

    Does it make more sense for you to enable curly_braces_in_flow_control_structures is curly braces is what you really want?

    (cc @bwilkerson for his thoughts on whether "Use curly braces" is a reasonable fix also for "always_put_control_body_on_new_line")

  6. added
    awaiting infoRequires more information from the customer to progress
    on May 24, 2022
  7. guidezpl commented on May 24, 2022

    @guidezpl
    ContributorAuthor

    Ah thanks, using curly_braces_in_flow_control_structures works.

    IMO I still think the issue is valid, and both options should be shown as fixes in all places.

  8. DanTup commented on May 26, 2022

    @DanTup
    Member

    @bwilkerson is there a way to mark "Add curly braces" as a fix for always_put_control_body_on_new_line without it applying as an automatic fix for dart fix (and related commands)?

    I think it would be reasonable to show in the fix list in VS Code (it does already show in the "Code Actions" list from being an assist, but not in places where the list is filtered to just fixes), however I'm less sure it makes sense to apply automatically for this lint because it doesn't exactly match the reported problem and there is a more specific lint for enforcing braces (curly_braces_in_flow_control_structures).

    Personally, I think wanting to have control bodies on a new line without braces is a reasonable choice. Outside of Dart, it's actually what I'd do.. I use braces in Dart only because the formatter doesn't support spanning two lines without braces.

  9. modified the milestones: v3.42.0, v3.44.0 on May 26, 2022
  10. bwilkerson commented on May 26, 2022

    @bwilkerson

    is there a way to mark "Add curly braces" as a fix for always_put_control_body_on_new_line without it applying as an automatic fix for dart fix (and related commands)?

    Yes. Every fix knows whether it can be applied in bulk (dart fix) and whether it can be applied to a whole file (presumably the "related commands"). It can be coded to return false for that particular diagnostic.

  11. modified the milestones: v3.44.0, v3.42.0 on May 30, 2022
  12. removed
    awaiting infoRequires more information from the customer to progress
    on May 30, 2022
  13. DanTup commented on May 30, 2022

    @DanTup
    Member

    With dart-lang/sdk@20b7de7, the "Add Curly Braces" fix will now show up as a fix for this lint:

    Screenshot 2022-05-30 at 13 51 41

    It won't be applied by dart fix (because I don't think it's clearly the correct fix for this lint, which is about newlines and not braces).

    Although, while testing this I realised that curly_braces_in_flow_control_structures doesn't cover things on a single line, so if your code is:

    if (true) print('');
    

    Then enabling both lints still won't get dart fix to do what you want. I think the solution to that is probably a new fix for always_put_control_body_on_new_line that inserts a newline, then the example above could be dart fixable, without conflating the lints (eg. not assuming people with always_put_control_body_on_new_line want braces, or people with curly_braces_in_flow_control_structures want everything split across lines).

    There's already an issue covering this at dart-lang/sdk#47956, although there's a slight complication because of conflicts with the formatter.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    in editorRelates to code editing or language featuresin lsp/analysis serverSomething to be fixed in the Dart analysis serveris enhancementAn enhancement or improvement that should be listed in release notes but is not a bug fix.

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions