Skip to content

Commit 0b7055f

Browse files
t-kameyamayole
authored andcommitted
Join lines could "convert to expression body" #KT-15769 Fixed (JetBrains#1304)
1 parent ab25bee commit 0b7055f

File tree

4 files changed

+25
-3
lines changed

4 files changed

+25
-3
lines changed

idea/src/org/jetbrains/kotlin/idea/joinLines/JoinBlockIntoSingleStatementHandler.kt

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@ package org.jetbrains.kotlin.idea.joinLines
1919
import com.intellij.codeInsight.editorActions.JoinRawLinesHandlerDelegate
2020
import com.intellij.openapi.editor.Document
2121
import com.intellij.psi.PsiFile
22+
import org.jetbrains.kotlin.idea.inspections.UseExpressionBodyInspection
2223
import org.jetbrains.kotlin.idea.intentions.MergeIfsIntention
2324
import org.jetbrains.kotlin.lexer.KtTokens
2425
import org.jetbrains.kotlin.psi.*
26+
import org.jetbrains.kotlin.psi.psiUtil.startOffset
2527

2628
class JoinBlockIntoSingleStatementHandler : JoinRawLinesHandlerDelegate {
2729
override fun tryJoinRawLines(document: Document, file: PsiFile, start: Int, end: Int): Int {
@@ -36,8 +38,12 @@ class JoinBlockIntoSingleStatementHandler : JoinRawLinesHandlerDelegate {
3638

3739
val block = brace.parent as? KtBlockExpression ?: return -1
3840
val statement = block.statements.singleOrNull() ?: return -1
41+
3942
val parent = block.parent
40-
if (parent !is KtContainerNode && parent !is KtWhenEntry) return -1
43+
val useExpressionBodyInspection = UseExpressionBodyInspection(convertEmptyToUnit = false)
44+
val oneLineReturnFunction = (parent as? KtDeclarationWithBody)?.takeIf { useExpressionBodyInspection.isActiveFor(it) }
45+
if (parent !is KtContainerNode && parent !is KtWhenEntry && oneLineReturnFunction == null) return -1
46+
4147
if (block.node.getChildren(KtTokens.COMMENTS).isNotEmpty()) return -1 // otherwise we will loose comments
4248

4349
// handle nested if's
@@ -61,8 +67,14 @@ class JoinBlockIntoSingleStatementHandler : JoinRawLinesHandlerDelegate {
6167
}
6268
}
6369

64-
val newStatement = block.replace(statement)
65-
return newStatement.textRange!!.startOffset
70+
return if (oneLineReturnFunction != null) {
71+
useExpressionBodyInspection.simplify(oneLineReturnFunction, false)
72+
oneLineReturnFunction.bodyExpression!!.startOffset
73+
}
74+
else {
75+
val newStatement = block.replace(statement)
76+
newStatement.textRange!!.startOffset
77+
}
6678
}
6779

6880
override fun tryJoinLines(document: Document, file: PsiFile, start: Int, end: Int) = -1
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
fun foo(): Int {<caret>
2+
return 1
3+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
fun foo(): Int =<caret> 1

idea/tests/org/jetbrains/kotlin/idea/intentions/declarations/JoinLinesTestGenerated.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,12 @@ public void testFunctionBody() throws Exception {
359359
doTest(fileName);
360360
}
361361

362+
@TestMetadata("FunctionWithOneLineReturn.kt")
363+
public void testFunctionWithOneLineReturn() throws Exception {
364+
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/joinLines/removeBraces/FunctionWithOneLineReturn.kt");
365+
doTest(fileName);
366+
}
367+
362368
@TestMetadata("If.kt")
363369
public void testIf() throws Exception {
364370
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/joinLines/removeBraces/If.kt");

0 commit comments

Comments
 (0)