Folding: fold function with expression body (KT-6316)

#KT-6316 Fixed
This commit is contained in:
Toshiaki Kameyama
2019-09-14 10:22:08 +09:00
committed by Nikolay Krasko
parent 58fb1dede3
commit e81fbe0a05
4 changed files with 32 additions and 0 deletions
@@ -40,6 +40,7 @@ import org.jetbrains.kotlin.psi.psiUtil.allChildren
import org.jetbrains.kotlin.psi.psiUtil.endOffset
import org.jetbrains.kotlin.psi.psiUtil.referenceExpression
import org.jetbrains.kotlin.psi.psiUtil.startOffset
import org.jetbrains.kotlin.psi.stubs.elements.KtFunctionElementType
import org.jetbrains.kotlin.resolve.calls.components.isVararg
class KotlinFoldingBuilder : CustomFoldingBuilder(), DumbAware {
@@ -104,6 +105,11 @@ class KotlinFoldingBuilder : CustomFoldingBuilder(), DumbAware {
private fun needFolding(node: ASTNode, document: Document): Boolean {
val type = node.elementType
val parentType = node.treeParent?.elementType
if (type is KtFunctionElementType) {
val bodyExpression = (node.psi as? KtNamedFunction)?.bodyExpression
if (bodyExpression != null && bodyExpression !is KtBlockExpression) return true
}
return type == KtNodeTypes.FUNCTION_LITERAL ||
(type == KtNodeTypes.BLOCK && parentType != KtNodeTypes.FUNCTION_LITERAL) ||
@@ -132,6 +138,13 @@ class KotlinFoldingBuilder : CustomFoldingBuilder(), DumbAware {
}
private fun getRangeToFold(node: ASTNode): TextRange {
if (node.elementType is KtFunctionElementType) {
val bodyExpression = (node.psi as? KtNamedFunction)?.bodyExpression
if (bodyExpression != null && bodyExpression !is KtBlockExpression) {
return bodyExpression.textRange
}
}
if (node.elementType == KtNodeTypes.FUNCTION_LITERAL) {
val psi = node.psi as? KtFunctionLiteral
val lbrace = psi?.lBrace
@@ -0,0 +1,4 @@
fun test() = <fold text='{...}' expand='true'>if (true)
1
else
0</fold>
@@ -0,0 +1,5 @@
fun test() =
<fold text='{...}' expand='true'>if (true)
1
else
0</fold>
@@ -118,6 +118,16 @@ public class KotlinFoldingTestGenerated extends AbstractKotlinFoldingTest {
runTest("idea/testData/folding/checkCollapse/functionLiteral.kt");
}
@TestMetadata("functionWithExpressionBody.kt")
public void testFunctionWithExpressionBody() throws Exception {
runTest("idea/testData/folding/checkCollapse/functionWithExpressionBody.kt");
}
@TestMetadata("functionWithExpressionBody2.kt")
public void testFunctionWithExpressionBody2() throws Exception {
runTest("idea/testData/folding/checkCollapse/functionWithExpressionBody2.kt");
}
@TestMetadata("headerKDoc.kt")
public void testHeaderKDoc() throws Exception {
runTest("idea/testData/folding/checkCollapse/headerKDoc.kt");