From 66877c138d37170322fe81b36b1201262dfdb0fa Mon Sep 17 00:00:00 2001 From: Dmitry Jemerov Date: Tue, 9 May 2017 12:29:30 +0200 Subject: [PATCH] Correctly handle editor selection in "Show expression type" To avoid showing a large list, the previous logic returned only the element directly at caret and other elements with the same start offset. When selection is present, the platform logic looks only at elements which have a larger range than the selection, so our elements could be completely filtered out. Now we look at the selection ourselves and adjust the returned elements accordingly. #KT-16423 Fixed --- .../codeInsight/KotlinExpressionTypeProvider.kt | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/idea/src/org/jetbrains/kotlin/idea/codeInsight/KotlinExpressionTypeProvider.kt b/idea/src/org/jetbrains/kotlin/idea/codeInsight/KotlinExpressionTypeProvider.kt index 4dc6f00f186..ef97208084f 100644 --- a/idea/src/org/jetbrains/kotlin/idea/codeInsight/KotlinExpressionTypeProvider.kt +++ b/idea/src/org/jetbrains/kotlin/idea/codeInsight/KotlinExpressionTypeProvider.kt @@ -17,6 +17,10 @@ package org.jetbrains.kotlin.idea.codeInsight import com.intellij.lang.ExpressionTypeProvider +import com.intellij.openapi.editor.ex.util.EditorUtil +import com.intellij.openapi.fileEditor.FileEditorManager +import com.intellij.openapi.fileEditor.TextEditor +import com.intellij.openapi.util.TextRange import com.intellij.psi.PsiElement import org.jetbrains.kotlin.descriptors.CallableDescriptor import org.jetbrains.kotlin.descriptors.ClassifierDescriptor @@ -50,7 +54,14 @@ class KotlinExpressionTypeProvider : ExpressionTypeProvider() { override fun getExpressionsAt(elementAt: PsiElement): List { val candidates = elementAt.parentsWithSelf.filterIsInstance().filter { it.shouldShowType() }.toList() - return candidates.takeWhile { it.textRange.startOffset == candidates.first().textRange.startOffset } + val fileEditor = elementAt.containingFile?.virtualFile?.let { FileEditorManager.getInstance(elementAt.project).getSelectedEditor(it) } + val selectionTextRange = if (fileEditor is TextEditor) { + EditorUtil.getSelectionInAnyMode(fileEditor.editor) + } else { + TextRange.EMPTY_RANGE + } + val anchor = candidates.firstOrNull { selectionTextRange.isEmpty || it.textRange.contains(selectionTextRange) } ?: return emptyList() + return candidates.filter { it.textRange.startOffset == anchor.textRange.startOffset } } private fun KtExpression.shouldShowType() = when (this) {