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.psi.PsiWhiteSpace
|
||||
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.psiUtil.getParentOfType
|
||||
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
|
||||
|
||||
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()
|
||||
}
|
||||
|
||||
|
||||
@@ -19,6 +19,10 @@ internal fun JavaCodeInsightTestFixture.checkHintType(text: String, hintType: Hi
|
||||
configureByText("A.kt", text.trimIndent())
|
||||
doHighlighting()
|
||||
|
||||
checkHintType(hintType)
|
||||
}
|
||||
|
||||
internal fun JavaCodeInsightTestFixture.checkHintType(hintType: HintType) {
|
||||
val hintInfo = getHintInfoFromProvider(caretOffset, file, editor)
|
||||
Assert.assertNotNull("No hint available at caret", hintInfo)
|
||||
Assert.assertEquals(hintType.option.name, (hintInfo as HintInfo.OptionInfo).optionName)
|
||||
|
||||
@@ -5,11 +5,9 @@
|
||||
|
||||
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.KotlinLightProjectDescriptor
|
||||
import org.jetbrains.kotlin.idea.test.KotlinWithJdkAndRuntimeLightProjectDescriptor
|
||||
import org.junit.Assert
|
||||
|
||||
class LambdaImplicitHintsTest : KotlinLightCodeInsightFixtureTestCase() {
|
||||
override fun getProjectDescriptor(): KotlinLightProjectDescriptor = KotlinWithJdkAndRuntimeLightProjectDescriptor.INSTANCE
|
||||
|
||||
@@ -15,18 +15,10 @@ class LambdaReturnValueHintsTest : KotlinLightCodeInsightFixtureTestCase() {
|
||||
fun check(text: String) {
|
||||
myFixture.configureByText("A.kt", text.trimIndent())
|
||||
myFixture.testInlays()
|
||||
}
|
||||
|
||||
fun testHintType() {
|
||||
myFixture.checkHintType(
|
||||
"""
|
||||
val x = run {
|
||||
println("foo")
|
||||
<caret>1
|
||||
}
|
||||
""",
|
||||
HintType.LAMBDA_RETURN_EXPRESSION
|
||||
)
|
||||
if (myFixture.editor.caretModel.offset != 0) {
|
||||
myFixture.checkHintType(HintType.LAMBDA_RETURN_EXPRESSION)
|
||||
}
|
||||
}
|
||||
|
||||
fun testSimple() {
|
||||
@@ -34,7 +26,7 @@ class LambdaReturnValueHintsTest : KotlinLightCodeInsightFixtureTestCase() {
|
||||
"""
|
||||
val x = run {
|
||||
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() {
|
||||
check(
|
||||
"""
|
||||
|
||||
Reference in New Issue
Block a user