From 367b32c3091a932ca2fa24c52b0efa0eddf0c0b6 Mon Sep 17 00:00:00 2001 From: Valentin Kipyatkov Date: Wed, 16 Sep 2015 13:07:58 +0300 Subject: [PATCH] Adapted IDE to no explicit return type required for public declarations --- .../ConvertToExpressionBodyIntention.kt | 2 -- .../intentions/RemoveExplicitTypeIntention.kt | 20 ---------------- .../jetbrains/kotlin/idea/intentions/Utils.kt | 9 ------- .../anonymousObjectExpression.kt.after | 2 +- .../funWithImplicitUnitTypeWithThrow.kt.after | 2 +- .../funWithNothingType.kt.after | 2 +- .../funWithReturn.kt.after | 2 +- .../funWithUnitTypeWithThrow.kt.after | 2 +- .../keepComments/1.kt.after | 2 +- .../keepComments/2.kt.after | 2 +- .../keepComments/3.kt.after | 2 +- .../protectedFunNoSelection.kt | 5 ---- .../protectedFunNoSelection.kt.after | 3 --- .../publicFunNoSelection.kt | 3 --- .../publicFunNoSelection.kt.after | 1 - .../notOnDefaultVisibility.kt | 2 -- .../removeExplicitType/notOnPublic.kt | 2 -- .../basic/traitFunction.kt.after | 2 +- .../intentions/IntentionTestGenerated.java | 24 ------------------- 19 files changed, 9 insertions(+), 80 deletions(-) delete mode 100644 idea/testData/intentions/convertToExpressionBody/protectedFunNoSelection.kt delete mode 100644 idea/testData/intentions/convertToExpressionBody/protectedFunNoSelection.kt.after delete mode 100644 idea/testData/intentions/convertToExpressionBody/publicFunNoSelection.kt delete mode 100644 idea/testData/intentions/convertToExpressionBody/publicFunNoSelection.kt.after delete mode 100644 idea/testData/intentions/removeExplicitType/notOnDefaultVisibility.kt delete mode 100644 idea/testData/intentions/removeExplicitType/notOnPublic.kt diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/ConvertToExpressionBodyIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/ConvertToExpressionBodyIntention.kt index b67b9092322..4f22b42f7df 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/ConvertToExpressionBodyIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/ConvertToExpressionBodyIntention.kt @@ -87,8 +87,6 @@ public class ConvertToExpressionBodyIntention : JetSelfTargetingOffsetIndependen } private fun canOmitType(declaration: JetCallableDeclaration, expression: JetExpression): Boolean { - if (!declaration.canRemoveTypeSpecificationByVisibility()) return false - // Workaround for anonymous objects and similar expressions without resolution scope // TODO: This should probably be fixed in front-end so that resolution scope is recorded for anonymous objects as well val scopeExpression = ((declaration as? JetDeclarationWithBody)?.getBodyExpression() as? JetBlockExpression) diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/RemoveExplicitTypeIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/RemoveExplicitTypeIntention.kt index 01647b0a3b4..588d15b5786 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/RemoveExplicitTypeIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/RemoveExplicitTypeIntention.kt @@ -16,26 +16,8 @@ package org.jetbrains.kotlin.idea.intentions -import com.intellij.codeInsight.template.* import com.intellij.openapi.editor.Editor -import com.intellij.psi.PsiDocumentManager -import com.intellij.psi.PsiElement -import org.jetbrains.kotlin.builtins.KotlinBuiltIns -import org.jetbrains.kotlin.descriptors.CallableDescriptor -import org.jetbrains.kotlin.descriptors.DeclarationDescriptor -import org.jetbrains.kotlin.diagnostics.Errors -import org.jetbrains.kotlin.idea.JetBundle -import org.jetbrains.kotlin.idea.caches.resolve.analyzeFully -import org.jetbrains.kotlin.idea.util.IdeDescriptorRenderers -import org.jetbrains.kotlin.idea.util.ShortenReferences import org.jetbrains.kotlin.psi.* -import org.jetbrains.kotlin.psi.psiUtil.getElementTextWithContext -import org.jetbrains.kotlin.resolve.BindingContext -import org.jetbrains.kotlin.resolve.DescriptorUtils -import org.jetbrains.kotlin.types.ErrorUtils -import org.jetbrains.kotlin.types.JetType -import org.jetbrains.kotlin.types.TypeUtils -import java.util.* public class RemoveExplicitTypeIntention : JetSelfTargetingIntention(javaClass(), "Remove explicit type specification") { override fun isApplicableTo(element: JetCallableDeclaration, caretOffset: Int): Boolean { @@ -45,8 +27,6 @@ public class RemoveExplicitTypeIntention : JetSelfTargetingIntention initializer != null is JetNamedFunction -> !element.hasBlockBody() && initializer != null diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/Utils.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/Utils.kt index 8e8b60e2464..1a1ea9f04b3 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/Utils.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/Utils.kt @@ -19,7 +19,6 @@ package org.jetbrains.kotlin.idea.intentions import com.intellij.psi.tree.IElementType import org.jetbrains.kotlin.JetNodeTypes import org.jetbrains.kotlin.descriptors.CallableDescriptor -import org.jetbrains.kotlin.descriptors.DeclarationDescriptorWithVisibility import org.jetbrains.kotlin.descriptors.ValueParameterDescriptor import org.jetbrains.kotlin.idea.caches.resolve.analyze import org.jetbrains.kotlin.idea.references.mainReference @@ -71,14 +70,6 @@ fun isAutoCreatedItUsage(expression: JetSimpleNameExpression): Boolean { return context[BindingContext.AUTO_CREATED_IT, target]!! } -fun JetCallableDeclaration.canRemoveTypeSpecificationByVisibility(): Boolean { - val isOverride = getModifierList()?.hasModifier(JetTokens.OVERRIDE_KEYWORD) ?: false - if (isOverride) return true - - val descriptor = analyze()[BindingContext.DECLARATION_TO_DESCRIPTOR, this] - return descriptor !is DeclarationDescriptorWithVisibility || !descriptor.getVisibility().isPublicAPI -} - // returns assignment which replaces initializer fun splitPropertyDeclaration(property: JetProperty): JetBinaryExpression { val parent = property.getParent()!! diff --git a/idea/testData/intentions/convertToExpressionBody/anonymousObjectExpression.kt.after b/idea/testData/intentions/convertToExpressionBody/anonymousObjectExpression.kt.after index 49a2c477c05..2d75ef40c5c 100644 --- a/idea/testData/intentions/convertToExpressionBody/anonymousObjectExpression.kt.after +++ b/idea/testData/intentions/convertToExpressionBody/anonymousObjectExpression.kt.after @@ -2,7 +2,7 @@ interface I { fun foo(): String } -fun bar(): I = object: I { +fun bar(): I = object: I { override fun foo(): String { return "a" } diff --git a/idea/testData/intentions/convertToExpressionBody/funWithImplicitUnitTypeWithThrow.kt.after b/idea/testData/intentions/convertToExpressionBody/funWithImplicitUnitTypeWithThrow.kt.after index e23ba962b31..c6608919a2b 100644 --- a/idea/testData/intentions/convertToExpressionBody/funWithImplicitUnitTypeWithThrow.kt.after +++ b/idea/testData/intentions/convertToExpressionBody/funWithImplicitUnitTypeWithThrow.kt.after @@ -1,3 +1,3 @@ // WITH_RUNTIME -fun foo(): Unit = throw UnsupportedOperationException() +fun foo(): Unit = throw UnsupportedOperationException() diff --git a/idea/testData/intentions/convertToExpressionBody/funWithNothingType.kt.after b/idea/testData/intentions/convertToExpressionBody/funWithNothingType.kt.after index bfd91dfcbd7..1907e83ea03 100644 --- a/idea/testData/intentions/convertToExpressionBody/funWithNothingType.kt.after +++ b/idea/testData/intentions/convertToExpressionBody/funWithNothingType.kt.after @@ -1,3 +1,3 @@ // WITH_RUNTIME -fun foo(): Nothing = throw UnsupportedOperationException() +fun foo(): Nothing = throw UnsupportedOperationException() diff --git a/idea/testData/intentions/convertToExpressionBody/funWithReturn.kt.after b/idea/testData/intentions/convertToExpressionBody/funWithReturn.kt.after index a05e7397914..f58b6004871 100644 --- a/idea/testData/intentions/convertToExpressionBody/funWithReturn.kt.after +++ b/idea/testData/intentions/convertToExpressionBody/funWithReturn.kt.after @@ -1 +1 @@ -fun foo(): String = "abc" \ No newline at end of file +fun foo(): String = "abc" \ No newline at end of file diff --git a/idea/testData/intentions/convertToExpressionBody/funWithUnitTypeWithThrow.kt.after b/idea/testData/intentions/convertToExpressionBody/funWithUnitTypeWithThrow.kt.after index e23ba962b31..c6608919a2b 100644 --- a/idea/testData/intentions/convertToExpressionBody/funWithUnitTypeWithThrow.kt.after +++ b/idea/testData/intentions/convertToExpressionBody/funWithUnitTypeWithThrow.kt.after @@ -1,3 +1,3 @@ // WITH_RUNTIME -fun foo(): Unit = throw UnsupportedOperationException() +fun foo(): Unit = throw UnsupportedOperationException() diff --git a/idea/testData/intentions/convertToExpressionBody/keepComments/1.kt.after b/idea/testData/intentions/convertToExpressionBody/keepComments/1.kt.after index c2a679dc20b..39a4b2901af 100644 --- a/idea/testData/intentions/convertToExpressionBody/keepComments/1.kt.after +++ b/idea/testData/intentions/convertToExpressionBody/keepComments/1.kt.after @@ -1 +1 @@ -fun getText(): String = "xxx" //TODO \ No newline at end of file +fun getText(): String = "xxx" //TODO \ No newline at end of file diff --git a/idea/testData/intentions/convertToExpressionBody/keepComments/2.kt.after b/idea/testData/intentions/convertToExpressionBody/keepComments/2.kt.after index 47fdc28f72c..54e6c1ad0bf 100644 --- a/idea/testData/intentions/convertToExpressionBody/keepComments/2.kt.after +++ b/idea/testData/intentions/convertToExpressionBody/keepComments/2.kt.after @@ -1,2 +1,2 @@ fun getText(): String = // let's return xxx - "xxx" //TODO \ No newline at end of file + "xxx" //TODO \ No newline at end of file diff --git a/idea/testData/intentions/convertToExpressionBody/keepComments/3.kt.after b/idea/testData/intentions/convertToExpressionBody/keepComments/3.kt.after index 7e6d671957f..b1518de3dc8 100644 --- a/idea/testData/intentions/convertToExpressionBody/keepComments/3.kt.after +++ b/idea/testData/intentions/convertToExpressionBody/keepComments/3.kt.after @@ -1,2 +1,2 @@ fun getText(): String = // let's return xxx - "xxx" \ No newline at end of file + "xxx" \ No newline at end of file diff --git a/idea/testData/intentions/convertToExpressionBody/protectedFunNoSelection.kt b/idea/testData/intentions/convertToExpressionBody/protectedFunNoSelection.kt deleted file mode 100644 index 9bf6b2b7422..00000000000 --- a/idea/testData/intentions/convertToExpressionBody/protectedFunNoSelection.kt +++ /dev/null @@ -1,5 +0,0 @@ -open class A { - protected fun foo(): String { - return "abc" - } -} diff --git a/idea/testData/intentions/convertToExpressionBody/protectedFunNoSelection.kt.after b/idea/testData/intentions/convertToExpressionBody/protectedFunNoSelection.kt.after deleted file mode 100644 index b6faf54f689..00000000000 --- a/idea/testData/intentions/convertToExpressionBody/protectedFunNoSelection.kt.after +++ /dev/null @@ -1,3 +0,0 @@ -open class A { - protected fun foo(): String = "abc" -} diff --git a/idea/testData/intentions/convertToExpressionBody/publicFunNoSelection.kt b/idea/testData/intentions/convertToExpressionBody/publicFunNoSelection.kt deleted file mode 100644 index ed9b5c35897..00000000000 --- a/idea/testData/intentions/convertToExpressionBody/publicFunNoSelection.kt +++ /dev/null @@ -1,3 +0,0 @@ -public fun foo(): String { - return "abc" -} \ No newline at end of file diff --git a/idea/testData/intentions/convertToExpressionBody/publicFunNoSelection.kt.after b/idea/testData/intentions/convertToExpressionBody/publicFunNoSelection.kt.after deleted file mode 100644 index b7352c37f3a..00000000000 --- a/idea/testData/intentions/convertToExpressionBody/publicFunNoSelection.kt.after +++ /dev/null @@ -1 +0,0 @@ -public fun foo(): String = "abc" \ No newline at end of file diff --git a/idea/testData/intentions/removeExplicitType/notOnDefaultVisibility.kt b/idea/testData/intentions/removeExplicitType/notOnDefaultVisibility.kt deleted file mode 100644 index dea17ec93a2..00000000000 --- a/idea/testData/intentions/removeExplicitType/notOnDefaultVisibility.kt +++ /dev/null @@ -1,2 +0,0 @@ -// IS_APPLICABLE: false -var x: String? = null \ No newline at end of file diff --git a/idea/testData/intentions/removeExplicitType/notOnPublic.kt b/idea/testData/intentions/removeExplicitType/notOnPublic.kt deleted file mode 100644 index 4d2853f726b..00000000000 --- a/idea/testData/intentions/removeExplicitType/notOnPublic.kt +++ /dev/null @@ -1,2 +0,0 @@ -// IS_APPLICABLE: false -public var x: String? = null \ No newline at end of file diff --git a/idea/testData/refactoring/extractFunction/basic/traitFunction.kt.after b/idea/testData/refactoring/extractFunction/basic/traitFunction.kt.after index 56a356b550a..7807b894d09 100644 --- a/idea/testData/refactoring/extractFunction/basic/traitFunction.kt.after +++ b/idea/testData/refactoring/extractFunction/basic/traitFunction.kt.after @@ -3,5 +3,5 @@ interface T{ return i() } - fun i(): Int = 1 + fun i() = 1 } \ 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 9d8e21210c8..1986ba4f345 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java @@ -4117,18 +4117,6 @@ public class IntentionTestGenerated extends AbstractIntentionTest { doTest(fileName); } - @TestMetadata("protectedFunNoSelection.kt") - public void testProtectedFunNoSelection() throws Exception { - String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/convertToExpressionBody/protectedFunNoSelection.kt"); - doTest(fileName); - } - - @TestMetadata("publicFunNoSelection.kt") - public void testPublicFunNoSelection() throws Exception { - String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/convertToExpressionBody/publicFunNoSelection.kt"); - doTest(fileName); - } - @TestMetadata("returnWithNoValue.kt") public void testReturnWithNoValue() throws Exception { String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/convertToExpressionBody/returnWithNoValue.kt"); @@ -6115,24 +6103,12 @@ public class IntentionTestGenerated extends AbstractIntentionTest { JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/intentions/removeExplicitType"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), true); } - @TestMetadata("notOnDefaultVisibility.kt") - public void testNotOnDefaultVisibility() throws Exception { - String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/removeExplicitType/notOnDefaultVisibility.kt"); - doTest(fileName); - } - @TestMetadata("notOnParameterOfFunctionType.kt") public void testNotOnParameterOfFunctionType() throws Exception { String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/removeExplicitType/notOnParameterOfFunctionType.kt"); doTest(fileName); } - @TestMetadata("notOnPublic.kt") - public void testNotOnPublic() throws Exception { - String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/removeExplicitType/notOnPublic.kt"); - doTest(fileName); - } - @TestMetadata("onOverride.kt") public void testOnOverride() throws Exception { String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/removeExplicitType/onOverride.kt");