diff --git a/idea/src/org/jetbrains/kotlin/idea/joinLines/JoinBlockIntoSingleStatementHandler.kt b/idea/src/org/jetbrains/kotlin/idea/joinLines/JoinBlockIntoSingleStatementHandler.kt index e8028632a6d..7ac894cf4ee 100644 --- a/idea/src/org/jetbrains/kotlin/idea/joinLines/JoinBlockIntoSingleStatementHandler.kt +++ b/idea/src/org/jetbrains/kotlin/idea/joinLines/JoinBlockIntoSingleStatementHandler.kt @@ -19,9 +19,11 @@ package org.jetbrains.kotlin.idea.joinLines import com.intellij.codeInsight.editorActions.JoinRawLinesHandlerDelegate import com.intellij.openapi.editor.Document import com.intellij.psi.PsiFile +import org.jetbrains.kotlin.idea.inspections.UseExpressionBodyInspection import org.jetbrains.kotlin.idea.intentions.MergeIfsIntention import org.jetbrains.kotlin.lexer.KtTokens import org.jetbrains.kotlin.psi.* +import org.jetbrains.kotlin.psi.psiUtil.startOffset class JoinBlockIntoSingleStatementHandler : JoinRawLinesHandlerDelegate { override fun tryJoinRawLines(document: Document, file: PsiFile, start: Int, end: Int): Int { @@ -36,8 +38,12 @@ class JoinBlockIntoSingleStatementHandler : JoinRawLinesHandlerDelegate { val block = brace.parent as? KtBlockExpression ?: return -1 val statement = block.statements.singleOrNull() ?: return -1 + val parent = block.parent - if (parent !is KtContainerNode && parent !is KtWhenEntry) return -1 + val useExpressionBodyInspection = UseExpressionBodyInspection(convertEmptyToUnit = false) + val oneLineReturnFunction = (parent as? KtDeclarationWithBody)?.takeIf { useExpressionBodyInspection.isActiveFor(it) } + if (parent !is KtContainerNode && parent !is KtWhenEntry && oneLineReturnFunction == null) return -1 + if (block.node.getChildren(KtTokens.COMMENTS).isNotEmpty()) return -1 // otherwise we will loose comments // handle nested if's @@ -61,8 +67,14 @@ class JoinBlockIntoSingleStatementHandler : JoinRawLinesHandlerDelegate { } } - val newStatement = block.replace(statement) - return newStatement.textRange!!.startOffset + return if (oneLineReturnFunction != null) { + useExpressionBodyInspection.simplify(oneLineReturnFunction, false) + oneLineReturnFunction.bodyExpression!!.startOffset + } + else { + val newStatement = block.replace(statement) + newStatement.textRange!!.startOffset + } } override fun tryJoinLines(document: Document, file: PsiFile, start: Int, end: Int) = -1 diff --git a/idea/testData/joinLines/removeBraces/FunctionWithOneLineReturn.kt b/idea/testData/joinLines/removeBraces/FunctionWithOneLineReturn.kt new file mode 100644 index 00000000000..b2c88f61b4e --- /dev/null +++ b/idea/testData/joinLines/removeBraces/FunctionWithOneLineReturn.kt @@ -0,0 +1,3 @@ +fun foo(): Int { + return 1 +} diff --git a/idea/testData/joinLines/removeBraces/FunctionWithOneLineReturn.kt.after b/idea/testData/joinLines/removeBraces/FunctionWithOneLineReturn.kt.after new file mode 100644 index 00000000000..a4647ee6f2d --- /dev/null +++ b/idea/testData/joinLines/removeBraces/FunctionWithOneLineReturn.kt.after @@ -0,0 +1 @@ +fun foo(): Int = 1 \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/idea/intentions/declarations/JoinLinesTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/intentions/declarations/JoinLinesTestGenerated.java index d6cb192178d..d17ebbc20a1 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/intentions/declarations/JoinLinesTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/intentions/declarations/JoinLinesTestGenerated.java @@ -359,6 +359,12 @@ public class JoinLinesTestGenerated extends AbstractJoinLinesTest { doTest(fileName); } + @TestMetadata("FunctionWithOneLineReturn.kt") + public void testFunctionWithOneLineReturn() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/joinLines/removeBraces/FunctionWithOneLineReturn.kt"); + doTest(fileName); + } + @TestMetadata("If.kt") public void testIf() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("idea/testData/joinLines/removeBraces/If.kt");