Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kotlin - specify type explicitly in IntelliJ after extracting a variable shortcut

When coding in Java (and Kotlin!) in Intellij, I usually extract variables/fields from the expression that they are being assigned. In other words, if I want to write the following code in Kotlin:

val name = getMyString()

Then I would write getMyString, highlight it and extract the variable using a keyboard shortcut. Nice and simple.

But let's say I want the value type to be shown explicitly, meaning:

val name: String = getMyString()

If I use variable extraction, IntelliJ will offer me a checkbox, which I have to use my mouse to click, that will show the type. Another annoying option would be to set the variable name, then use Alt + Enter show the type explicitly.

Unlike previous questions (or JetBrains documentation), I do not want to have the type displayed by default. I just want to know if there is a keyboard shortcut to check the aforementioned checkbox, without disrupting the flow of writing the code.

like image 495
unlimitednzt Avatar asked Sep 29 '18 00:09

unlimitednzt


People also ask

How do I introduce a local variable in IntelliJ shortcut?

Press Ctrl+Alt+V or from the main menu, select Refactor | Extract/Introduce | Variable. Select a name suggested in the popup or type your own and press Enter .

How do I show type in IntelliJ?

You can use the shortcut ctrl + shift + p to show inferred type of a variable when the cursor is on the variable in IntelliJ IDEA.

How do I set variables in IntelliJ?

Add environment variablesFrom the main menu, select Run | Edit Configurations or choose Edit Configurations from the run/debug configurations selector on the toolbar. In the Run/Debug Configurations dialog, select a configuration you want to add the environment variables to.


1 Answers

SpecifyType popup

After using the extract variable shortcut (Ctrl+Alt +V for Windows), you can use

  • Alt+T to toggle Specify type explicitly
  • Alt+V to toggle Declare with var
like image 55
zapl Avatar answered Sep 28 '22 04:09

zapl