Replace getParentByTypePredicate with getParentByTypesAndPredicate
This commit is contained in:
@@ -38,25 +38,6 @@ import kotlin.test.assertTrue
|
||||
import com.intellij.psi.search.SearchScope
|
||||
import com.intellij.psi.search.PsiSearchScopeUtil
|
||||
|
||||
fun PsiElement.getParentByTypeAndPredicate<T: PsiElement>(
|
||||
parentClass : Class<T>, strict : Boolean = false, predicate: (T) -> Boolean
|
||||
) : T? {
|
||||
var element = if (strict) getParent() else this
|
||||
while (element != null) {
|
||||
[suppress("UNCHECKED_CAST")]
|
||||
when {
|
||||
parentClass.isInstance(element) && predicate(element as T) ->
|
||||
return element as T
|
||||
element is PsiFile ->
|
||||
return null
|
||||
else ->
|
||||
element = element?.getParent()
|
||||
}
|
||||
}
|
||||
|
||||
return null
|
||||
}
|
||||
|
||||
fun PsiElement.getParentByTypesAndPredicate<T: PsiElement>(
|
||||
strict : Boolean = false, vararg parentClasses : Class<T>, predicate: (T) -> Boolean
|
||||
) : T? {
|
||||
@@ -64,7 +45,7 @@ fun PsiElement.getParentByTypesAndPredicate<T: PsiElement>(
|
||||
while (element != null) {
|
||||
[suppress("UNCHECKED_CAST")]
|
||||
when {
|
||||
parentClasses.any {parentClass -> parentClass.isInstance(element)} && predicate(element!! as T) ->
|
||||
(parentClasses.isEmpty() || parentClasses.any {parentClass -> parentClass.isInstance(element)}) && predicate(element!! as T) ->
|
||||
return element as T
|
||||
element is PsiFile ->
|
||||
return null
|
||||
|
||||
@@ -137,9 +137,8 @@ public object JetUsageTypeProvider : UsageTypeProviderEx {
|
||||
}
|
||||
|
||||
return when {
|
||||
simpleName.getParentByTypeAndPredicate(
|
||||
javaClass<JetBinaryExpression>(), false, { JetPsiUtil.isAssignment(it) }
|
||||
)?.getLeft().isAncestor(simpleName) ->
|
||||
(simpleName.getParentByTypesAndPredicate(false, javaClass<JetBinaryExpression>()) { JetPsiUtil.isAssignment(it) })
|
||||
?.getLeft().isAncestor(simpleName) ->
|
||||
UsageType.WRITE
|
||||
|
||||
simpleName.getParentByType(javaClass<JetSimpleNameExpression>()) != null ->
|
||||
|
||||
@@ -3,6 +3,7 @@ package org.jetbrains.jet.plugin.hierarchy;
|
||||
import com.intellij.psi.PsiClass;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.PsiMethod;
|
||||
import com.intellij.util.ArrayUtil;
|
||||
import jet.Function1;
|
||||
import org.jetbrains.jet.lang.psi.*;
|
||||
import org.jetbrains.jet.lang.psi.psiUtil.PsiUtilPackage;
|
||||
@@ -23,6 +24,7 @@ public class HierarchyUtils {
|
||||
};
|
||||
|
||||
public static PsiElement getCallHierarchyElement(PsiElement element) {
|
||||
return PsiUtilPackage.getParentByTypeAndPredicate(element, PsiElement.class, false, IS_CALL_HIERARCHY_ELEMENT);
|
||||
//noinspection unchecked
|
||||
return PsiUtilPackage.getParentByTypesAndPredicate(element, false, ArrayUtil.EMPTY_CLASS_ARRAY, IS_CALL_HIERARCHY_ELEMENT);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ import com.intellij.openapi.project.Project;
|
||||
import com.intellij.psi.PsiClass;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.PsiMethod;
|
||||
import com.intellij.util.ArrayUtil;
|
||||
import com.intellij.util.containers.HashMap;
|
||||
import jet.Function1;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
@@ -57,7 +58,7 @@ public abstract class KotlinCallTreeStructure extends HierarchyTreeStructure {
|
||||
@Nullable
|
||||
protected static PsiMethod getRepresentativePsiMethod(PsiElement element) {
|
||||
while (true) {
|
||||
element = PsiUtilPackage.getParentByTypeAndPredicate(element, PsiElement.class, false, IS_NON_LOCAL_DECLARATION);
|
||||
element = PsiUtilPackage.getParentByTypesAndPredicate(element, false, ArrayUtil.EMPTY_CLASS_ARRAY, IS_NON_LOCAL_DECLARATION);
|
||||
if (element == null) return null;
|
||||
|
||||
PsiMethod method = getRepresentativePsiMethodForNonLocalDeclaration(element);
|
||||
|
||||
@@ -27,7 +27,7 @@ import org.jetbrains.annotations.Nullable
|
||||
import org.jetbrains.jet.lang.psi.JetElement
|
||||
import org.jetbrains.jet.lang.psi.JetFile
|
||||
import org.jetbrains.jet.plugin.JetBundle
|
||||
import org.jetbrains.jet.lang.psi.psiUtil.getParentByTypeAndPredicate
|
||||
import org.jetbrains.jet.lang.psi.psiUtil.getParentByTypesAndPredicate
|
||||
|
||||
public abstract class JetSelfTargetingIntention<T: JetElement>(val key: String, val elementType: Class<T>) : BaseIntentionAction() {
|
||||
{
|
||||
@@ -39,7 +39,7 @@ public abstract class JetSelfTargetingIntention<T: JetElement>(val key: String,
|
||||
|
||||
private fun getTarget(editor: Editor, file: PsiFile): T? {
|
||||
val offset = editor.getCaretModel().getOffset()
|
||||
return file.findElementAt(offset)?.getParentByTypeAndPredicate(elementType) { element -> isApplicableTo(element) }
|
||||
return file.findElementAt(offset)?.getParentByTypesAndPredicate(false, elementType) { element -> isApplicableTo(element) }
|
||||
}
|
||||
|
||||
public override fun getFamilyName(): String {
|
||||
|
||||
+3
-2
@@ -4,9 +4,9 @@ import com.intellij.codeInsight.editorActions.JoinRawLinesHandlerDelegate;
|
||||
import com.intellij.openapi.editor.Document;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.PsiFile;
|
||||
import com.intellij.util.ArrayUtil;
|
||||
import jet.Function1;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.jet.lang.psi.JetElement;
|
||||
import org.jetbrains.jet.lang.psi.JetPsiUtil;
|
||||
import org.jetbrains.jet.lang.psi.psiUtil.PsiUtilPackage;
|
||||
|
||||
@@ -22,7 +22,8 @@ public class JetDeclarationJoinLinesHandler implements JoinRawLinesHandlerDelega
|
||||
public int tryJoinRawLines(Document document, PsiFile file, int start, int end) {
|
||||
PsiElement element = JetPsiUtil.skipSiblingsBackwardByPredicate(file.findElementAt(start), DeclarationUtils.SKIP_DELIMITERS);
|
||||
|
||||
PsiElement target = PsiUtilPackage.getParentByTypeAndPredicate(element, JetElement.class, false, IS_APPLICABLE);
|
||||
//noinspection unchecked
|
||||
PsiElement target = PsiUtilPackage.getParentByTypesAndPredicate(element, false, ArrayUtil.EMPTY_CLASS_ARRAY, IS_APPLICABLE);
|
||||
if (target == null) return -1;
|
||||
|
||||
return DeclarationUtils.joinPropertyDeclarationWithInitializer(target).getTextRange().getStartOffset();
|
||||
|
||||
Reference in New Issue
Block a user