From b0dece756a03da08c76a9f1806b9a777f774c698 Mon Sep 17 00:00:00 2001 From: Andrei Klunnyi Date: Tue, 7 Jul 2020 15:49:07 +0200 Subject: [PATCH] KT-32368 Rework Inline hints settings // migrate tests for lambdas KotlinLambdasHintsProvider which in now responsible for lambda related hints is not compatible with the existing LambdaReturnValueHintsTest. Because of that tests were migrated to the new infrastructure. --- .../kotlin/generators/tests/GenerateTests.kt | 5 + .../hints/lambda/AnnotatedStatement.kt | 16 + .../codeInsight/hints/lambda/DisabledHints.kt | 5 + .../codeInsight/hints/lambda/Elvis.kt | 7 + idea/testData/codeInsight/hints/lambda/If.kt | 8 + .../codeInsight/hints/lambda/ImplicitIt.kt | 4 + .../hints/lambda/ImplicitSingleLine.kt | 2 + .../codeInsight/hints/lambda/ImplicitThis.kt | 4 + .../codeInsight/hints/lambda/Label.kt | 5 + .../hints/lambda/LabeledStatement.kt | 13 + .../codeInsight/hints/lambda/Nested.kt | 12 + .../hints/lambda/NoHintForSingleExpression.kt | 4 + .../codeInsight/hints/lambda/OneLineIf.kt | 5 + .../hints/lambda/PostfixPrefixExpr.kt | 13 + .../codeInsight/hints/lambda/Qualified.kt | 5 + .../codeInsight/hints/lambda/ReturnFunType.kt | 5 + .../codeInsight/hints/lambda/SimpleCase.kt | 5 + .../testData/codeInsight/hints/lambda/When.kt | 7 + .../AbstractKotlinLambdasHintsProviderTest.kt | 48 +++ .../KotlinLambdasHintsProviderGenerated.java | 115 ++++++ .../parameterInfo/InlayHintTypeTestUtil.kt | 41 --- .../parameterInfo/LambdaImplicitHintsTest.kt | 59 --- .../LambdaReturnValueHintsTest.kt | 338 ------------------ 23 files changed, 288 insertions(+), 438 deletions(-) create mode 100644 idea/testData/codeInsight/hints/lambda/AnnotatedStatement.kt create mode 100644 idea/testData/codeInsight/hints/lambda/DisabledHints.kt create mode 100644 idea/testData/codeInsight/hints/lambda/Elvis.kt create mode 100644 idea/testData/codeInsight/hints/lambda/If.kt create mode 100644 idea/testData/codeInsight/hints/lambda/ImplicitIt.kt create mode 100644 idea/testData/codeInsight/hints/lambda/ImplicitSingleLine.kt create mode 100644 idea/testData/codeInsight/hints/lambda/ImplicitThis.kt create mode 100644 idea/testData/codeInsight/hints/lambda/Label.kt create mode 100644 idea/testData/codeInsight/hints/lambda/LabeledStatement.kt create mode 100644 idea/testData/codeInsight/hints/lambda/Nested.kt create mode 100644 idea/testData/codeInsight/hints/lambda/NoHintForSingleExpression.kt create mode 100644 idea/testData/codeInsight/hints/lambda/OneLineIf.kt create mode 100644 idea/testData/codeInsight/hints/lambda/PostfixPrefixExpr.kt create mode 100644 idea/testData/codeInsight/hints/lambda/Qualified.kt create mode 100644 idea/testData/codeInsight/hints/lambda/ReturnFunType.kt create mode 100644 idea/testData/codeInsight/hints/lambda/SimpleCase.kt create mode 100644 idea/testData/codeInsight/hints/lambda/When.kt create mode 100644 idea/tests/org/jetbrains/kotlin/idea/codeInsight/hints/AbstractKotlinLambdasHintsProviderTest.kt create mode 100644 idea/tests/org/jetbrains/kotlin/idea/codeInsight/hints/KotlinLambdasHintsProviderGenerated.java delete mode 100644 idea/tests/org/jetbrains/kotlin/idea/parameterInfo/InlayHintTypeTestUtil.kt delete mode 100644 idea/tests/org/jetbrains/kotlin/idea/parameterInfo/LambdaImplicitHintsTest.kt delete mode 100644 idea/tests/org/jetbrains/kotlin/idea/parameterInfo/LambdaReturnValueHintsTest.kt diff --git a/generators/tests/org/jetbrains/kotlin/generators/tests/GenerateTests.kt b/generators/tests/org/jetbrains/kotlin/generators/tests/GenerateTests.kt index a4286e49d75..e6e7dcf0654 100644 --- a/generators/tests/org/jetbrains/kotlin/generators/tests/GenerateTests.kt +++ b/generators/tests/org/jetbrains/kotlin/generators/tests/GenerateTests.kt @@ -45,6 +45,7 @@ import org.jetbrains.kotlin.idea.codeInsight.generate.AbstractCodeInsightActionT import org.jetbrains.kotlin.idea.codeInsight.generate.AbstractGenerateHashCodeAndEqualsActionTest import org.jetbrains.kotlin.idea.codeInsight.generate.AbstractGenerateTestSupportMethodActionTest import org.jetbrains.kotlin.idea.codeInsight.generate.AbstractGenerateToStringActionTest +import org.jetbrains.kotlin.idea.codeInsight.hints.AbstractKotlinLambdasHintsProvider import org.jetbrains.kotlin.idea.codeInsight.moveUpDown.AbstractMoveLeftRightTest import org.jetbrains.kotlin.idea.codeInsight.moveUpDown.AbstractMoveStatementTest import org.jetbrains.kotlin.idea.codeInsight.postfix.AbstractPostfixTemplateProviderTest @@ -879,6 +880,10 @@ fun main(args: Array) { model("codeInsight/codeVision") } + testClass { + model("codeInsight/hints/lambda") + } + testClass { model("script/definition/highlighting", extension = null, recursive = false) model("script/definition/complex", extension = null, recursive = false, testMethod = "doComplexTest") diff --git a/idea/testData/codeInsight/hints/lambda/AnnotatedStatement.kt b/idea/testData/codeInsight/hints/lambda/AnnotatedStatement.kt new file mode 100644 index 00000000000..a6fc7c5dfc3 --- /dev/null +++ b/idea/testData/codeInsight/hints/lambda/AnnotatedStatement.kt @@ -0,0 +1,16 @@ +// MODE: return +@Target(AnnotationTarget.EXPRESSION) +annotation class Some + +fun test() { + run { + val files: Any? = null + @Some + 12<# ^run #> + } + + run { + val files: Any? = null + @Some 12<# ^run #> + } +} \ No newline at end of file diff --git a/idea/testData/codeInsight/hints/lambda/DisabledHints.kt b/idea/testData/codeInsight/hints/lambda/DisabledHints.kt new file mode 100644 index 00000000000..b34640c5046 --- /dev/null +++ b/idea/testData/codeInsight/hints/lambda/DisabledHints.kt @@ -0,0 +1,5 @@ +// MODE: off +val x = run { + println("foo") + 1 +} \ No newline at end of file diff --git a/idea/testData/codeInsight/hints/lambda/Elvis.kt b/idea/testData/codeInsight/hints/lambda/Elvis.kt new file mode 100644 index 00000000000..3745e683e0c --- /dev/null +++ b/idea/testData/codeInsight/hints/lambda/Elvis.kt @@ -0,0 +1,7 @@ +// MODE: return +fun foo() { + run { + val length: Int? = null + length ?: 0<# ^run #> + } +} \ No newline at end of file diff --git a/idea/testData/codeInsight/hints/lambda/If.kt b/idea/testData/codeInsight/hints/lambda/If.kt new file mode 100644 index 00000000000..f43510d8ae8 --- /dev/null +++ b/idea/testData/codeInsight/hints/lambda/If.kt @@ -0,0 +1,8 @@ +// MODE: return +val x = run { + if (true) { + 1<# ^run #> + } else { + 0<# ^run #> + } +} \ No newline at end of file diff --git a/idea/testData/codeInsight/hints/lambda/ImplicitIt.kt b/idea/testData/codeInsight/hints/lambda/ImplicitIt.kt new file mode 100644 index 00000000000..aefc3addc63 --- /dev/null +++ b/idea/testData/codeInsight/hints/lambda/ImplicitIt.kt @@ -0,0 +1,4 @@ +// MODE: receivers_params +val x = listOf("").filter {<# it: String #> + it.startsWith("") +} \ No newline at end of file diff --git a/idea/testData/codeInsight/hints/lambda/ImplicitSingleLine.kt b/idea/testData/codeInsight/hints/lambda/ImplicitSingleLine.kt new file mode 100644 index 00000000000..0cfa6a3a968 --- /dev/null +++ b/idea/testData/codeInsight/hints/lambda/ImplicitSingleLine.kt @@ -0,0 +1,2 @@ +// MODE: receivers_params +val x = listOf("").filter { it.startsWith("") } \ No newline at end of file diff --git a/idea/testData/codeInsight/hints/lambda/ImplicitThis.kt b/idea/testData/codeInsight/hints/lambda/ImplicitThis.kt new file mode 100644 index 00000000000..2c5d63040ed --- /dev/null +++ b/idea/testData/codeInsight/hints/lambda/ImplicitThis.kt @@ -0,0 +1,4 @@ +// MODE: receivers_params +val x = buildString { + append("foo") +} \ No newline at end of file diff --git a/idea/testData/codeInsight/hints/lambda/Label.kt b/idea/testData/codeInsight/hints/lambda/Label.kt new file mode 100644 index 00000000000..2e23df2e505 --- /dev/null +++ b/idea/testData/codeInsight/hints/lambda/Label.kt @@ -0,0 +1,5 @@ +// MODE: return +val x = run foo@{ + println("foo") + 1<# ^foo #> +} \ No newline at end of file diff --git a/idea/testData/codeInsight/hints/lambda/LabeledStatement.kt b/idea/testData/codeInsight/hints/lambda/LabeledStatement.kt new file mode 100644 index 00000000000..ee4cbd41565 --- /dev/null +++ b/idea/testData/codeInsight/hints/lambda/LabeledStatement.kt @@ -0,0 +1,13 @@ +// MODE: return +fun test() { + run { + val files: Any? = null + run@ + 12<# ^run #> + } + + run { + val files: Any? = null + run@12<# ^run #> + } +} \ No newline at end of file diff --git a/idea/testData/codeInsight/hints/lambda/Nested.kt b/idea/testData/codeInsight/hints/lambda/Nested.kt new file mode 100644 index 00000000000..31e650b9405 --- /dev/null +++ b/idea/testData/codeInsight/hints/lambda/Nested.kt @@ -0,0 +1,12 @@ +// MODE: return +val x = run hello@{ + if (true) { + } + + run { // Two hints here + when (true) { + true -> 1<# ^run #> + false -> 0<# ^run #> + } + }<# ^hello #> +} \ No newline at end of file diff --git a/idea/testData/codeInsight/hints/lambda/NoHintForSingleExpression.kt b/idea/testData/codeInsight/hints/lambda/NoHintForSingleExpression.kt new file mode 100644 index 00000000000..68d3d01bd94 --- /dev/null +++ b/idea/testData/codeInsight/hints/lambda/NoHintForSingleExpression.kt @@ -0,0 +1,4 @@ +// MODE: return +val x = run { + 1 +} \ No newline at end of file diff --git a/idea/testData/codeInsight/hints/lambda/OneLineIf.kt b/idea/testData/codeInsight/hints/lambda/OneLineIf.kt new file mode 100644 index 00000000000..9476523b867 --- /dev/null +++ b/idea/testData/codeInsight/hints/lambda/OneLineIf.kt @@ -0,0 +1,5 @@ +// MODE: return +val x = run { + println(1) + if (true) 1 else { 0 }<# ^run #> +} \ No newline at end of file diff --git a/idea/testData/codeInsight/hints/lambda/PostfixPrefixExpr.kt b/idea/testData/codeInsight/hints/lambda/PostfixPrefixExpr.kt new file mode 100644 index 00000000000..300aa20ff84 --- /dev/null +++ b/idea/testData/codeInsight/hints/lambda/PostfixPrefixExpr.kt @@ -0,0 +1,13 @@ +// MODE: return +fun bar() { + var test = 0 + run { + test + test++<# ^run #> + } + + run { + test + ++test<# ^run #> + } +} \ No newline at end of file diff --git a/idea/testData/codeInsight/hints/lambda/Qualified.kt b/idea/testData/codeInsight/hints/lambda/Qualified.kt new file mode 100644 index 00000000000..0542ae18ce3 --- /dev/null +++ b/idea/testData/codeInsight/hints/lambda/Qualified.kt @@ -0,0 +1,5 @@ +// MODE: return +val x = run { + var s = "abc" + s.length<# ^run #> +} \ No newline at end of file diff --git a/idea/testData/codeInsight/hints/lambda/ReturnFunType.kt b/idea/testData/codeInsight/hints/lambda/ReturnFunType.kt new file mode 100644 index 00000000000..11deb9ffcbf --- /dev/null +++ b/idea/testData/codeInsight/hints/lambda/ReturnFunType.kt @@ -0,0 +1,5 @@ +// MODE: return +fun test() = run { + val a = 1 + { a }<# ^run #> +} \ No newline at end of file diff --git a/idea/testData/codeInsight/hints/lambda/SimpleCase.kt b/idea/testData/codeInsight/hints/lambda/SimpleCase.kt new file mode 100644 index 00000000000..a0cf3aa6e72 --- /dev/null +++ b/idea/testData/codeInsight/hints/lambda/SimpleCase.kt @@ -0,0 +1,5 @@ +// MODE: return +val x = run { + println("foo") + 1<# ^run #> +} \ No newline at end of file diff --git a/idea/testData/codeInsight/hints/lambda/When.kt b/idea/testData/codeInsight/hints/lambda/When.kt new file mode 100644 index 00000000000..75dcf9b51b3 --- /dev/null +++ b/idea/testData/codeInsight/hints/lambda/When.kt @@ -0,0 +1,7 @@ +// MODE: return +val x = run { + when (true) { + true -> 1<# ^run #> + false -> 0<# ^run #> + } +} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/idea/codeInsight/hints/AbstractKotlinLambdasHintsProviderTest.kt b/idea/tests/org/jetbrains/kotlin/idea/codeInsight/hints/AbstractKotlinLambdasHintsProviderTest.kt new file mode 100644 index 00000000000..81cb8812471 --- /dev/null +++ b/idea/tests/org/jetbrains/kotlin/idea/codeInsight/hints/AbstractKotlinLambdasHintsProviderTest.kt @@ -0,0 +1,48 @@ +/* + * Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. + */ + +package org.jetbrains.kotlin.idea.codeInsight.hints + +import com.intellij.openapi.util.io.FileUtil +import com.intellij.testFramework.LightProjectDescriptor +import com.intellij.testFramework.utils.inlays.InlayHintsProviderTestCase +import org.jetbrains.kotlin.idea.test.KotlinWithJdkAndRuntimeLightProjectDescriptor +import org.jetbrains.kotlin.test.InTextDirectivesUtils +import java.io.File + +@Suppress("UnstableApiUsage") +abstract class AbstractKotlinLambdasHintsProvider : + InlayHintsProviderTestCase() { // Abstract- prefix is just a convention for GenerateTests + + override fun getProjectDescriptor(): LightProjectDescriptor { + return KotlinWithJdkAndRuntimeLightProjectDescriptor.INSTANCE + } + + fun doTest(testPath: String) { // named according to the convention imposed by GenerateTests + assertThatActualHintsMatch(testPath) + } + + private fun assertThatActualHintsMatch(fileName: String) { + with(KotlinLambdasHintsProvider()) { + val fileContents = FileUtil.loadFile(File(fileName), true) + val settings = createSettings() + with(settings) { + when (InTextDirectivesUtils.findStringWithPrefixes(fileContents, "// MODE: ")) { + "return" -> set(returns = true) + "receivers_params" -> set(receiversAndParams = true) + "return-&-receivers_params" -> set(returns = true, receiversAndParams = true) + else -> set() + } + } + + testProvider("KotlinLambdasHintsProvider.kt", fileContents, this, settings) + } + } + + private fun KotlinLambdasHintsProvider.Settings.set(returns: Boolean = false, receiversAndParams: Boolean = false) { + this.returnExpressions = returns + this.implicitReceiversAndParams = receiversAndParams + } +} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/idea/codeInsight/hints/KotlinLambdasHintsProviderGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/codeInsight/hints/KotlinLambdasHintsProviderGenerated.java new file mode 100644 index 00000000000..02765677445 --- /dev/null +++ b/idea/tests/org/jetbrains/kotlin/idea/codeInsight/hints/KotlinLambdasHintsProviderGenerated.java @@ -0,0 +1,115 @@ +/* + * Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. + */ + +package org.jetbrains.kotlin.idea.codeInsight.hints; + +import com.intellij.testFramework.TestDataPath; +import org.jetbrains.kotlin.test.JUnit3RunnerWithInners; +import org.jetbrains.kotlin.test.KotlinTestUtils; +import org.jetbrains.kotlin.test.TestMetadata; +import org.junit.runner.RunWith; + +import java.io.File; +import java.util.regex.Pattern; + +/** This class is generated by {@link org.jetbrains.kotlin.generators.tests.TestsPackage}. DO NOT MODIFY MANUALLY */ +@SuppressWarnings("all") +@TestMetadata("idea/testData/codeInsight/hints/lambda") +@TestDataPath("$PROJECT_ROOT") +@RunWith(JUnit3RunnerWithInners.class) +public class KotlinLambdasHintsProviderGenerated extends AbstractKotlinLambdasHintsProvider { + private void runTest(String testDataFilePath) throws Exception { + KotlinTestUtils.runTest(this::doTest, this, testDataFilePath); + } + + public void testAllFilesPresentInLambda() throws Exception { + KotlinTestUtils.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("idea/testData/codeInsight/hints/lambda"), Pattern.compile("^(.+)\\.kt$"), null, true); + } + + @TestMetadata("AnnotatedStatement.kt") + public void testAnnotatedStatement() throws Exception { + runTest("idea/testData/codeInsight/hints/lambda/AnnotatedStatement.kt"); + } + + @TestMetadata("DisabledHints.kt") + public void testDisabledHints() throws Exception { + runTest("idea/testData/codeInsight/hints/lambda/DisabledHints.kt"); + } + + @TestMetadata("Elvis.kt") + public void testElvis() throws Exception { + runTest("idea/testData/codeInsight/hints/lambda/Elvis.kt"); + } + + @TestMetadata("If.kt") + public void testIf() throws Exception { + runTest("idea/testData/codeInsight/hints/lambda/If.kt"); + } + + @TestMetadata("ImplicitIt.kt") + public void testImplicitIt() throws Exception { + runTest("idea/testData/codeInsight/hints/lambda/ImplicitIt.kt"); + } + + @TestMetadata("ImplicitSingleLine.kt") + public void testImplicitSingleLine() throws Exception { + runTest("idea/testData/codeInsight/hints/lambda/ImplicitSingleLine.kt"); + } + + @TestMetadata("ImplicitThis.kt") + public void testImplicitThis() throws Exception { + runTest("idea/testData/codeInsight/hints/lambda/ImplicitThis.kt"); + } + + @TestMetadata("Label.kt") + public void testLabel() throws Exception { + runTest("idea/testData/codeInsight/hints/lambda/Label.kt"); + } + + @TestMetadata("LabeledStatement.kt") + public void testLabeledStatement() throws Exception { + runTest("idea/testData/codeInsight/hints/lambda/LabeledStatement.kt"); + } + + @TestMetadata("Nested.kt") + public void testNested() throws Exception { + runTest("idea/testData/codeInsight/hints/lambda/Nested.kt"); + } + + @TestMetadata("NoHintForSingleExpression.kt") + public void testNoHintForSingleExpression() throws Exception { + runTest("idea/testData/codeInsight/hints/lambda/NoHintForSingleExpression.kt"); + } + + @TestMetadata("OneLineIf.kt") + public void testOneLineIf() throws Exception { + runTest("idea/testData/codeInsight/hints/lambda/OneLineIf.kt"); + } + + @TestMetadata("PostfixPrefixExpr.kt") + public void testPostfixPrefixExpr() throws Exception { + runTest("idea/testData/codeInsight/hints/lambda/PostfixPrefixExpr.kt"); + } + + @TestMetadata("Qualified.kt") + public void testQualified() throws Exception { + runTest("idea/testData/codeInsight/hints/lambda/Qualified.kt"); + } + + @TestMetadata("ReturnFunType.kt") + public void testReturnFunType() throws Exception { + runTest("idea/testData/codeInsight/hints/lambda/ReturnFunType.kt"); + } + + @TestMetadata("SimpleCase.kt") + public void testSimpleCase() throws Exception { + runTest("idea/testData/codeInsight/hints/lambda/SimpleCase.kt"); + } + + @TestMetadata("When.kt") + public void testWhen() throws Exception { + runTest("idea/testData/codeInsight/hints/lambda/When.kt"); + } +} diff --git a/idea/tests/org/jetbrains/kotlin/idea/parameterInfo/InlayHintTypeTestUtil.kt b/idea/tests/org/jetbrains/kotlin/idea/parameterInfo/InlayHintTypeTestUtil.kt deleted file mode 100644 index 932946682dc..00000000000 --- a/idea/tests/org/jetbrains/kotlin/idea/parameterInfo/InlayHintTypeTestUtil.kt +++ /dev/null @@ -1,41 +0,0 @@ -/* - * Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors. - * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. - */ - -package org.jetbrains.kotlin.idea.parameterInfo - -import com.intellij.codeInsight.hints.HintInfo -import com.intellij.codeInsight.hints.InlayParameterHintsExtension -import com.intellij.openapi.editor.Editor -import com.intellij.psi.PsiElement -import com.intellij.psi.PsiFile -import com.intellij.psi.util.PsiTreeUtil -import com.intellij.testFramework.fixtures.JavaCodeInsightTestFixture -import org.jetbrains.kotlin.idea.codeInsight.hints.HintType -import org.junit.Assert - -internal fun JavaCodeInsightTestFixture.checkHintType(text: String, hintType: HintType) { - 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) -} - -// It's crucial for this method to be conformable with IDEA internals. -// Originally copied from com.intellij.codeInsight.hints.getHintInfoFromProvider() -private fun getHintInfoFromProvider(offset: Int, file: PsiFile, editor: Editor): HintInfo? { - val element = file.findElementAt(offset) ?: return null - val provider = InlayParameterHintsExtension.forLanguage(file.language) ?: return null - - val isHintOwnedByElement: (PsiElement) -> Boolean = { e -> provider.getHintInfo(e)?.isOwnedByPsiElement(e, editor) ?: false } - val method = PsiTreeUtil.findFirstParent(element, isHintOwnedByElement) ?: return null - - return provider.getHintInfo(method) -} diff --git a/idea/tests/org/jetbrains/kotlin/idea/parameterInfo/LambdaImplicitHintsTest.kt b/idea/tests/org/jetbrains/kotlin/idea/parameterInfo/LambdaImplicitHintsTest.kt deleted file mode 100644 index ff498ab2bde..00000000000 --- a/idea/tests/org/jetbrains/kotlin/idea/parameterInfo/LambdaImplicitHintsTest.kt +++ /dev/null @@ -1,59 +0,0 @@ -/* - * Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors. - * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. - */ - -package org.jetbrains.kotlin.idea.parameterInfo - -import org.jetbrains.kotlin.idea.codeInsight.hints.HintType -import org.jetbrains.kotlin.idea.test.KotlinLightCodeInsightFixtureTestCase -import org.jetbrains.kotlin.idea.test.KotlinLightProjectDescriptor -import org.jetbrains.kotlin.idea.test.KotlinWithJdkAndRuntimeLightProjectDescriptor -import org.jetbrains.kotlin.test.JUnit3WithIdeaConfigurationRunner -import org.junit.runner.RunWith - -@RunWith(JUnit3WithIdeaConfigurationRunner::class) -class LambdaImplicitHintsTest : KotlinLightCodeInsightFixtureTestCase() { - override fun getProjectDescriptor(): KotlinLightProjectDescriptor = KotlinWithJdkAndRuntimeLightProjectDescriptor.INSTANCE - - fun check(text: String) { - myFixture.configureByText("A.kt", text) - myFixture.testInlays() - } - - fun testHintType() { - myFixture.checkHintType( - """ - val x = listOf("").filter { - } - """, - HintType.LAMBDA_IMPLICIT_PARAMETER_RECEIVER - ) - } - - fun testSimpleIt() { - check( - """ - val x = listOf("").filter { - it.startsWith("") - }""" - ) - } - - fun testSimpleThis() { - check( - """ - val x = buildString { - append("foo") - }""" - ) - } - - fun testSingleLine() { - check( - """ - val x = listOf("").filter { it.startsWith("") } - """ - ) - } -} diff --git a/idea/tests/org/jetbrains/kotlin/idea/parameterInfo/LambdaReturnValueHintsTest.kt b/idea/tests/org/jetbrains/kotlin/idea/parameterInfo/LambdaReturnValueHintsTest.kt deleted file mode 100644 index 7d75da49bd9..00000000000 --- a/idea/tests/org/jetbrains/kotlin/idea/parameterInfo/LambdaReturnValueHintsTest.kt +++ /dev/null @@ -1,338 +0,0 @@ -/* - * Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors. - * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. - */ - -package org.jetbrains.kotlin.idea.parameterInfo - -import com.intellij.codeInsight.intention.IntentionAction -import com.intellij.openapi.editor.Editor -import com.intellij.openapi.editor.LineExtensionInfo -import com.intellij.openapi.editor.impl.EditorImpl -import com.intellij.testFramework.utils.inlays.InlayHintsChecker -import org.jetbrains.kotlin.idea.codeInsight.hints.HintType -import org.jetbrains.kotlin.idea.test.KotlinLightCodeInsightFixtureTestCase -import org.jetbrains.kotlin.idea.test.KotlinLightProjectDescriptor -import org.jetbrains.kotlin.idea.test.KotlinWithJdkAndRuntimeLightProjectDescriptor -import org.jetbrains.kotlin.test.JUnit3WithIdeaConfigurationRunner -import org.jetbrains.kotlin.test.TagsTestDataUtil -import org.junit.Assert -import org.junit.runner.RunWith - -@RunWith(JUnit3WithIdeaConfigurationRunner::class) -class LambdaReturnValueHintsTest : KotlinLightCodeInsightFixtureTestCase() { - companion object { - const val DISABLE_ACTION_TEXT = "Do not show lambda return expression hints" - const val ENABLE_ACTION_TEXT = "Show lambda return expression hints" - } - - override fun getProjectDescriptor(): KotlinLightProjectDescriptor = KotlinWithJdkAndRuntimeLightProjectDescriptor.INSTANCE - - private class LineExtensionInfoTag(offset: Int, data: LineExtensionInfo) : - TagsTestDataUtil.TagInfo(offset, true, true, false, data) { - - override fun getName() = "hint" - override fun getAttributesString(): String = "text=\"${data.text}\"" - } - - private class CaretTag(editor: Editor) : - TagsTestDataUtil.TagInfo( - editor.caretModel.currentCaret.offset, - /*isStart = */true, /*isClosed = */false, /*isFixed = */true, - "caret" - ) - - - private fun collectActualLineExtensionsTags(): List { - val tags = ArrayList() - val lineCount = myFixture.editor.document.lineCount - for (i in 0 until lineCount) { - val lineEndOffset = myFixture.editor.document.getLineEndOffset(i) - - (myFixture.editor as EditorImpl).processLineExtensions(i) { lineExtensionInfo -> - tags.add(LineExtensionInfoTag(lineEndOffset, lineExtensionInfo)) - true - } - } - - return tags - } - - fun check(text: String) { - myFixture.configureByText("A.kt", text.trimIndent()) - - val expectedText = run { - val tags = if (editor.caretModel.offset > 0) listOf(CaretTag(editor)) else emptyList() - TagsTestDataUtil.insertTagsInText(tags, editor.document.text) { null } - } - - // Clean test file from the hints tags - InlayHintsChecker(myFixture).extractInlaysAndCaretInfo(editor.document) - - myFixture.doHighlighting() - - Assert.assertTrue( - "No other inlays should be present in the file", - editor.inlayModel.getInlineElementsInRange(0, editor.document.textLength).isEmpty() - ) - - if (editor.caretModel.offset > 0) { - val availableIntentions = myFixture.availableIntentions - Assert.assertTrue( - "Disable action with text `$DISABLE_ACTION_TEXT` is expected: \n${availableIntentions.joinToString(separator = "\n") { " $it" }}", - availableIntentions.any { it.text == DISABLE_ACTION_TEXT } - ) - } - - val actualText = run { - val tags = ArrayList>() - - if (editor.caretModel.offset > 0) { - tags.add(CaretTag(editor)) - } - - tags.addAll(collectActualLineExtensionsTags()) - - TagsTestDataUtil.insertTagsInText(tags, editor.document.text) { null } - } - - Assert.assertEquals(expectedText, actualText) - } - - fun testDisableEnableActions() { - myFixture.configureByText( - "A.kt", - """ - val x = run { - println("foo") - 1 - } - """.trimIndent() - ) - - Assert.assertTrue("Return expression hint should be enabled", HintType.LAMBDA_RETURN_EXPRESSION.enabled) - try { - val disableIntention = findDisableReturnHintsIntention() - disableIntention.invoke(project, editor, file) - - Assert.assertFalse("Disable action doesn't work", HintType.LAMBDA_RETURN_EXPRESSION.option.get()) - - val availableIntentions = myFixture.availableIntentions - Assert.assertTrue( - intentionsPresenceErrorMessage( - "Disable action shouldn't be present when option is already disabled", availableIntentions - ), - availableIntentions.find { it.text == DISABLE_ACTION_TEXT } == null - ) - - val enableAction = availableIntentions.find { it.text == ENABLE_ACTION_TEXT } - Assert.assertTrue( - intentionsPresenceErrorMessage("No enable action with text $ENABLE_ACTION_TEXT found", availableIntentions), - enableAction != null - ) - - enableAction!!.invoke(project, editor, file) - Assert.assertTrue("Enable action doesn't work", HintType.LAMBDA_RETURN_EXPRESSION.option.get()) - - } finally { - HintType.LAMBDA_RETURN_EXPRESSION.option.set(true) - } - - } - - private fun findDisableReturnHintsIntention(): IntentionAction { - val availableIntentions = myFixture.availableIntentions - return availableIntentions.find { it.text == DISABLE_ACTION_TEXT } - ?: throw AssertionError( - intentionsPresenceErrorMessage("Disable action with text `$DISABLE_ACTION_TEXT` is expected", availableIntentions) - ) - } - - private fun intentionsPresenceErrorMessage(message: String, intentions: List): String { - return "$message: \n" + - intentions.joinToString(separator = "\n") { " $it" } - } - - fun testSimple() { - check( - """ - val x = run { - println("foo") - 1 - } - """ - ) - } - - fun testQualified() { - check( - """ - val x = run { - var s = "abc" - s.length - } - """ - ) - } - - fun testIf() { - check( - """ - val x = run { - if (true) { - 1 - } else { - 0 - } - } - """ - ) - } - - fun testOneLineIf() { - check( - """ - val x = run { - println(1) - if (true) 1 else { 0 } - } - """ - ) - } - - fun testWhen() { - check( - """ - val x = run { - when (true) { - true -> 1 - false -> 0 - } - } - """ - ) - } - - fun testNoHintForSingleExpression() { - check( - """ - val x = run { - 1 - } - """ - ) - } - - fun testLabel() { - check( - """ - val x = run foo@{ - println("foo") - 1 - } - """ - ) - } - - fun testNested() { - check( - """ - val x = run hello@{ - if (true) { - } - - run { // Two hints here - when (true) { - true -> 1 - false -> 0 - } - } - } - """ - ) - } - - fun testElvisOperator() { - check( - """ - fun foo() { - run { - val length: Int? = null - length ?: 0 - } - } - """ - ) - } - - fun testPostfixPrefixExpressions() { - check( - """ - fun bar() { - var test = 0 - run { - test - test++ - } - - run { - test - ++test - } - } - """ - ) - } - - fun testAnnotatedStatement() { - check( - """ - @Target(AnnotationTarget.EXPRESSION) - annotation class Some - - fun test() { - run { - val files: Any? = null - @Some - 12 - } - - run { - val files: Any? = null - @Some 12 - } - } - """ - ) - } - - fun testLabeledStatement() { - check( - """ - fun test() { - run { - val files: Any? = null - run@ - 12 - } - - run { - val files: Any? = null - run@12 - } - } - """ - ) - } - - fun testReturnFunctionType() { - check( - """ - fun test() = run { - val a = 1 - { a } - } - """ - ) - } -}