@@ -19,9 +19,11 @@ package org.jetbrains.kotlin.idea.joinLines
19
19
import com.intellij.codeInsight.editorActions.JoinRawLinesHandlerDelegate
20
20
import com.intellij.openapi.editor.Document
21
21
import com.intellij.psi.PsiFile
22
+ import org.jetbrains.kotlin.idea.inspections.UseExpressionBodyInspection
22
23
import org.jetbrains.kotlin.idea.intentions.MergeIfsIntention
23
24
import org.jetbrains.kotlin.lexer.KtTokens
24
25
import org.jetbrains.kotlin.psi.*
26
+ import org.jetbrains.kotlin.psi.psiUtil.startOffset
25
27
26
28
class JoinBlockIntoSingleStatementHandler : JoinRawLinesHandlerDelegate {
27
29
override fun tryJoinRawLines (document : Document , file : PsiFile , start : Int , end : Int ): Int {
@@ -36,8 +38,12 @@ class JoinBlockIntoSingleStatementHandler : JoinRawLinesHandlerDelegate {
36
38
37
39
val block = brace.parent as ? KtBlockExpression ? : return - 1
38
40
val statement = block.statements.singleOrNull() ? : return - 1
41
+
39
42
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
+
41
47
if (block.node.getChildren(KtTokens .COMMENTS ).isNotEmpty()) return - 1 // otherwise we will loose comments
42
48
43
49
// handle nested if's
@@ -61,8 +67,14 @@ class JoinBlockIntoSingleStatementHandler : JoinRawLinesHandlerDelegate {
61
67
}
62
68
}
63
69
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
+ }
66
78
}
67
79
68
80
override fun tryJoinLines (document : Document , file : PsiFile , start : Int , end : Int ) = - 1
0 commit comments