Adapted IDE to no explicit return type required for public declarations
This commit is contained in:
@@ -87,8 +87,6 @@ public class ConvertToExpressionBodyIntention : JetSelfTargetingOffsetIndependen
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun canOmitType(declaration: JetCallableDeclaration, expression: JetExpression): Boolean {
|
private fun canOmitType(declaration: JetCallableDeclaration, expression: JetExpression): Boolean {
|
||||||
if (!declaration.canRemoveTypeSpecificationByVisibility()) return false
|
|
||||||
|
|
||||||
// Workaround for anonymous objects and similar expressions without resolution scope
|
// 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
|
// 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)
|
val scopeExpression = ((declaration as? JetDeclarationWithBody)?.getBodyExpression() as? JetBlockExpression)
|
||||||
|
|||||||
@@ -16,26 +16,8 @@
|
|||||||
|
|
||||||
package org.jetbrains.kotlin.idea.intentions
|
package org.jetbrains.kotlin.idea.intentions
|
||||||
|
|
||||||
import com.intellij.codeInsight.template.*
|
|
||||||
import com.intellij.openapi.editor.Editor
|
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.*
|
||||||
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<JetCallableDeclaration>(javaClass(), "Remove explicit type specification") {
|
public class RemoveExplicitTypeIntention : JetSelfTargetingIntention<JetCallableDeclaration>(javaClass(), "Remove explicit type specification") {
|
||||||
override fun isApplicableTo(element: JetCallableDeclaration, caretOffset: Int): Boolean {
|
override fun isApplicableTo(element: JetCallableDeclaration, caretOffset: Int): Boolean {
|
||||||
@@ -45,8 +27,6 @@ public class RemoveExplicitTypeIntention : JetSelfTargetingIntention<JetCallable
|
|||||||
val initializer = (element as? JetWithExpressionInitializer)?.getInitializer()
|
val initializer = (element as? JetWithExpressionInitializer)?.getInitializer()
|
||||||
if (initializer != null && initializer.getTextRange().containsOffset(caretOffset)) return false
|
if (initializer != null && initializer.getTextRange().containsOffset(caretOffset)) return false
|
||||||
|
|
||||||
if (!element.canRemoveTypeSpecificationByVisibility()) return false
|
|
||||||
|
|
||||||
return when (element) {
|
return when (element) {
|
||||||
is JetProperty -> initializer != null
|
is JetProperty -> initializer != null
|
||||||
is JetNamedFunction -> !element.hasBlockBody() && initializer != null
|
is JetNamedFunction -> !element.hasBlockBody() && initializer != null
|
||||||
|
|||||||
@@ -19,7 +19,6 @@ package org.jetbrains.kotlin.idea.intentions
|
|||||||
import com.intellij.psi.tree.IElementType
|
import com.intellij.psi.tree.IElementType
|
||||||
import org.jetbrains.kotlin.JetNodeTypes
|
import org.jetbrains.kotlin.JetNodeTypes
|
||||||
import org.jetbrains.kotlin.descriptors.CallableDescriptor
|
import org.jetbrains.kotlin.descriptors.CallableDescriptor
|
||||||
import org.jetbrains.kotlin.descriptors.DeclarationDescriptorWithVisibility
|
|
||||||
import org.jetbrains.kotlin.descriptors.ValueParameterDescriptor
|
import org.jetbrains.kotlin.descriptors.ValueParameterDescriptor
|
||||||
import org.jetbrains.kotlin.idea.caches.resolve.analyze
|
import org.jetbrains.kotlin.idea.caches.resolve.analyze
|
||||||
import org.jetbrains.kotlin.idea.references.mainReference
|
import org.jetbrains.kotlin.idea.references.mainReference
|
||||||
@@ -71,14 +70,6 @@ fun isAutoCreatedItUsage(expression: JetSimpleNameExpression): Boolean {
|
|||||||
return context[BindingContext.AUTO_CREATED_IT, target]!!
|
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
|
// returns assignment which replaces initializer
|
||||||
fun splitPropertyDeclaration(property: JetProperty): JetBinaryExpression {
|
fun splitPropertyDeclaration(property: JetProperty): JetBinaryExpression {
|
||||||
val parent = property.getParent()!!
|
val parent = property.getParent()!!
|
||||||
|
|||||||
+1
-1
@@ -2,7 +2,7 @@ interface I {
|
|||||||
fun foo(): String
|
fun foo(): String
|
||||||
}
|
}
|
||||||
|
|
||||||
fun bar(): I = object<caret>: I {
|
fun bar()<selection>: I</selection> = object: I {
|
||||||
override fun foo(): String {
|
override fun foo(): String {
|
||||||
return "a"
|
return "a"
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-1
@@ -1,3 +1,3 @@
|
|||||||
// WITH_RUNTIME
|
// WITH_RUNTIME
|
||||||
|
|
||||||
fun foo(): Unit = throw <caret>UnsupportedOperationException()
|
fun foo()<selection>: Unit</selection> = throw UnsupportedOperationException()
|
||||||
|
|||||||
+1
-1
@@ -1,3 +1,3 @@
|
|||||||
// WITH_RUNTIME
|
// WITH_RUNTIME
|
||||||
|
|
||||||
fun foo(): Nothing = throw <caret>UnsupportedOperationException()
|
fun foo()<selection>: Nothing</selection> = throw UnsupportedOperationException()
|
||||||
|
|||||||
@@ -1 +1 @@
|
|||||||
fun <caret>foo(): String = "abc"
|
fun foo()<selection>: String</selection> = "abc"
|
||||||
+1
-1
@@ -1,3 +1,3 @@
|
|||||||
// WITH_RUNTIME
|
// WITH_RUNTIME
|
||||||
|
|
||||||
fun foo(): Unit = throw <caret>UnsupportedOperationException()
|
fun foo()<selection>: Unit</selection> = throw UnsupportedOperationException()
|
||||||
|
|||||||
@@ -1 +1 @@
|
|||||||
fun getText(): String = "xxx"<caret> //TODO
|
fun getText(): String = "xxx" //TODO
|
||||||
@@ -1,2 +1,2 @@
|
|||||||
fun getText(): String = // let's return xxx
|
fun getText(): String = // let's return xxx
|
||||||
"xxx"<caret> //TODO
|
"xxx" //TODO
|
||||||
@@ -1,2 +1,2 @@
|
|||||||
fun getText(): String = // let's return xxx
|
fun getText(): String = // let's return xxx
|
||||||
"xxx"<caret>
|
"xxx"
|
||||||
@@ -1,5 +0,0 @@
|
|||||||
open class A {
|
|
||||||
protected fun <caret>foo(): String {
|
|
||||||
return "abc"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
-3
@@ -1,3 +0,0 @@
|
|||||||
open class A {
|
|
||||||
protected fun <caret>foo(): String = "abc"
|
|
||||||
}
|
|
||||||
@@ -1,3 +0,0 @@
|
|||||||
public fun <caret>foo(): String {
|
|
||||||
return "abc"
|
|
||||||
}
|
|
||||||
-1
@@ -1 +0,0 @@
|
|||||||
public fun <caret>foo(): String = "abc"
|
|
||||||
@@ -1,2 +0,0 @@
|
|||||||
// IS_APPLICABLE: false
|
|
||||||
var x: String? = null
|
|
||||||
@@ -1,2 +0,0 @@
|
|||||||
// IS_APPLICABLE: false
|
|
||||||
public var x: String? = null
|
|
||||||
@@ -3,5 +3,5 @@ interface T{
|
|||||||
return i()
|
return i()
|
||||||
}
|
}
|
||||||
|
|
||||||
fun i(): Int = 1
|
fun i() = 1
|
||||||
}
|
}
|
||||||
@@ -4117,18 +4117,6 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
|
|||||||
doTest(fileName);
|
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")
|
@TestMetadata("returnWithNoValue.kt")
|
||||||
public void testReturnWithNoValue() throws Exception {
|
public void testReturnWithNoValue() throws Exception {
|
||||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/convertToExpressionBody/returnWithNoValue.kt");
|
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);
|
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")
|
@TestMetadata("notOnParameterOfFunctionType.kt")
|
||||||
public void testNotOnParameterOfFunctionType() throws Exception {
|
public void testNotOnParameterOfFunctionType() throws Exception {
|
||||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/removeExplicitType/notOnParameterOfFunctionType.kt");
|
String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/removeExplicitType/notOnParameterOfFunctionType.kt");
|
||||||
doTest(fileName);
|
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")
|
@TestMetadata("onOverride.kt")
|
||||||
public void testOnOverride() throws Exception {
|
public void testOnOverride() throws Exception {
|
||||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/removeExplicitType/onOverride.kt");
|
String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/removeExplicitType/onOverride.kt");
|
||||||
|
|||||||
Reference in New Issue
Block a user