diff --git a/generators/tests/org/jetbrains/kotlin/generators/tests/GenerateTests.kt b/generators/tests/org/jetbrains/kotlin/generators/tests/GenerateTests.kt index a258ba4f77e..4739fc48607 100644 --- a/generators/tests/org/jetbrains/kotlin/generators/tests/GenerateTests.kt +++ b/generators/tests/org/jetbrains/kotlin/generators/tests/GenerateTests.kt @@ -180,6 +180,7 @@ import org.jetbrains.kotlin.samWithReceiver.AbstractSamWithReceiverScriptTest import org.jetbrains.kotlin.samWithReceiver.AbstractSamWithReceiverTest import org.jetbrains.kotlin.search.AbstractAnnotatedMembersSearchTest import org.jetbrains.kotlin.search.AbstractInheritorsSearchTest +import org.jetbrains.kotlin.shortenRefs.AbstractFirShortenRefsTest import org.jetbrains.kotlin.shortenRefs.AbstractShortenRefsTest import org.jetbrains.kotlin.test.TargetBackend import org.jetbrains.kotlin.tools.projectWizard.cli.AbstractProjectTemplateBuildFileGenerationTest @@ -1117,6 +1118,10 @@ fun main(args: Array) { val pattern = "^([\\w\\-_]+)\\.(kt|kts)$" model("intentions/specifyTypeExplicitly", pattern = pattern) } + + testClass { + model("shortenRefsFir", pattern = KT_WITHOUT_DOTS_IN_NAME, testMethod = "doTestWithMuting") + } } testGroup("idea/idea-fir/tests", "idea/idea-completion/testData") { diff --git a/idea/idea-fir/tests/org/jetbrains/kotlin/shortenRefs/AbstractFirShortenRefsTest.kt b/idea/idea-fir/tests/org/jetbrains/kotlin/shortenRefs/AbstractFirShortenRefsTest.kt new file mode 100644 index 00000000000..ba9665c218a --- /dev/null +++ b/idea/idea-fir/tests/org/jetbrains/kotlin/shortenRefs/AbstractFirShortenRefsTest.kt @@ -0,0 +1,53 @@ +/* + * 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.shortenRefs + +import com.intellij.openapi.application.ApplicationManager +import org.jetbrains.kotlin.AbstractImportsTest +import org.jetbrains.kotlin.idea.frontend.api.analyze +import org.jetbrains.kotlin.idea.util.application.executeWriteCommand +import org.jetbrains.kotlin.idea.util.application.runReadAction +import org.jetbrains.kotlin.psi.KtFile +import org.jetbrains.kotlin.test.uitls.IgnoreTests + +abstract class AbstractFirShortenRefsTest : AbstractImportsTest() { + override val captureExceptions: Boolean = false + + override fun doTest(file: KtFile): String? { + val selectionModel = myFixture.editor.selectionModel + if (!selectionModel.hasSelection()) error("No selection in input file") + + val (startOffset, endOffset) = runReadAction { selectionModel.selectionStart to selectionModel.selectionEnd } + + val shortenings = executeOnPooledThread { + runReadAction { + analyze(file) { + collectPossibleReferenceShortenings(file, startOffset, endOffset) + } + } + } + + project.executeWriteCommand("") { + shortenings.invokeShortening() + } + + selectionModel.removeSelection() + return null + } + + override val runTestInWriteCommand: Boolean = false + + protected fun doTestWithMuting(unused: String) { + IgnoreTests.runTestIfEnabledByFileDirective(testDataFile().toPath(), IgnoreTests.DIRECTIVES.FIR_COMPARISON, ".after") { + doTest(unused) + } + } + + override val nameCountToUseStarImportDefault: Int + get() = Integer.MAX_VALUE +} + +private fun executeOnPooledThread(action: () -> R): R = + ApplicationManager.getApplication().executeOnPooledThread { action() }.get() diff --git a/idea/idea-fir/tests/org/jetbrains/kotlin/shortenRefs/FirShortenRefsTestGenerated.java b/idea/idea-fir/tests/org/jetbrains/kotlin/shortenRefs/FirShortenRefsTestGenerated.java new file mode 100644 index 00000000000..b075dfbe794 --- /dev/null +++ b/idea/idea-fir/tests/org/jetbrains/kotlin/shortenRefs/FirShortenRefsTestGenerated.java @@ -0,0 +1,49 @@ +/* + * 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.shortenRefs; + +import com.intellij.testFramework.TestDataPath; +import org.jetbrains.kotlin.test.JUnit3RunnerWithInners; +import org.jetbrains.kotlin.test.KotlinTestUtils; +import org.jetbrains.kotlin.test.util.KtTestUtil; +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/shortenRefsFir") +@TestDataPath("$PROJECT_ROOT") +@RunWith(JUnit3RunnerWithInners.class) +public class FirShortenRefsTestGenerated extends AbstractFirShortenRefsTest { + private void runTest(String testDataFilePath) throws Exception { + KotlinTestUtils.runTest(this::doTestWithMuting, this, testDataFilePath); + } + + public void testAllFilesPresentInShortenRefsFir() throws Exception { + KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("idea/testData/shortenRefsFir"), Pattern.compile("^([^.]+)\\.kt$"), null, true); + } + + @TestMetadata("idea/testData/shortenRefsFir/types") + @TestDataPath("$PROJECT_ROOT") + @RunWith(JUnit3RunnerWithInners.class) + public static class Types extends AbstractFirShortenRefsTest { + private void runTest(String testDataFilePath) throws Exception { + KotlinTestUtils.runTest(this::doTestWithMuting, this, testDataFilePath); + } + + public void testAllFilesPresentInTypes() throws Exception { + KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("idea/testData/shortenRefsFir/types"), Pattern.compile("^([^.]+)\\.kt$"), null, true); + } + + @TestMetadata("ParameterType.kt") + public void testParameterType() throws Exception { + runTest("idea/testData/shortenRefsFir/types/ParameterType.kt"); + } + } +} diff --git a/idea/testData/shortenRefsFir/types/ParameterType.kt b/idea/testData/shortenRefsFir/types/ParameterType.kt new file mode 100644 index 00000000000..73512a538fa --- /dev/null +++ b/idea/testData/shortenRefsFir/types/ParameterType.kt @@ -0,0 +1,6 @@ +// FIR_COMPARISON +package test + +class T + +fun foo(t: test.T) {} diff --git a/idea/testData/shortenRefsFir/types/ParameterType.kt.after b/idea/testData/shortenRefsFir/types/ParameterType.kt.after new file mode 100644 index 00000000000..9a84264e1b8 --- /dev/null +++ b/idea/testData/shortenRefsFir/types/ParameterType.kt.after @@ -0,0 +1,6 @@ +// FIR_COMPARISON +package test + +class T + +fun foo(t: T) {}