在 Intellij Scala 插件中显示推断类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25586931/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Show inferred type in Intellij Scala plugin
提问by benstpierre
Is there a way to show the derived type of something in Intellij's Scala support?
有没有办法在 Intellij 的 Scala 支持中显示某些东西的派生类型?
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.
例如,如果我正在摸索一些漂亮的代码,并想知道“用户”val 是什么类型,也许可以通过按下一个键并将鼠标悬停在我的鼠标上,这将非常有用。
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?
知道如何在我的 IDE 中尽可能轻松地找出“用户”的类型吗?
回答by Cy?egha
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.
- 确保文本光标位于变量内,并检查菜单 View > Type Info 以获取其快捷方式。使用该快捷方式显示类型信息。在 OS X 上,它是 ctrl-shift-P。
- 或者在首选项 > IDE 设置 > Scala 中设置首选项以在鼠标悬停时显示相同的类型信息。选中“延迟显示鼠标运动的类型信息”并根据需要更改延迟。


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.
或者,您不仅可以显示类型信息,还可以显示该类型的文档。即使没有适用的 scaladoc 或 javadoc,它也会显示类型。
- 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.
- 使用快速文档快捷方式(在视图 > 快速文档中看到),就像类型信息一样。在 OS X 上,它是 ctrl-J。
- 或者在首选项 > IDE 设置 > 编辑器中设置首选项以在鼠标悬停时显示快速文档。选中“在鼠标移动时显示快速文档”并根据需要更改延迟。


回答by Ende Neu
In addition to @Cy?egha approach you can even tell idea to infer the type and add it for you.
除了@Cy?egha 方法之外,您甚至可以告诉 idea 来推断类型并为您添加它。
On OS X, go on the variable, click alt + enter and you should see this
在 OS X 上,继续变量,点击 alt + enter,你应该看到这个


After you click on it you'll see
点击后你会看到
val test: String = "dsds"
The short key is at the voice Show Intention Actions.
快捷键在语音Show Intention Actions。
回答by Mala
1) Select the variable or place your cursor within the variable text.
1) 选择变量或将光标置于变量文本内。
2) Press Ctrl + Qfor Windows/ Linuxor F1for macOS.
2)按Ctrl + Q供Windows/ Linux或F1供macOS。
回答by bobbyr
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.
选择您感兴趣的表达式,突出显示它并使用将其分配给局部变量的工具 - Cmd + Alt + V(我认为,来自内存)在 os-x 上。这将显示 IntelliJ 认为的类型。

