Minor: rename and refactor contract-related PSI checks in ktPsiUtil

This commit is contained in:
Dmitry Savvinov
2018-07-19 19:06:21 +03:00
parent b943140f3a
commit 724b7bf363
3 changed files with 22 additions and 19 deletions
@@ -293,17 +293,20 @@ inline fun <reified T : KtElement, R> flatMapDescendantsOfTypeVisitor(
// ----------- Contracts -------------------------------------------------------------------------------------------------------------------
fun fastCheckIfContractPresent(element: KtElement): Boolean {
if (!isContractAllowedHere(element)) return false
val firstExpression = ((element as? KtFunction)?.bodyExpression as? KtBlockExpression)?.statements?.firstOrNull() ?: return false
return isContractDescriptionCallFastCheck(firstExpression)
fun KtElement.isContractPresentPsiCheck(): Boolean {
val contractAllowedHere = this is KtNamedFunction &&
isTopLevel &&
hasBlockBody() &&
!hasModifier(KtTokens.OPERATOR_KEYWORD)
if (!contractAllowedHere) return false
val firstExpression = ((this as? KtFunction)?.bodyExpression as? KtBlockExpression)?.statements?.firstOrNull() ?: return false
return firstExpression.isContractDescriptionCallPsiCheck()
}
private fun isContractAllowedHere(element: KtElement): Boolean =
element is KtNamedFunction && element.isTopLevel && element.hasBlockBody() && !element.hasModifier(KtTokens.OPERATOR_KEYWORD)
fun isContractDescriptionCallFastCheck(expression: KtExpression): Boolean =
expression is KtCallExpression && expression.calleeExpression?.text == "contract"
fun KtExpression.isContractDescriptionCallPsiCheck(): Boolean =
this is KtCallExpression && calleeExpression?.text == "contract"
// ----------- Other -----------------------------------------------------------------------------------------------------------------------