Show lambda return hints only once in one-liner ifs (KT-27802)
#KT-27802 Fixed
This commit is contained in:
@@ -8,6 +8,7 @@ package org.jetbrains.kotlin.idea.parameterInfo
|
|||||||
import com.intellij.codeInsight.hints.InlayInfo
|
import com.intellij.codeInsight.hints.InlayInfo
|
||||||
import com.intellij.psi.PsiWhiteSpace
|
import com.intellij.psi.PsiWhiteSpace
|
||||||
import org.jetbrains.kotlin.idea.caches.resolve.analyze
|
import org.jetbrains.kotlin.idea.caches.resolve.analyze
|
||||||
|
import org.jetbrains.kotlin.idea.intentions.branchedTransformations.isOneLiner
|
||||||
import org.jetbrains.kotlin.psi.*
|
import org.jetbrains.kotlin.psi.*
|
||||||
import org.jetbrains.kotlin.psi.psiUtil.getParentOfType
|
import org.jetbrains.kotlin.psi.psiUtil.getParentOfType
|
||||||
import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType
|
import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType
|
||||||
@@ -15,7 +16,15 @@ import org.jetbrains.kotlin.psi.psiUtil.startOffset
|
|||||||
import org.jetbrains.kotlin.resolve.bindingContextUtil.isUsedAsResultOfLambda
|
import org.jetbrains.kotlin.resolve.bindingContextUtil.isUsedAsResultOfLambda
|
||||||
|
|
||||||
fun provideLambdaReturnValueHints(expression: KtExpression): List<InlayInfo> {
|
fun provideLambdaReturnValueHints(expression: KtExpression): List<InlayInfo> {
|
||||||
if (expression is KtIfExpression || expression is KtWhenExpression || expression is KtBlockExpression) {
|
if (expression is KtWhenExpression || expression is KtBlockExpression) {
|
||||||
|
return emptyList()
|
||||||
|
}
|
||||||
|
|
||||||
|
if (expression is KtIfExpression && !expression.isOneLiner()) {
|
||||||
|
return emptyList()
|
||||||
|
}
|
||||||
|
|
||||||
|
if (expression.getParentOfType<KtIfExpression>(true)?.isOneLiner() == true) {
|
||||||
return emptyList()
|
return emptyList()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -19,6 +19,10 @@ internal fun JavaCodeInsightTestFixture.checkHintType(text: String, hintType: Hi
|
|||||||
configureByText("A.kt", text.trimIndent())
|
configureByText("A.kt", text.trimIndent())
|
||||||
doHighlighting()
|
doHighlighting()
|
||||||
|
|
||||||
|
checkHintType(hintType)
|
||||||
|
}
|
||||||
|
|
||||||
|
internal fun JavaCodeInsightTestFixture.checkHintType(hintType: HintType) {
|
||||||
val hintInfo = getHintInfoFromProvider(caretOffset, file, editor)
|
val hintInfo = getHintInfoFromProvider(caretOffset, file, editor)
|
||||||
Assert.assertNotNull("No hint available at caret", hintInfo)
|
Assert.assertNotNull("No hint available at caret", hintInfo)
|
||||||
Assert.assertEquals(hintType.option.name, (hintInfo as HintInfo.OptionInfo).optionName)
|
Assert.assertEquals(hintType.option.name, (hintInfo as HintInfo.OptionInfo).optionName)
|
||||||
|
|||||||
@@ -5,11 +5,9 @@
|
|||||||
|
|
||||||
package org.jetbrains.kotlin.idea.parameterInfo
|
package org.jetbrains.kotlin.idea.parameterInfo
|
||||||
|
|
||||||
import com.intellij.codeInsight.hints.HintInfo
|
|
||||||
import org.jetbrains.kotlin.idea.test.KotlinLightCodeInsightFixtureTestCase
|
import org.jetbrains.kotlin.idea.test.KotlinLightCodeInsightFixtureTestCase
|
||||||
import org.jetbrains.kotlin.idea.test.KotlinLightProjectDescriptor
|
import org.jetbrains.kotlin.idea.test.KotlinLightProjectDescriptor
|
||||||
import org.jetbrains.kotlin.idea.test.KotlinWithJdkAndRuntimeLightProjectDescriptor
|
import org.jetbrains.kotlin.idea.test.KotlinWithJdkAndRuntimeLightProjectDescriptor
|
||||||
import org.junit.Assert
|
|
||||||
|
|
||||||
class LambdaImplicitHintsTest : KotlinLightCodeInsightFixtureTestCase() {
|
class LambdaImplicitHintsTest : KotlinLightCodeInsightFixtureTestCase() {
|
||||||
override fun getProjectDescriptor(): KotlinLightProjectDescriptor = KotlinWithJdkAndRuntimeLightProjectDescriptor.INSTANCE
|
override fun getProjectDescriptor(): KotlinLightProjectDescriptor = KotlinWithJdkAndRuntimeLightProjectDescriptor.INSTANCE
|
||||||
|
|||||||
@@ -15,18 +15,10 @@ class LambdaReturnValueHintsTest : KotlinLightCodeInsightFixtureTestCase() {
|
|||||||
fun check(text: String) {
|
fun check(text: String) {
|
||||||
myFixture.configureByText("A.kt", text.trimIndent())
|
myFixture.configureByText("A.kt", text.trimIndent())
|
||||||
myFixture.testInlays()
|
myFixture.testInlays()
|
||||||
}
|
|
||||||
|
|
||||||
fun testHintType() {
|
if (myFixture.editor.caretModel.offset != 0) {
|
||||||
myFixture.checkHintType(
|
myFixture.checkHintType(HintType.LAMBDA_RETURN_EXPRESSION)
|
||||||
"""
|
}
|
||||||
val x = run {
|
|
||||||
println("foo")
|
|
||||||
<caret>1
|
|
||||||
}
|
|
||||||
""",
|
|
||||||
HintType.LAMBDA_RETURN_EXPRESSION
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun testSimple() {
|
fun testSimple() {
|
||||||
@@ -34,7 +26,7 @@ class LambdaReturnValueHintsTest : KotlinLightCodeInsightFixtureTestCase() {
|
|||||||
"""
|
"""
|
||||||
val x = run {
|
val x = run {
|
||||||
println("foo")
|
println("foo")
|
||||||
<hint text="^run" />1
|
<caret><hint text="^run" />1
|
||||||
}
|
}
|
||||||
"""
|
"""
|
||||||
)
|
)
|
||||||
@@ -65,6 +57,17 @@ class LambdaReturnValueHintsTest : KotlinLightCodeInsightFixtureTestCase() {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun testOneLineIf() {
|
||||||
|
check(
|
||||||
|
"""
|
||||||
|
val x = run {
|
||||||
|
println(1)
|
||||||
|
<caret><hint text="^run"/>if (true) 1 else { 0 }
|
||||||
|
}
|
||||||
|
"""
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
fun testWhen() {
|
fun testWhen() {
|
||||||
check(
|
check(
|
||||||
"""
|
"""
|
||||||
|
|||||||
Reference in New Issue
Block a user