Folding: fold function with expression body (KT-6316)
#KT-6316 Fixed
This commit is contained in:
committed by
Nikolay Krasko
parent
58fb1dede3
commit
e81fbe0a05
@@ -40,6 +40,7 @@ import org.jetbrains.kotlin.psi.psiUtil.allChildren
|
|||||||
import org.jetbrains.kotlin.psi.psiUtil.endOffset
|
import org.jetbrains.kotlin.psi.psiUtil.endOffset
|
||||||
import org.jetbrains.kotlin.psi.psiUtil.referenceExpression
|
import org.jetbrains.kotlin.psi.psiUtil.referenceExpression
|
||||||
import org.jetbrains.kotlin.psi.psiUtil.startOffset
|
import org.jetbrains.kotlin.psi.psiUtil.startOffset
|
||||||
|
import org.jetbrains.kotlin.psi.stubs.elements.KtFunctionElementType
|
||||||
import org.jetbrains.kotlin.resolve.calls.components.isVararg
|
import org.jetbrains.kotlin.resolve.calls.components.isVararg
|
||||||
|
|
||||||
class KotlinFoldingBuilder : CustomFoldingBuilder(), DumbAware {
|
class KotlinFoldingBuilder : CustomFoldingBuilder(), DumbAware {
|
||||||
@@ -105,6 +106,11 @@ class KotlinFoldingBuilder : CustomFoldingBuilder(), DumbAware {
|
|||||||
val type = node.elementType
|
val type = node.elementType
|
||||||
val parentType = node.treeParent?.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 ||
|
return type == KtNodeTypes.FUNCTION_LITERAL ||
|
||||||
(type == KtNodeTypes.BLOCK && parentType != KtNodeTypes.FUNCTION_LITERAL) ||
|
(type == KtNodeTypes.BLOCK && parentType != KtNodeTypes.FUNCTION_LITERAL) ||
|
||||||
type == KtNodeTypes.CLASS_BODY || type == KtTokens.BLOCK_COMMENT || type == KDocTokens.KDOC ||
|
type == KtNodeTypes.CLASS_BODY || type == KtTokens.BLOCK_COMMENT || type == KDocTokens.KDOC ||
|
||||||
@@ -132,6 +138,13 @@ class KotlinFoldingBuilder : CustomFoldingBuilder(), DumbAware {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun getRangeToFold(node: ASTNode): TextRange {
|
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) {
|
if (node.elementType == KtNodeTypes.FUNCTION_LITERAL) {
|
||||||
val psi = node.psi as? KtFunctionLiteral
|
val psi = node.psi as? KtFunctionLiteral
|
||||||
val lbrace = psi?.lBrace
|
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>
|
||||||
+10
@@ -118,6 +118,16 @@ public class KotlinFoldingTestGenerated extends AbstractKotlinFoldingTest {
|
|||||||
runTest("idea/testData/folding/checkCollapse/functionLiteral.kt");
|
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")
|
@TestMetadata("headerKDoc.kt")
|
||||||
public void testHeaderKDoc() throws Exception {
|
public void testHeaderKDoc() throws Exception {
|
||||||
runTest("idea/testData/folding/checkCollapse/headerKDoc.kt");
|
runTest("idea/testData/folding/checkCollapse/headerKDoc.kt");
|
||||||
|
|||||||
Reference in New Issue
Block a user