26

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?

benstpierre
  • 32,833
  • 51
  • 177
  • 288

4 Answers4

46

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

Cyäegha
  • 4,191
  • 2
  • 20
  • 36
  • 9
    In newest IDEA the hover option is found under `Settings -> Language & Frameworks -> Scala -> Editor tab`. – Răzvan Flavius Panda Apr 06 '16 at 14:53
  • Any idea why this might not show anything? When I go View->Type Info with my text cursor in a scala variable nothing happens. – GreenAsJade Aug 29 '16 at 23:18
  • Any way to change the keyboard shortcut? I want to keep it hover+Ctrl as in NetBeans (and reassign current hover+Ctrl to hover+Shift). – Jus12 Jan 19 '19 at 06:12
7

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.

Ende Neu
  • 15,581
  • 5
  • 57
  • 68
2

1) Select the variable or place your cursor within the variable text.

2) Press Ctrl + Q for Windows/ Linux or F1 for macOS.

Dinesh Shingadiya
  • 988
  • 1
  • 8
  • 23
Mala
  • 21
  • 2
0

Select the expression you're interested in, highlight it and use the facility that assigns it to a local variable - Cmd + Alt + V (I think, from memory) on os-x. This will show you the type IntelliJ considers it to be.

bobbyr
  • 226
  • 1
  • 9
  • I am not really sure what this is doing, it appears to be a command declare a val for an expression or something. I am simply interested in finding the type of an expression. – benstpierre Aug 30 '14 at 22:13
  • That will show you the type that IntelliJ thinks it is. I don't know another way – bobbyr Aug 30 '14 at 22:14