Reputation: 1036
When extracting a variable (Ctrl+Alt+V) in IntelliJ IDEA with Java 11, I would like for the default to be that it is extracted to a var
instead of the verbose type.
var home = "127.0.0.1";
instead of
String home = "127.0.0.1";
Is there a way to configure IntelliJ IDEA to do this?
Upvotes: 37
Views: 4294
Reputation: 113
As mentioned in here, it can be enabled as follows from 2023.2 version onwards.
Upvotes: 3
Reputation: 25136
Feature has been implemented and available since IntelliJ IDEA 2019.1 release
https://youtrack.jetbrains.com/issue/IDEA-179176
Fix versions 2019.1 (191.6183.87)
This feature hasn't been adopted by IntelliJ IDEA yet.
I've submitted an explicit feature request at JetBrains' bug tracking system: https://youtrack.jetbrains.com/issue/IDEA-206367
Although, other similar tickets which have been submitted before, are not yet completed:
However, you can somewhat achieve the desired behavior by using Custom Postfix Templates plugin, which allows to define your own custom postfix completion templates.
Statement like this:
Will be converted to:
To achieve this:
3) Add the following template:
.var : Extracts variable as inferred 'var' type
NON_VOID → var $VAR:suggestVariableName()$ = $expr$;
Restart IntelliJ and you're good to go.
Note. Existing postfix completion named 'var' exists in IntelliJ by default, you might want to disable the existing one (via Settings → Editor → General → Postfix Completion) or find another name for a new one.
Upvotes: 9