use IDEA 15 UI for "Show expression type"
This commit is contained in:
@@ -610,6 +610,8 @@
|
||||
|
||||
<problemFileHighlightFilter implementation="org.jetbrains.kotlin.idea.projectView.KotlinProblemFileHighlightFilter"/>
|
||||
|
||||
<codeInsight.typeInfo language="kotlin" implementationClass="org.jetbrains.kotlin.idea.codeInsight.KotlinExpressionTypeProvider"/>
|
||||
|
||||
<intentionAction>
|
||||
<className>org.jetbrains.kotlin.idea.intentions.IfNullToElvisIntention</className>
|
||||
<category>Kotlin</category>
|
||||
|
||||
@@ -18,6 +18,7 @@ package org.jetbrains.kotlin.idea.actions
|
||||
|
||||
import com.intellij.codeInsight.CodeInsightUtilCore
|
||||
import com.intellij.codeInsight.hint.HintManager
|
||||
import com.intellij.openapi.actionSystem.ActionManager
|
||||
import com.intellij.openapi.actionSystem.AnAction
|
||||
import com.intellij.openapi.actionSystem.AnActionEvent
|
||||
import com.intellij.openapi.actionSystem.CommonDataKeys
|
||||
@@ -30,13 +31,13 @@ import org.jetbrains.kotlin.psi.JetCallableDeclaration
|
||||
import org.jetbrains.kotlin.psi.JetExpression
|
||||
import org.jetbrains.kotlin.psi.JetFile
|
||||
import org.jetbrains.kotlin.psi.psiUtil.endOffset
|
||||
import org.jetbrains.kotlin.psi.psiUtil.getParentOfType
|
||||
import org.jetbrains.kotlin.psi.psiUtil.parents
|
||||
import org.jetbrains.kotlin.psi.psiUtil.startOffset
|
||||
import org.jetbrains.kotlin.renderer.DescriptorRenderer
|
||||
import org.jetbrains.kotlin.resolve.BindingContext
|
||||
import org.jetbrains.kotlin.types.JetType
|
||||
|
||||
@Deprecated("Remove once we no longer support IDEA 14.1")
|
||||
public class ShowExpressionTypeAction : AnAction() {
|
||||
override fun actionPerformed(e: AnActionEvent) {
|
||||
val editor = e.getData<Editor>(CommonDataKeys.EDITOR)!!
|
||||
@@ -61,26 +62,37 @@ public class ShowExpressionTypeAction : AnAction() {
|
||||
}
|
||||
|
||||
if (type != null) {
|
||||
HintManager.getInstance().showInformationHint(editor, "<html>" + DescriptorRenderer.HTML.renderType(type) + "</html>")
|
||||
HintManager.getInstance().showInformationHint(editor, renderTypeHint(type))
|
||||
}
|
||||
}
|
||||
|
||||
private fun typeByExpression(expression: JetExpression): JetType? {
|
||||
val bindingContext = expression.analyze()
|
||||
|
||||
if (expression is JetCallableDeclaration) {
|
||||
val descriptor = bindingContext[BindingContext.DECLARATION_TO_DESCRIPTOR, expression] as? CallableDescriptor
|
||||
if (descriptor != null) {
|
||||
return descriptor.getReturnType()
|
||||
}
|
||||
}
|
||||
|
||||
return bindingContext.getType(expression)
|
||||
}
|
||||
|
||||
override fun update(e: AnActionEvent) {
|
||||
// hide the action in IDEA 15 where a standard platform action is available
|
||||
if (ActionManager.getInstance().getAction("ExpressionTypeInfo") != null) {
|
||||
e.presentation.isVisible = false
|
||||
return
|
||||
}
|
||||
|
||||
val editor = e.getData<Editor>(CommonDataKeys.EDITOR)
|
||||
val psiFile = e.getData<PsiFile>(CommonDataKeys.PSI_FILE)
|
||||
e.getPresentation().setEnabled(editor != null && psiFile is JetFile)
|
||||
}
|
||||
|
||||
companion object {
|
||||
fun renderTypeHint(type: JetType) = "<html>" + DescriptorRenderer.HTML.renderType(type) + "</html>"
|
||||
|
||||
fun typeByExpression(expression: JetExpression): JetType? {
|
||||
val bindingContext = expression.analyze()
|
||||
|
||||
if (expression is JetCallableDeclaration) {
|
||||
val descriptor = bindingContext[BindingContext.DECLARATION_TO_DESCRIPTOR, expression] as? CallableDescriptor
|
||||
if (descriptor != null) {
|
||||
return descriptor.getReturnType()
|
||||
}
|
||||
}
|
||||
|
||||
return bindingContext.getType(expression)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
/*
|
||||
* Copyright 2010-2015 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.idea.codeInsight
|
||||
|
||||
import com.intellij.lang.ExpressionTypeProvider
|
||||
import com.intellij.psi.PsiElement
|
||||
import org.jetbrains.kotlin.idea.actions.ShowExpressionTypeAction
|
||||
import org.jetbrains.kotlin.psi.JetExpression
|
||||
import org.jetbrains.kotlin.psi.JetFunction
|
||||
import org.jetbrains.kotlin.psi.JetStatementExpression
|
||||
import org.jetbrains.kotlin.psi.psiUtil.parentsWithSelf
|
||||
|
||||
class KotlinExpressionTypeProvider : ExpressionTypeProvider<JetExpression>() {
|
||||
override fun getExpressionsAt(elementAt: PsiElement): List<JetExpression> =
|
||||
elementAt.parentsWithSelf.filterIsInstance<JetExpression>().filterNot { it.shouldSkip() }.toArrayList()
|
||||
|
||||
private fun JetExpression.shouldSkip(): Boolean {
|
||||
return this is JetStatementExpression && this !is JetFunction
|
||||
}
|
||||
|
||||
override fun getInformationHint(element: JetExpression): String {
|
||||
val type = ShowExpressionTypeAction.typeByExpression(element) ?: return "Type is unknown"
|
||||
return ShowExpressionTypeAction.renderTypeHint(type)
|
||||
}
|
||||
|
||||
override fun getErrorHint(): String = "No expression found"
|
||||
}
|
||||
Reference in New Issue
Block a user