Create isCallee utility function

This commit is contained in:
Nikolay Krasko
2017-07-26 15:26:13 +03:00
parent 142a64e039
commit 1b93e2030f
4 changed files with 84 additions and 3 deletions
@@ -357,6 +357,33 @@ fun KtStringTemplateExpression.getContentRange(): TextRange {
return TextRange(start, if (lastChild.elementType == KtTokens.CLOSING_QUOTE) length - lastChild.textLength else length)
}
/**
* Check expression might be a callee of call with the same name.
* Note that 'this' in 'this(args)' isn't considered to be a callee, also 'name' is not a callee in 'name++'.
*/
fun KtSimpleNameExpression.isCallee(): Boolean {
val parent = parent
return when (parent) {
is KtCallElement -> parent.calleeExpression == this
is KtBinaryExpression -> parent.operationReference == this
else -> {
val callElement =
getStrictParentOfType<KtUserType>()
?.getStrictParentOfType<KtTypeReference>()
?.getStrictParentOfType<KtConstructorCalleeExpression>()
?.getStrictParentOfType<KtCallElement>()
if (callElement != null) {
val ktConstructorCalleeExpression = callElement.calleeExpression as? KtConstructorCalleeExpression
(ktConstructorCalleeExpression?.typeReference?.typeElement as? KtUserType)?.referenceExpression == this
}
else {
false
}
}
}
}
val KtStringTemplateExpression.plainContent: String
get() = getContentRange().substring(text)