From 6d2d054b3bcebf643fd13ee205073f169df06f13 Mon Sep 17 00:00:00 2001 From: Valentin Kipyatkov Date: Mon, 1 Jun 2015 20:28:34 +0300 Subject: [PATCH] Refactored ShowExpressionTypeAction to work for variable type --- .../idea/actions/ShowExpressionTypeAction.kt | 45 ++++++++++++------- 1 file changed, 28 insertions(+), 17 deletions(-) diff --git a/idea/src/org/jetbrains/kotlin/idea/actions/ShowExpressionTypeAction.kt b/idea/src/org/jetbrains/kotlin/idea/actions/ShowExpressionTypeAction.kt index bc1a52a3952..05034943c54 100644 --- a/idea/src/org/jetbrains/kotlin/idea/actions/ShowExpressionTypeAction.kt +++ b/idea/src/org/jetbrains/kotlin/idea/actions/ShowExpressionTypeAction.kt @@ -23,48 +23,59 @@ import com.intellij.openapi.actionSystem.AnActionEvent import com.intellij.openapi.actionSystem.CommonDataKeys import com.intellij.openapi.editor.Editor import com.intellij.psi.PsiFile +import org.jetbrains.kotlin.descriptors.CallableDescriptor import org.jetbrains.kotlin.idea.JetLanguage import org.jetbrains.kotlin.idea.caches.resolve.analyze -import org.jetbrains.kotlin.idea.conversion.copy.end -import org.jetbrains.kotlin.idea.conversion.copy.start +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 public class ShowExpressionTypeAction : AnAction() { override fun actionPerformed(e: AnActionEvent) { val editor = e.getData(CommonDataKeys.EDITOR)!! val psiFile = e.getData(CommonDataKeys.PSI_FILE)!! - var expression: JetExpression? - var bindingContext: BindingContext - if (editor.getSelectionModel().hasSelection()) { + val type = if (editor.getSelectionModel().hasSelection()) { val startOffset = editor.getSelectionModel().getSelectionStart() val endOffset = editor.getSelectionModel().getSelectionEnd() - expression = CodeInsightUtilCore.findElementInRange(psiFile, startOffset, endOffset, javaClass(), JetLanguage.INSTANCE) - bindingContext = expression.analyze() + val expression = CodeInsightUtilCore.findElementInRange(psiFile, startOffset, endOffset, javaClass(), JetLanguage.INSTANCE) ?: return + typeByExpression(expression) } else { val offset = editor.getCaretModel().getOffset() - expression = psiFile.findElementAt(offset)?.getParentOfType(true) ?: return - bindingContext = expression.analyze() - while (bindingContext.getType(expression!!) == null) { - expression = expression.getParentOfType(true) ?: return - bindingContext = expression.analyze() - } + val token = psiFile.findElementAt(offset) ?: return + val pair = token.parents + .filterIsInstance() + .map { it to typeByExpression(it) } + .firstOrNull { it.second != null } ?: return + val (expression, type) = pair editor.getSelectionModel().setSelection(expression.startOffset, expression.endOffset) + type } - if (expression != null) { - val type = bindingContext.getType(expression) - if (type != null) { - HintManager.getInstance().showInformationHint(editor, "" + DescriptorRenderer.HTML.renderType(type) + "") + if (type != null) { + HintManager.getInstance().showInformationHint(editor, "" + DescriptorRenderer.HTML.renderType(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) {