You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
add to constructor refactor an option to add a final variable #4166
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
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).
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?).
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).
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.
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.
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.
Activity
DanTup commentedon Sep 21, 2022
Can you give an example of how this would be used? There is an existing fix you can use once you've type
this.fieldto create the field:And also a fix to update the constructor if there are final fields that aren't in the constructor:
Zekfad commentedon Sep 21, 2022
This is supposed to be used when user want to add new field and add it to constructor. In a scenario like that:
this.fieldAlso current "create field" adds no type and isn't final as I see.
DanTup commentedon Sep 22, 2022
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.
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?).
Zekfad commentedon Sep 22, 2022
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).
bwilkerson commentedon Sep 22, 2022
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.
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.
github-actions commentedon Oct 13, 2022
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.
Zekfad commentedon Oct 13, 2022
Hi, any progress on this? I mean, you haven't put "wontfix" on this issue, so I assumed it is considered at least?
DanTup commentedon Oct 13, 2022
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.