From d6ea7019010ad87b738343cef14915431a4d5d48 Mon Sep 17 00:00:00 2001 From: Dmitry Jemerov Date: Tue, 6 Oct 2015 17:26:05 +0200 Subject: [PATCH] use IDEA 15 UI for "Show expression type" --- idea/src/META-INF/plugin.xml | 2 + .../idea/actions/ShowExpressionTypeAction.kt | 40 +++++++++++------- .../KotlinExpressionTypeProvider.kt | 41 +++++++++++++++++++ 3 files changed, 69 insertions(+), 14 deletions(-) create mode 100644 idea/src/org/jetbrains/kotlin/idea/codeInsight/KotlinExpressionTypeProvider.kt diff --git a/idea/src/META-INF/plugin.xml b/idea/src/META-INF/plugin.xml index 427962f9382..22e27de2804 100644 --- a/idea/src/META-INF/plugin.xml +++ b/idea/src/META-INF/plugin.xml @@ -610,6 +610,8 @@ + + org.jetbrains.kotlin.idea.intentions.IfNullToElvisIntention Kotlin diff --git a/idea/src/org/jetbrains/kotlin/idea/actions/ShowExpressionTypeAction.kt b/idea/src/org/jetbrains/kotlin/idea/actions/ShowExpressionTypeAction.kt index 6d2f0eb96a3..3ef8bbab4a7 100644 --- a/idea/src/org/jetbrains/kotlin/idea/actions/ShowExpressionTypeAction.kt +++ b/idea/src/org/jetbrains/kotlin/idea/actions/ShowExpressionTypeAction.kt @@ -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(CommonDataKeys.EDITOR)!! @@ -61,26 +62,37 @@ public class ShowExpressionTypeAction : AnAction() { } if (type != null) { - HintManager.getInstance().showInformationHint(editor, "" + DescriptorRenderer.HTML.renderType(type) + "") + 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(CommonDataKeys.EDITOR) val psiFile = e.getData(CommonDataKeys.PSI_FILE) e.getPresentation().setEnabled(editor != null && psiFile is JetFile) } + + companion object { + fun renderTypeHint(type: JetType) = "" + DescriptorRenderer.HTML.renderType(type) + "" + + 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) + } + } } diff --git a/idea/src/org/jetbrains/kotlin/idea/codeInsight/KotlinExpressionTypeProvider.kt b/idea/src/org/jetbrains/kotlin/idea/codeInsight/KotlinExpressionTypeProvider.kt new file mode 100644 index 00000000000..a4841259987 --- /dev/null +++ b/idea/src/org/jetbrains/kotlin/idea/codeInsight/KotlinExpressionTypeProvider.kt @@ -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() { + override fun getExpressionsAt(elementAt: PsiElement): List = + elementAt.parentsWithSelf.filterIsInstance().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" +}