From 50c535de8552760dec2bc5278ed6cccd5ab927c8 Mon Sep 17 00:00:00 2001 From: Natalia Ukhorskaya Date: Tue, 10 Mar 2015 18:55:41 +0300 Subject: [PATCH] Debugger: Alt + click on variable name in declaration should open a popup with it's value #KT-6875 Fixed --- .../idea/debugger/KotlinEditorTextProvider.kt | 47 +++++++++++-------- .../selectExpression/propertyDeclaration.kt | 2 +- ...AbstractSelectExpressionForDebuggerTest.kt | 7 ++- 3 files changed, 35 insertions(+), 21 deletions(-) diff --git a/idea/src/org/jetbrains/kotlin/idea/debugger/KotlinEditorTextProvider.kt b/idea/src/org/jetbrains/kotlin/idea/debugger/KotlinEditorTextProvider.kt index ef56ebe90a3..97659e7d836 100644 --- a/idea/src/org/jetbrains/kotlin/idea/debugger/KotlinEditorTextProvider.kt +++ b/idea/src/org/jetbrains/kotlin/idea/debugger/KotlinEditorTextProvider.kt @@ -25,34 +25,36 @@ import com.intellij.debugger.engine.evaluation.TextWithImportsImpl import com.intellij.debugger.engine.evaluation.CodeFragmentKind import org.jetbrains.kotlin.idea.JetFileType import com.intellij.psi.util.PsiTreeUtil -import org.jetbrains.kotlin.psi.JetQualifiedExpression -import org.jetbrains.kotlin.psi.JetElement -import org.jetbrains.kotlin.psi.JetReferenceExpression -import org.jetbrains.kotlin.psi.JetThisExpression -import org.jetbrains.kotlin.psi.JetSimpleNameExpression -import org.jetbrains.kotlin.psi.JetOperationExpression -import org.jetbrains.kotlin.psi.JetExpression -import org.jetbrains.kotlin.psi.JetSuperExpression -import org.jetbrains.kotlin.psi.JetCodeFragment -import org.jetbrains.kotlin.psi.JetUserType -import org.jetbrains.kotlin.psi.JetImportDirective -import org.jetbrains.kotlin.psi.JetPackageDirective -import org.jetbrains.kotlin.psi.JetCallExpression -import org.jetbrains.kotlin.psi.JetArrayAccessExpression +import org.jetbrains.kotlin.psi.* class KotlinEditorTextProvider : EditorTextProvider { override fun getEditorText(elementAtCaret: PsiElement): TextWithImports? { - val expression = findExpressionInner(elementAtCaret, true) - return TextWithImportsImpl(CodeFragmentKind.EXPRESSION, expression?.getText() ?: "", "", JetFileType.INSTANCE) + val expression = findExpressionInner(elementAtCaret, true) ?: return null + + val expressionText = getElementInfo(expression) { it.getText() } + return TextWithImportsImpl(CodeFragmentKind.EXPRESSION, expressionText, "", JetFileType.INSTANCE) } override fun findExpression(elementAtCaret: PsiElement, allowMethodCalls: Boolean): Pair? { - val expression = findExpressionInner(elementAtCaret, allowMethodCalls) - if (expression == null) return null - return Pair(expression, expression.getTextRange()) + val expression = findExpressionInner(elementAtCaret, allowMethodCalls) ?: return null + + val expressionRange = getElementInfo(expression) { it.getTextRange() } + return Pair(expression, expressionRange) } default object { + + fun getElementInfo(expr: JetExpression, f: (PsiElement) -> T): T { + var expressionText = f(expr) + if (expr is JetProperty) { + val nameIdentifier = expr.getNameIdentifier() + if (nameIdentifier != null) { + expressionText = f(nameIdentifier) + } + } + return expressionText + } + fun findExpressionInner(element: PsiElement, allowMethodCalls: Boolean): JetExpression? { if (PsiTreeUtil.getParentOfType(element, javaClass(), javaClass(), javaClass()) != null) { return null @@ -61,6 +63,13 @@ class KotlinEditorTextProvider : EditorTextProvider { val jetElement = PsiTreeUtil.getParentOfType(element, javaClass()) if (jetElement == null) return null + if (jetElement is JetProperty) { + val nameIdentifier = jetElement.getNameIdentifier() + if (nameIdentifier == element) { + return jetElement + } + } + val parent = jetElement.getParent() if (parent == null) return null diff --git a/idea/testData/debugger/selectExpression/propertyDeclaration.kt b/idea/testData/debugger/selectExpression/propertyDeclaration.kt index e72a41adc4b..f94ddbcc569 100644 --- a/idea/testData/debugger/selectExpression/propertyDeclaration.kt +++ b/idea/testData/debugger/selectExpression/propertyDeclaration.kt @@ -1,3 +1,3 @@ val a = 1 -// EXPECTED: null \ No newline at end of file +// EXPECTED: a \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/idea/debugger/evaluate/AbstractSelectExpressionForDebuggerTest.kt b/idea/tests/org/jetbrains/kotlin/idea/debugger/evaluate/AbstractSelectExpressionForDebuggerTest.kt index 4cccd4ce61a..c4dbd22735e 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/debugger/evaluate/AbstractSelectExpressionForDebuggerTest.kt +++ b/idea/tests/org/jetbrains/kotlin/idea/debugger/evaluate/AbstractSelectExpressionForDebuggerTest.kt @@ -39,7 +39,12 @@ public abstract class AbstractSelectExpressionForDebuggerTest : LightCodeInsight val selectedExpression = KotlinEditorTextProvider.findExpressionInner(elementAt, allowMethodCalls) val expected = InTextDirectivesUtils.findStringWithPrefixes(myFixture.getFile()?.getText()!!, "// EXPECTED: ") - Assert.assertEquals("Another expression should be selected", expected, selectedExpression?.getText() ?: "null") + val actualResult = if (selectedExpression != null) + KotlinEditorTextProvider.getElementInfo(selectedExpression) { it.getText() } + else + "null" + + Assert.assertEquals("Another expression should be selected", expected, actualResult) } override fun getTestDataPath() = PluginTestCaseBase.getTestDataPathBase() + "/debugger/selectExpression";