diff --git a/idea/src/org/jetbrains/jet/plugin/intentions/ConvertToExpressionBodyAction.kt b/idea/src/org/jetbrains/jet/plugin/intentions/ConvertToExpressionBodyAction.kt index 5aab80bbbf9..7a6bbc5aa79 100644 --- a/idea/src/org/jetbrains/jet/plugin/intentions/ConvertToExpressionBodyAction.kt +++ b/idea/src/org/jetbrains/jet/plugin/intentions/ConvertToExpressionBodyAction.kt @@ -25,6 +25,10 @@ import org.jetbrains.jet.plugin.JetBundle import org.jetbrains.jet.lang.psi.* import org.jetbrains.jet.lang.types.lang.KotlinBuiltIns import org.jetbrains.jet.lexer.JetTokens +import com.intellij.openapi.util.TextRange +import org.jetbrains.jet.lang.psi.psiUtil.siblings +import org.jetbrains.jet.plugin.caches.resolve.getLazyResolveSession +import org.jetbrains.jet.lang.descriptors.DeclarationDescriptorWithVisibility public class ConvertToExpressionBodyAction : PsiElementBaseIntentionAction() { override fun getFamilyName(): String = JetBundle.message("convert.to.expression.body.action.family.name") @@ -48,6 +52,19 @@ public class ConvertToExpressionBodyAction : PsiElementBaseIntentionAction() { val body = declaration.getBodyExpression()!! declaration.addBefore(JetPsiFactory(declaration).createEQ(), body) body.replace(value) + + if (declaration.hasDeclaredReturnType() && declaration is JetCallableDeclaration && canOmitType(declaration)) { + val typeRef = declaration.getTypeReference()!! + val colon = typeRef.siblings(forward = false, withItself = false).first { it.getNode().getElementType() == JetTokens.COLON } + val range = TextRange(colon.getTextRange().getStartOffset(), typeRef.getTextRange().getEndOffset()) + editor.getSelectionModel().setSelection(range.getStartOffset(), range.getEndOffset()) + editor.getCaretModel().moveToOffset(range.getEndOffset()) + } + } + + private fun canOmitType(declaration: JetCallableDeclaration): Boolean { + val descriptor = declaration.getLazyResolveSession().resolveToDescriptor(declaration) + return !((descriptor as? DeclarationDescriptorWithVisibility)?.getVisibility()?.isPublicAPI() ?: false) } private data class Data(val declaration: JetDeclarationWithBody, val value: JetExpression) diff --git a/idea/testData/intentions/convertToExpressionBody/anonymousObjectExpression.kt.after b/idea/testData/intentions/convertToExpressionBody/anonymousObjectExpression.kt.after index 7b8029c1e54..4bbf62a4786 100644 --- a/idea/testData/intentions/convertToExpressionBody/anonymousObjectExpression.kt.after +++ b/idea/testData/intentions/convertToExpressionBody/anonymousObjectExpression.kt.after @@ -2,7 +2,7 @@ trait 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 e42c1a944e0..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 c055e8d8ec3..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 1b12b36524c..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 e42c1a944e0..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/protectedFunNoSelection.kt b/idea/testData/intentions/convertToExpressionBody/protectedFunNoSelection.kt new file mode 100644 index 00000000000..9bf6b2b7422 --- /dev/null +++ b/idea/testData/intentions/convertToExpressionBody/protectedFunNoSelection.kt @@ -0,0 +1,5 @@ +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 new file mode 100644 index 00000000000..b6faf54f689 --- /dev/null +++ b/idea/testData/intentions/convertToExpressionBody/protectedFunNoSelection.kt.after @@ -0,0 +1,3 @@ +open class A { + protected fun foo(): String = "abc" +} diff --git a/idea/testData/intentions/convertToExpressionBody/publicFunNoSelection.kt b/idea/testData/intentions/convertToExpressionBody/publicFunNoSelection.kt new file mode 100644 index 00000000000..ed9b5c35897 --- /dev/null +++ b/idea/testData/intentions/convertToExpressionBody/publicFunNoSelection.kt @@ -0,0 +1,3 @@ +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 new file mode 100644 index 00000000000..b7352c37f3a --- /dev/null +++ b/idea/testData/intentions/convertToExpressionBody/publicFunNoSelection.kt.after @@ -0,0 +1 @@ +public fun foo(): String = "abc" \ No newline at end of file diff --git a/idea/tests/org/jetbrains/jet/plugin/intentions/IntentionTestGenerated.java b/idea/tests/org/jetbrains/jet/plugin/intentions/IntentionTestGenerated.java index f40f1fbef54..99935d70dd3 100644 --- a/idea/tests/org/jetbrains/jet/plugin/intentions/IntentionTestGenerated.java +++ b/idea/tests/org/jetbrains/jet/plugin/intentions/IntentionTestGenerated.java @@ -3138,6 +3138,18 @@ 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");