Refactored ShowExpressionTypeAction

This commit is contained in:
Valentin Kipyatkov
2015-06-01 20:14:21 +03:00
parent 5a50423ae5
commit 5d9259e9a4
@@ -23,37 +23,42 @@ import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.actionSystem.CommonDataKeys import com.intellij.openapi.actionSystem.CommonDataKeys
import com.intellij.openapi.editor.Editor import com.intellij.openapi.editor.Editor
import com.intellij.psi.PsiFile import com.intellij.psi.PsiFile
import com.intellij.psi.util.PsiTreeUtil
import org.jetbrains.kotlin.idea.JetLanguage import org.jetbrains.kotlin.idea.JetLanguage
import org.jetbrains.kotlin.idea.caches.resolve.* 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.JetExpression import org.jetbrains.kotlin.psi.JetExpression
import org.jetbrains.kotlin.psi.JetFile 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.startOffset
import org.jetbrains.kotlin.renderer.DescriptorRenderer import org.jetbrains.kotlin.renderer.DescriptorRenderer
import org.jetbrains.kotlin.resolve.BindingContext import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.types.JetType
public class ShowExpressionTypeAction : AnAction() { public class ShowExpressionTypeAction : AnAction() {
override fun actionPerformed(e: AnActionEvent) { override fun actionPerformed(e: AnActionEvent) {
val editor = e.getData<Editor>(CommonDataKeys.EDITOR) val editor = e.getData<Editor>(CommonDataKeys.EDITOR)!!
val psiFile = e.getData<PsiFile>(CommonDataKeys.PSI_FILE) val psiFile = e.getData<PsiFile>(CommonDataKeys.PSI_FILE)!!
assert(editor != null && psiFile != null)
var expression: JetExpression? var expression: JetExpression?
val bindingContext = (psiFile as JetFile).analyzeFully() var bindingContext: BindingContext
if (editor!!.getSelectionModel().hasSelection()) { if (editor.getSelectionModel().hasSelection()) {
val startOffset = editor.getSelectionModel().getSelectionStart() val startOffset = editor.getSelectionModel().getSelectionStart()
val endOffset = editor.getSelectionModel().getSelectionEnd() val endOffset = editor.getSelectionModel().getSelectionEnd()
expression = CodeInsightUtilCore.findElementInRange<JetExpression>(psiFile, startOffset, endOffset, javaClass<JetExpression>(), JetLanguage.INSTANCE) expression = CodeInsightUtilCore.findElementInRange<JetExpression>(psiFile, startOffset, endOffset, javaClass<JetExpression>(), JetLanguage.INSTANCE)
bindingContext = expression.analyze()
} }
else { else {
val offset = editor.getCaretModel().getOffset() val offset = editor.getCaretModel().getOffset()
expression = PsiTreeUtil.getParentOfType<JetExpression>(psiFile.findElementAt(offset), javaClass<JetExpression>()) expression = psiFile.findElementAt(offset)?.getParentOfType<JetExpression>(true) ?: return
while (expression != null && bindingContext.getType(expression) == null) { bindingContext = expression.analyze()
expression = PsiTreeUtil.getParentOfType<JetExpression>(expression, javaClass<JetExpression>()) while (bindingContext.getType(expression!!) == null) {
} expression = expression.getParentOfType<JetExpression>(true) ?: return
if (expression != null) { bindingContext = expression.analyze()
editor.getSelectionModel().setSelection(expression.getTextRange().getStartOffset(), expression.getTextRange().getEndOffset())
} }
editor.getSelectionModel().setSelection(expression.startOffset, expression.endOffset)
} }
if (expression != null) { if (expression != null) {
val type = bindingContext.getType(expression) val type = bindingContext.getType(expression)
if (type != null) { if (type != null) {
@@ -62,8 +67,8 @@ public class ShowExpressionTypeAction : AnAction() {
} }
} }
override fun update(e: AnActionEvent?) { override fun update(e: AnActionEvent) {
val editor = e!!.getData<Editor>(CommonDataKeys.EDITOR) val editor = e.getData<Editor>(CommonDataKeys.EDITOR)
val psiFile = e.getData<PsiFile>(CommonDataKeys.PSI_FILE) val psiFile = e.getData<PsiFile>(CommonDataKeys.PSI_FILE)
e.getPresentation().setEnabled(editor != null && psiFile is JetFile) e.getPresentation().setEnabled(editor != null && psiFile is JetFile)
} }