From bc432ecb852e74ab2af5009cf33713ecf3ccdcc3 Mon Sep 17 00:00:00 2001 From: Leonid Startsev Date: Tue, 8 Sep 2020 17:34:53 +0300 Subject: [PATCH] Improve remove/specify type intentions in Explicit API mode Do not suggest to remove type for public declarations #KT-38915 Fixed Do not show intention to specify type when corresponding quickfix is available #KT-39026 Fixed --- .../checkers/ExplicitApiDeclarationChecker.kt | 16 +++++++++++++ .../intentions/RemoveExplicitTypeIntention.kt | 11 ++++++++- .../SpecifyTypeExplicitlyIntention.kt | 9 ++++++++ .../removeExplicitTypeWithApiMode/.intention | 1 + .../nonPublicFunction.kt | 4 ++++ .../nonPublicFunction.kt.after | 4 ++++ .../publicFunction.kt | 4 ++++ .../intentions/IntentionTestGenerated.java | 23 +++++++++++++++++++ 8 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 idea/testData/intentions/removeExplicitTypeWithApiMode/.intention create mode 100644 idea/testData/intentions/removeExplicitTypeWithApiMode/nonPublicFunction.kt create mode 100644 idea/testData/intentions/removeExplicitTypeWithApiMode/nonPublicFunction.kt.after create mode 100644 idea/testData/intentions/removeExplicitTypeWithApiMode/publicFunction.kt diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/checkers/ExplicitApiDeclarationChecker.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/checkers/ExplicitApiDeclarationChecker.kt index 8e48b58c3f2..19d0494d31c 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/checkers/ExplicitApiDeclarationChecker.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/checkers/ExplicitApiDeclarationChecker.kt @@ -7,6 +7,7 @@ package org.jetbrains.kotlin.resolve.checkers import org.jetbrains.kotlin.config.AnalysisFlags import org.jetbrains.kotlin.config.ExplicitApiMode +import org.jetbrains.kotlin.config.LanguageVersionSettings import org.jetbrains.kotlin.descriptors.* import org.jetbrains.kotlin.diagnostics.Errors import org.jetbrains.kotlin.diagnostics.reportDiagnosticOnce @@ -122,5 +123,20 @@ class ExplicitApiDeclarationChecker : DeclarationChecker { return true } + + fun publicReturnTypeShouldBePresentInApiMode( + element: KtCallableDeclaration, + languageVersionSettings: LanguageVersionSettings, + descriptor: DeclarationDescriptor? + ): Boolean { + val isInApiMode = languageVersionSettings.getFlag(AnalysisFlags.explicitApiMode) != ExplicitApiMode.DISABLED + return isInApiMode && returnTypeRequired( + element, + descriptor, + checkForPublicApi = true, + checkForInternal = false, + checkForPrivate = false + ) + } } } diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/RemoveExplicitTypeIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/RemoveExplicitTypeIntention.kt index bc2bd34d6a2..8621cf02999 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/RemoveExplicitTypeIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/RemoveExplicitTypeIntention.kt @@ -10,6 +10,8 @@ import com.intellij.openapi.editor.Editor import com.intellij.openapi.util.TextRange import org.jetbrains.kotlin.descriptors.FunctionDescriptor import org.jetbrains.kotlin.idea.KotlinBundle +import org.jetbrains.kotlin.idea.caches.resolve.resolveToDescriptorIfAny +import org.jetbrains.kotlin.idea.project.languageVersionSettings import org.jetbrains.kotlin.idea.refactoring.addTypeArgumentsIfNeeded import org.jetbrains.kotlin.idea.refactoring.getQualifiedTypeArgumentList import org.jetbrains.kotlin.idea.references.mainReference @@ -18,6 +20,7 @@ import org.jetbrains.kotlin.lexer.KtTokens import org.jetbrains.kotlin.psi.* import org.jetbrains.kotlin.psi.psiUtil.endOffset import org.jetbrains.kotlin.psi.psiUtil.startOffset +import org.jetbrains.kotlin.resolve.checkers.ExplicitApiDeclarationChecker import org.jetbrains.kotlin.types.typeUtil.isUnit class RemoveExplicitTypeIntention : SelfTargetingRangeIntention( @@ -56,6 +59,12 @@ class RemoveExplicitTypeIntention : SelfTargetingRangeIntentionInt = 42 \ No newline at end of file diff --git a/idea/testData/intentions/removeExplicitTypeWithApiMode/nonPublicFunction.kt.after b/idea/testData/intentions/removeExplicitTypeWithApiMode/nonPublicFunction.kt.after new file mode 100644 index 00000000000..b49c73cf86a --- /dev/null +++ b/idea/testData/intentions/removeExplicitTypeWithApiMode/nonPublicFunction.kt.after @@ -0,0 +1,4 @@ +// COMPILER_ARGUMENTS: -Xexplicit-api=strict +// IS_APPLICABLE: true + +internal fun foo() = 42 diff --git a/idea/testData/intentions/removeExplicitTypeWithApiMode/publicFunction.kt b/idea/testData/intentions/removeExplicitTypeWithApiMode/publicFunction.kt new file mode 100644 index 00000000000..b18c3bf1f57 --- /dev/null +++ b/idea/testData/intentions/removeExplicitTypeWithApiMode/publicFunction.kt @@ -0,0 +1,4 @@ +// COMPILER_ARGUMENTS: -Xexplicit-api=strict +// IS_APPLICABLE: false + +public fun foo(): Int = 42 \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java index f9800d73e3a..f8571978745 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java @@ -14417,6 +14417,29 @@ public class IntentionTestGenerated extends AbstractIntentionTest { } } + @TestMetadata("idea/testData/intentions/removeExplicitTypeWithApiMode") + @TestDataPath("$PROJECT_ROOT") + @RunWith(JUnit3RunnerWithInners.class) + public static class RemoveExplicitTypeWithApiMode extends AbstractIntentionTest { + private void runTest(String testDataFilePath) throws Exception { + KotlinTestUtils.runTest(this::doTest, this, testDataFilePath); + } + + public void testAllFilesPresentInRemoveExplicitTypeWithApiMode() throws Exception { + KotlinTestUtils.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("idea/testData/intentions/removeExplicitTypeWithApiMode"), Pattern.compile("^([\\w\\-_]+)\\.(kt|kts)$"), null, true); + } + + @TestMetadata("nonPublicFunction.kt") + public void testNonPublicFunction() throws Exception { + runTest("idea/testData/intentions/removeExplicitTypeWithApiMode/nonPublicFunction.kt"); + } + + @TestMetadata("publicFunction.kt") + public void testPublicFunction() throws Exception { + runTest("idea/testData/intentions/removeExplicitTypeWithApiMode/publicFunction.kt"); + } + } + @TestMetadata("idea/testData/intentions/removeForLoopIndices") @TestDataPath("$PROJECT_ROOT") @RunWith(JUnit3RunnerWithInners.class)