Skip to content

add to constructor refactor an option to add a final variable #4166

Open

Description

@Zekfad

Is your feature request related to a problem? Please describe.
It would be very cool to have in constructor parameters refactor menu an option to add a final field to class and add this.field placeholder to constructor.
Such a thing will make adding widget parameters way easier and faster.

Refactor menu

image

Describe the solution you'd like
Add option that will allow to quickly create final field and add it to constructor parameters (named/positional/optional).

Activity

  1. added
    is enhancement
    on Sep 21, 2022
  2. DanTup commented on Sep 21, 2022

    @DanTup
    Member

    Can you give an example of how this would be used? There is an existing fix you can use once you've type this.field to create the field:

    Screenshot 2022-09-21 at 17 08 33

    And also a fix to update the constructor if there are final fields that aren't in the constructor:

    Screenshot 2022-09-21 at 17 09 24

  3. added
    awaiting info
    on Sep 21, 2022
  4. Zekfad commented on Sep 21, 2022

    @Zekfad
    Author

    This is supposed to be used when user want to add new field and add it to constructor. In a scenario like that:

    • User presses Ctrl + Shift + R
    • Presses create field
    • Types field name
    • Types type
    • Cursor returns to constructor after declared this.field

    Also current "create field" adds no type and isn't final as I see.

  5. DanTup commented on Sep 22, 2022

    @DanTup
    Member

    Thanks for clarifying. Generally, I think refactors that don't require asking the user for input work better, because VS Code has very limited options for asking for input. For example in the scenario you described above, when asking you to type the Type name, you wouldn't get code completion like you would if you were typing it into the editor.

    Also current "create field" adds no type and isn't final as I see.

    That's right - there is another fix (see second screenshot) you can use if you type the field yourself instead, although it looks like you have to invoke it on the constructor. I wonder if this could be made to also be available from the field itself (@bwilkerson any thoughts on this?).

  6. Zekfad commented on Sep 22, 2022

    @Zekfad
    Author

    Isn't it is possible to prompt for field name, and place selection to type and then to the constructor, like if it was a snippet? (The problem with snippets that they have no access to deeper context, means they don't know about constructor and so on).

  7. bwilkerson commented on Sep 22, 2022

    @bwilkerson

    And also a fix to update the constructor if there are final fields that aren't in the constructor:

    That label isn't very clear. I sounds like the field formal parameter will be final, when it's really adding parameters for final fields. Something to polish.

    I wonder if this could be made to also be available from the field itself ...

    It would be possible, but I don't know how good a job we could do. There are several design choices we'd need to make.

    We would potentially need to add it to multiple constructors, so I assume we'd need to add it to all existing constructors that don't already initialize the field (because we can't ask which constructors to add it to). In each of those constructors the field can be initialized as either a field formal parameter or in the initializer list, so we'd have to choose one option or the other (because we can't prompt on a case by case basis). If we add it to the parameter list we'd need to decide whether we're making it a positional or named parameter (assuming that it can be a named parameter) and where in the parameter list to add it if there are other parameters. If we decide to put it in the initializer list we'd need to figure out how to initialize it.

  8. github-actions commented on Oct 13, 2022

    @github-actions

    This issue has been marked stale because it is tagged awaiting-info for 20 days with no activity. Remove the stale label or comment to prevent the issue being closed in 10 days.

  9. added
    stale
    on Oct 13, 2022
  10. Zekfad commented on Oct 13, 2022

    @Zekfad
    Author

    Hi, any progress on this? I mean, you haven't put "wontfix" on this issue, so I assumed it is considered at least?

  11. removed
    awaiting info
    stale
    on Oct 13, 2022
  12. added this to the Backlog milestone on Oct 13, 2022
  13. DanTup commented on Oct 13, 2022

    @DanTup
    Member

    Sorry for the delay. I haven't had chance to look back at this yet. Brian noted some concerns about how well we could do this above so it needs some thought/investigation to see what the possibilities are. I've removed the tag so the bot won't close it if there's no activity in the short-term.

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

    is enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions