Skip to content
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

Provide option to extract to local *final* variable #3308

Closed
oliviercailloux opened this issue Sep 18, 2023 · 1 comment · Fixed by eclipse-jdtls/eclipse.jdt.ls#3198
Closed

Comments

@oliviercailloux
Copy link

oliviercailloux commented Sep 18, 2023

Please consider adding an option when refactoring using “extract to local variable”, to add the final keyword to the variable created by the refactoring.

Steps To Reproduce
  1. select statement that yields a result
  2. trigger code actions
  3. select “Extract to local variable”
Current Result

The statement is refactored so as to assign its result to a new variable. If I want the said variable to be final, then I have to afterwards go and add the final keyword before the variable declaration.

Expected Result

An option is provided so that the new variable is final, in other words, so that the final keyword is added automatically.

Additional Informations

Some authors claim that variables should be final whenever possible. See for example here for a small glimpse about this debate. Please consider providing help for those authors who like this style.

Icing on the cake

Detect automatically (as is done, I think, with the current option “Change modifiers to final when possible”) whether it is possible to make the variable final and provide a global setting to automatically make variables final, when refactoring using “extract to local variable”, whenever possible.

@rgrunber
Copy link
Member

rgrunber commented Jun 18, 2024

Note that we partly did this for other quick-assists in eclipse-jdtls/eclipse.jdt.ls#2371 . It's controlled by java.codeGeneration.addFinalForNewDeclaration and can be none, variables, fields, all . Looks like it was only done for a handful of quick assists (eg. Assign statement to new (local variable|field), Create (local variable|field), Assign parameter to new field, Assign all parameters to new fields ).

If we can support this for the extract quick-assist through the same setting ,it makes sense to do.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: ✅ Done
Development

Successfully merging a pull request may close this issue.

3 participants