7

Recently I installed the new version (2018.1) of IntelliJ IDEA, which added support for Java 10.

But when I trying to use the var (for local variable type inference), I found there was no var in the code completion list. (See the screenshot below)

If I continue typing, it will apply VarHandle as the first suggestion in that list. Then I have to undo that completion to restore var.

I have checked project settings, I use JDK 10 and the language level is also set to 10. Code like var number = 1; compiles well. And I couldn't find anything about this in IDE settings.

What am I doing wrong? I just want to use var declaration without interruption.

Code Completion

Grant Howard
  • 135
  • 2
  • 9
  • 1
    Looks like its a bug https://youtrack.jetbrains.com/issue/IDEA-189285#comment=27-2791283 – Vic Apr 08 '18 at 15:23
  • 1
    Sounds like a bug. My suggestion would be to add a live template for `var` that simply expands back to `var`. It’s a hack, but you can remove it once the issue is resolved. – Mike Strobel Apr 08 '18 at 15:23
  • @MikeStrobel The template hack works, thanks for your suggestion! – Grant Howard Apr 08 '18 at 15:43
  • Did you try with the latest EAP? – LppEdd Apr 08 '18 at 15:50
  • It would really annoy me if my IDE was interpreting pressing *space* as “insert some word” instead of “insert *space*”. But I’m very confident that IntelliJ also has an option to use a different key (e.g. *enter*) for completion instead of *space*, to allow inserting *space* when pressing *space* (assuming you don’t want to “complete” `var` to `var`)… – Holger Apr 10 '18 at 11:44
  • @LppEdd No, now that there is a solution, I'm not willing to meet more bugs in EAP. – Grant Howard Apr 10 '18 at 12:34
  • @Holger Well, it's also a nice solution. But I really like hitting space for completion :) – Grant Howard Apr 10 '18 at 12:43
  • @GrantHoward I always use EAPs and I've never encountered a blocking bug. – LppEdd Apr 10 '18 at 12:43
  • @LppEdd OK, I'll try it next time. – Grant Howard Apr 10 '18 at 13:10

2 Answers2

7

As @Vic pointed out, this appears to be a bug. Until it gets fixed, you can create a custom live template as a workaround.

Create a live template triggered by the var abbreviation, then enter var as the template text. Make it available in the Java statement context.

Screenshot

Mike Strobel
  • 25,075
  • 57
  • 69
1

Issue has been fixed in upcoming IntelliJ IDEA 2018.2 release:

https://www.jetbrains.com/idea/nextversion/ (currently EAP version)

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