Extensions PsiElement.getParentOfType with reified T

1. Renamed *ByType -> *OfType (as in PsiTreeUtil.java)
2. Introduced PsiElement.getStrictParentOfType and PsiElement.getNonStrictParentOfType
with reified TP
3. Replaced current usages of PsiTreeUtil.getParentOfType and current
extensions
4. Made reified version of PsiElement.getParentOfTypeAndBranch with
default strict value --- false (as it used)
This commit is contained in:
Denis Zharkov
2014-12-01 10:58:42 +03:00
parent 8b80daa2a1
commit b3bf8933df
53 changed files with 204 additions and 176 deletions
@@ -32,8 +32,8 @@ import org.jetbrains.jet.lang.resolve.calls.model.ResolvedValueArgument
import org.jetbrains.jet.JetNodeTypes
import java.math.BigInteger
import org.jetbrains.jet.lang.diagnostics.Errors
import com.intellij.psi.util.PsiTreeUtil
import org.jetbrains.jet.lang.resolve.calls.callUtil.getResolvedCall
import org.jetbrains.jet.lang.psi.psiUtil.getStrictParentOfType
public class ConstantExpressionEvaluator private (val trace: BindingTrace) : JetVisitor<CompileTimeConstant<*>, JetType>() {
@@ -257,7 +257,7 @@ public class ConstantExpressionEvaluator private (val trace: BindingTrace) : Jet
assert (name == "minus", "Only negation should be checked for overflow")
if (receiver.value == result) {
trace.report(Errors.INTEGER_OVERFLOW.on(PsiTreeUtil.getParentOfType(callExpression, javaClass<JetExpression>()) ?: callExpression))
trace.report(Errors.INTEGER_OVERFLOW.on(callExpression.getStrictParentOfType<JetExpression>() ?: callExpression))
}
return result
}
@@ -282,7 +282,7 @@ public class ConstantExpressionEvaluator private (val trace: BindingTrace) : Jet
val resultInBigIntegers = checker(toBigInteger(receiver.value), toBigInteger(parameter.value))
if (toBigInteger(actualResult) != resultInBigIntegers) {
trace.report(Errors.INTEGER_OVERFLOW.on(PsiTreeUtil.getParentOfType(callExpression, javaClass<JetExpression>()) ?: callExpression))
trace.report(Errors.INTEGER_OVERFLOW.on(callExpression.getStrictParentOfType<JetExpression>() ?: callExpression))
}
return actualResult
}
@@ -50,7 +50,7 @@ public fun JetCallElement.getCallNameExpression(): JetSimpleNameExpression? {
}
}
public fun PsiElement.getParentByTypesAndPredicate<T: PsiElement>(
public fun PsiElement.getParentOfTypesAndPredicate<T: PsiElement>(
strict : Boolean = false, vararg parentClasses : Class<T>, predicate: (T) -> Boolean
) : T? {
var element = if (strict) getParent() else this
@@ -69,8 +69,20 @@ public fun PsiElement.getParentByTypesAndPredicate<T: PsiElement>(
return null
}
public fun PsiElement.getParentByType<T: PsiElement>(parentClass : Class<T>, strict : Boolean = false) : T? {
return PsiTreeUtil.getParentOfType(this, parentClass, strict)
public fun PsiElement.getNonStrictParentOfType<T: PsiElement>(parentClass : Class<T>) : T? {
return PsiTreeUtil.getParentOfType(this, parentClass, false)
}
inline public fun PsiElement.getParentOfType<reified T: PsiElement>(strict: Boolean): T? {
return PsiTreeUtil.getParentOfType(this, javaClass<T>(), strict)
}
inline public fun PsiElement.getStrictParentOfType<reified T: PsiElement>(): T? {
return PsiTreeUtil.getParentOfType(this, javaClass<T>(), true)
}
inline public fun PsiElement.getNonStrictParentOfType<reified T: PsiElement>(): T? {
return PsiTreeUtil.getParentOfType(this, javaClass<T>(), false)
}
public fun PsiElement?.isAncestor(element: PsiElement, strict: Boolean = false): Boolean {
@@ -81,9 +93,8 @@ public fun <T: PsiElement> T.getIfChildIsInBranch(element: PsiElement, branch: T
return if (branch().isAncestor(element)) this else null
}
public fun PsiElement.getParentByTypeAndBranch<T: PsiElement>(
parentClass : Class<T>, strict : Boolean = false, branch: T.() -> PsiElement?) : T? {
return getParentByType(parentClass, strict)?.getIfChildIsInBranch(this, branch)
inline public fun PsiElement.getParentOfTypeAndBranch<reified T: PsiElement>(strict: Boolean = false, noinline branch: T.() -> PsiElement?) : T? {
return getParentOfType<T>(strict)?.getIfChildIsInBranch(this, branch)
}
public fun JetClassOrObject.effectiveDeclarations(): List<JetDeclaration> =
@@ -192,7 +203,7 @@ public fun JetDeclaration.isOverridable(): Boolean {
public fun PsiElement.isExtensionDeclaration(): Boolean {
val callable: JetCallableDeclaration? = when (this) {
is JetNamedFunction, is JetProperty -> this as JetCallableDeclaration
is JetPropertyAccessor -> getParentByType(javaClass<JetProperty>())
is JetPropertyAccessor -> getNonStrictParentOfType<JetProperty>()
else -> null
}
@@ -22,7 +22,7 @@ import org.jetbrains.jet.lang.descriptors.FunctionDescriptor
import org.jetbrains.jet.lang.resolve.BindingContext.LABEL_TARGET
import org.jetbrains.jet.lang.resolve.BindingContext.FUNCTION
import org.jetbrains.jet.lang.resolve.BindingContext.DECLARATION_TO_DESCRIPTOR
import org.jetbrains.jet.lang.psi.psiUtil.getParentByType
import org.jetbrains.jet.lang.psi.psiUtil.getNonStrictParentOfType
import org.jetbrains.jet.lang.psi.JetDeclarationWithBody
import org.jetbrains.jet.lang.resolve.DescriptorUtils
import org.jetbrains.jet.lang.descriptors.impl.AnonymousFunctionDescriptor
@@ -34,7 +34,7 @@ public fun JetReturnExpression.getTargetFunctionDescriptor(context: BindingConte
val targetLabel = getTargetLabel()
if (targetLabel != null) return context[LABEL_TARGET, targetLabel]?.let { context[FUNCTION, it] }
val declarationDescriptor = context[DECLARATION_TO_DESCRIPTOR, getParentByType(javaClass<JetDeclarationWithBody>())]
val declarationDescriptor = context[DECLARATION_TO_DESCRIPTOR, getNonStrictParentOfType<JetDeclarationWithBody>()]
val containingFunctionDescriptor = DescriptorUtils.getParentOfType(declarationDescriptor, javaClass<FunctionDescriptor>(), false)
if (containingFunctionDescriptor == null) return null