Add bodyBlockExpression() helper function to KtDeclarationWithBody

This commit is contained in:
Nikolay Krasko
2018-11-08 16:02:11 +03:00
parent 2b8a81c5a8
commit 2284028bbe
19 changed files with 45 additions and 36 deletions
@@ -41,5 +41,15 @@ public interface KtDeclarationWithBody extends KtDeclaration {
@NotNull
List<KtParameter> getValueParameters();
@Nullable
default KtBlockExpression getBodyBlockExpression() {
KtExpression bodyExpression = getBodyExpression();
if (bodyExpression instanceof KtBlockExpression) {
return (KtBlockExpression) bodyExpression;
}
return null;
}
}
@@ -276,7 +276,7 @@ class KtPsiFactory @JvmOverloads constructor(private val project: Project, val m
}
fun createDestructuringDeclaration(text: String): KtDestructuringDeclaration {
return (createFunction("fun foo() {$text}").bodyExpression as KtBlockExpression).statements.first() as KtDestructuringDeclaration
return createFunction("fun foo() {$text}").bodyBlockExpression!!.statements.first() as KtDestructuringDeclaration
}
fun createDestructuringParameter(text: String): KtParameter {
@@ -337,7 +337,7 @@ class KtPsiFactory @JvmOverloads constructor(private val project: Project, val m
}
fun createEmptyBody(): KtBlockExpression {
return createFunction("fun foo() {}").bodyExpression as KtBlockExpression
return createFunction("fun foo() {}").bodyBlockExpression!!
}
fun createAnonymousInitializer(): KtAnonymousInitializer {
@@ -821,13 +821,13 @@ class KtPsiFactory @JvmOverloads constructor(private val project: Project, val m
}
fun createBlock(bodyText: String): KtBlockExpression {
return createFunction("fun foo() {\n$bodyText\n}").bodyExpression as KtBlockExpression
return createFunction("fun foo() {\n$bodyText\n}").bodyBlockExpression!!
}
fun createSingleStatementBlock(statement: KtExpression, prevComment: String? = null, nextComment: String? = null): KtBlockExpression {
val prev = if (prevComment == null) "" else " $prevComment "
val next = if (nextComment == null) "" else " $nextComment "
return createDeclarationByPattern<KtNamedFunction>("fun foo() {\n$prev$0$next\n}", statement).bodyExpression as KtBlockExpression
return createDeclarationByPattern<KtNamedFunction>("fun foo() {\n$prev$0$next\n}", statement).bodyBlockExpression!!
}
fun createComment(text: String): PsiComment {
@@ -35,7 +35,6 @@ import org.jetbrains.kotlin.name.SpecialNames
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.stubs.KotlinClassOrObjectStub
import org.jetbrains.kotlin.types.expressions.OperatorConventions
import java.lang.IllegalArgumentException
import java.util.*
// NOTE: in this file we collect only Kotlin-specific methods working with PSI and not modifying it
@@ -300,7 +299,7 @@ fun KtNamedFunction.isContractPresentPsiCheck(): Boolean {
!hasModifier(KtTokens.OPERATOR_KEYWORD)
if (!contractAllowedHere) return false
val firstExpression = ((this as? KtFunction)?.bodyExpression as? KtBlockExpression)?.statements?.firstOrNull() ?: return false
val firstExpression = (this as? KtFunction)?.bodyBlockExpression?.statements?.firstOrNull() ?: return false
return firstExpression.isContractDescriptionCallPsiCheck()
}