Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show inferred type in Intellij Scala plugin

Is there a way to show the derived type of something in Intellij's Scala support?

For example if I am fumbling my way through some slick code and want to know what type the "user" val is perhaps by olding down a key and hovering my mouse that would be incredibly usefull.

    DB.withSession {       implicit session =>         val user = users.filter(user => user.email === email && user.password === password).first     } 

Any idea how to find out the type of "user" in my IDE as easily as possible?

like image 239
benstpierre Avatar asked Aug 30 '14 21:08

benstpierre


People also ask

Why Scala class is not showing in IntelliJ?

On the Project pane on the left, right-click src and select New => Scala class. If you don't see Scala class, right-click on HelloWorld and click on Add Framework Support…, select Scala and proceed. If you see Error: library is not specified, you can either click download button, or select the library path manually.

How do I get Scala plugin in IntelliJ?

To install Scala plugin, press Ctrl+Alt+S , open the Plugins page, browse repositories to locate the Scala plugin, click Install and restart IntelliJ IDEA. Now you can successfully check out from VCS, create, or import Scala projects.

How do I get Scala class in IntelliJ?

In the Project tool window, right-click a Scala library class that you want to decompile. From the context menu, select Decompile Scala to Java. IntelliJ IDEA converts code to Java and opens the converted file in the editor. You can also open the library class in the editor and use its context menu for the conversion.


2 Answers

You can:

  • Make sure the text cursor is within the variable and check the menu View > Type Info for its shortcut. Use that shortcut to display type info. On OS X, it's ctrl-shift-P.
  • Or set preferences to show the same type info on mouse hover in Preferences > IDE Settings > Scala. Check "Show type info on mouse motion with delay" and change the delay if you want.

enter image description here

Alternatively, instead of just showing type info, you can show the documentation for the type. It will show the type even if there is no scaladoc or javadoc applicable.

  • Use the Quick Documentation shortcut (seen in View > Quick Documentation) like the type info one. On OS X, it's ctrl-J.
  • Or set preferences to show quick doc on mouse hover in Preferences > IDE Settings > Editor. Check "Show quick doc on mouse move" and change the delay if you want.

enter image description here

like image 169
Cyäegha Avatar answered Sep 18 '22 19:09

Cyäegha


In addition to @Cyäegha approach you can even tell idea to infer the type and add it for you.

On OS X, go on the variable, click alt + enter and you should see this

enter image description here

After you click on it you'll see

val test: String = "dsds" 

The short key is at the voice Show Intention Actions.

like image 36
Ende Neu Avatar answered Sep 20 '22 19:09

Ende Neu