Join lines could "convert to expression body" #KT-15769 Fixed (#1304)
This commit is contained in:
committed by
Dmitry Jemerov
parent
ab25beeb37
commit
0b7055f7c0
+15
-3
@@ -19,9 +19,11 @@ package org.jetbrains.kotlin.idea.joinLines
|
|||||||
import com.intellij.codeInsight.editorActions.JoinRawLinesHandlerDelegate
|
import com.intellij.codeInsight.editorActions.JoinRawLinesHandlerDelegate
|
||||||
import com.intellij.openapi.editor.Document
|
import com.intellij.openapi.editor.Document
|
||||||
import com.intellij.psi.PsiFile
|
import com.intellij.psi.PsiFile
|
||||||
|
import org.jetbrains.kotlin.idea.inspections.UseExpressionBodyInspection
|
||||||
import org.jetbrains.kotlin.idea.intentions.MergeIfsIntention
|
import org.jetbrains.kotlin.idea.intentions.MergeIfsIntention
|
||||||
import org.jetbrains.kotlin.lexer.KtTokens
|
import org.jetbrains.kotlin.lexer.KtTokens
|
||||||
import org.jetbrains.kotlin.psi.*
|
import org.jetbrains.kotlin.psi.*
|
||||||
|
import org.jetbrains.kotlin.psi.psiUtil.startOffset
|
||||||
|
|
||||||
class JoinBlockIntoSingleStatementHandler : JoinRawLinesHandlerDelegate {
|
class JoinBlockIntoSingleStatementHandler : JoinRawLinesHandlerDelegate {
|
||||||
override fun tryJoinRawLines(document: Document, file: PsiFile, start: Int, end: Int): Int {
|
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 block = brace.parent as? KtBlockExpression ?: return -1
|
||||||
val statement = block.statements.singleOrNull() ?: return -1
|
val statement = block.statements.singleOrNull() ?: return -1
|
||||||
|
|
||||||
val parent = block.parent
|
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
|
if (block.node.getChildren(KtTokens.COMMENTS).isNotEmpty()) return -1 // otherwise we will loose comments
|
||||||
|
|
||||||
// handle nested if's
|
// handle nested if's
|
||||||
@@ -61,8 +67,14 @@ class JoinBlockIntoSingleStatementHandler : JoinRawLinesHandlerDelegate {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
val newStatement = block.replace(statement)
|
return if (oneLineReturnFunction != null) {
|
||||||
return newStatement.textRange!!.startOffset
|
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
|
override fun tryJoinLines(document: Document, file: PsiFile, start: Int, end: Int) = -1
|
||||||
|
|||||||
@@ -0,0 +1,3 @@
|
|||||||
|
fun foo(): Int {<caret>
|
||||||
|
return 1
|
||||||
|
}
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
fun foo(): Int =<caret> 1
|
||||||
+6
@@ -359,6 +359,12 @@ public class JoinLinesTestGenerated extends AbstractJoinLinesTest {
|
|||||||
doTest(fileName);
|
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")
|
@TestMetadata("If.kt")
|
||||||
public void testIf() throws Exception {
|
public void testIf() throws Exception {
|
||||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/joinLines/removeBraces/If.kt");
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/joinLines/removeBraces/If.kt");
|
||||||
|
|||||||
Reference in New Issue
Block a user