33

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?

Jewels
  • 966
  • 11
  • 28
  • didn't get possibly what you're trying to achieve, deleted the answer. though what is it that you're eyeing, is it changing the variable name or type (suggestions)? – Naman Jan 16 '19 at 15:07
  • 1
    There is related request on YouTrack: https://youtrack.jetbrains.com/issue/IDEA-190495 – y.bedrov Jan 16 '19 at 15:08
  • 1
    @nullpointer I would like the default *type* to be `var` and not `String` – Jewels Jan 16 '19 at 15:13
  • Probably the IntelliJ people share my preferences, to produce code as explicit as possible, not forcing a future reader to reason about the variable type. – Ralf Kleberhoff Jan 16 '19 at 15:51
  • 1
    This will be more helpful in complex types with generics. Like HasMap> – Brachi Jan 16 '19 at 15:57
  • @RalfKleberhoff you would think that they would make the IDE as configurable as possible to cater to the preferences of _all_ of their users... – Jewels Jan 16 '19 at 16:25
  • @y.bedrov I'm not sure that that is the same issue - i'm referring to the default type of the variable upon extraction, not an issue with code compilation – Jewels Jan 16 '19 at 16:26

2 Answers2

10

Jet Brains added this feature to Intellij 2019.1.1

variable extraction in Intellij 2019.1.1

Jewels
  • 966
  • 11
  • 28
8

Update

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:


Alternative

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:

enter image description here

Will be converted to:

enter image description here

To achieve this:

  • 1) Install Custom Postfix Templates plugin via Settings → Plugins → Browse Repositories.
  • 2) Press Shift+Alt+P (or go to menu Tools → Custom Postfix Templates → Edit Templates of Current Language) to open the custom postfix templates for the programming language in your current editor.
  • 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.

Community
  • 1
  • 1
Mikhail Kholodkov
  • 23,642
  • 17
  • 61
  • 78