Translate utility method getParentByTypeAndPredicate() to Kotlin

This commit is contained in:
Alexey Sedunov
2013-10-17 15:52:12 +04:00
parent dd50438c78
commit 2add0b0810
15 changed files with 88 additions and 66 deletions
@@ -750,27 +750,6 @@ public class JetPsiUtil {
return checkElement.apply(lastElement) ? lastElement : null;
}
@Nullable
public static PsiElement getParentByTypeAndPredicate(
@Nullable PsiElement element, @NotNull Class<? extends PsiElement> aClass, @NotNull Predicate<PsiElement> predicate, boolean strict) {
if (element == null) return null;
if (strict) {
element = element.getParent();
}
while (element != null) {
//noinspection unchecked
if (aClass.isInstance(element) && predicate.apply(element)) {
//noinspection unchecked
return element;
}
if (element instanceof PsiFile) return null;
element = element.getParent();
}
return null;
}
public static boolean checkVariableDeclarationInBlock(@NotNull JetBlockExpression block, @NotNull String varName) {
for (JetElement element : block.getStatements()) {
if (element instanceof JetVariableDeclaration) {
@@ -0,0 +1,39 @@
/*
* Copyright 2010-2013 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.jet.lang.psi.psiUtil
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiFile
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
}
@@ -16,25 +16,26 @@
package org.jetbrains.jet.plugin.findUsages;
import com.google.common.base.Predicate;
import com.intellij.psi.PsiElement;
import com.intellij.psi.tree.IElementType;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.usages.UsageTarget;
import com.intellij.usages.impl.rules.UsageType;
import com.intellij.usages.impl.rules.UsageTypeProviderEx;
import jet.Function1;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.descriptors.*;
import org.jetbrains.jet.lang.psi.*;
import org.jetbrains.jet.lang.psi.psiUtil.PsiUtilPackage;
import org.jetbrains.jet.lang.resolve.BindingContext;
import org.jetbrains.jet.lexer.JetTokens;
import org.jetbrains.jet.plugin.project.AnalyzerFacadeWithCache;
public class JetUsageTypeProvider implements UsageTypeProviderEx {
private static final Predicate<PsiElement> IS_ASSIGNMENT = new Predicate<PsiElement>() {
private static final Function1<PsiElement, Boolean> IS_ASSIGNMENT = new Function1<PsiElement, Boolean>() {
@Override
public boolean apply(@Nullable PsiElement input) {
public Boolean invoke(@Nullable PsiElement input) {
return input != null && JetPsiUtil.isAssignment(input);
}
};
@@ -203,7 +204,7 @@ public class JetUsageTypeProvider implements UsageTypeProviderEx {
}
JetBinaryExpression binaryExpression =
(JetBinaryExpression) JetPsiUtil.getParentByTypeAndPredicate(element, JetBinaryExpression.class, IS_ASSIGNMENT, false);
PsiUtilPackage.getParentByTypeAndPredicate(element, JetBinaryExpression.class, false, IS_ASSIGNMENT);
if (binaryExpression != null && PsiTreeUtil.isAncestor(binaryExpression.getLeft(), element, false)) {
return UsageType.WRITE;
}
@@ -1,17 +1,18 @@
package org.jetbrains.jet.plugin.hierarchy;
import com.google.common.base.Predicate;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiMethod;
import jet.Function1;
import org.jetbrains.jet.lang.psi.*;
import org.jetbrains.jet.lang.psi.psiUtil.PsiUtilPackage;
import javax.annotation.Nullable;
public class HierarchyUtils {
public static final Predicate<PsiElement> IS_CALL_HIERARCHY_ELEMENT = new Predicate<PsiElement>() {
public static final Function1<PsiElement, Boolean> IS_CALL_HIERARCHY_ELEMENT = new Function1<PsiElement, Boolean>() {
@Override
public boolean apply(@Nullable PsiElement input) {
public Boolean invoke(@Nullable PsiElement input) {
return input instanceof PsiMethod ||
input instanceof PsiClass ||
input instanceof JetFile ||
@@ -22,6 +23,6 @@ public class HierarchyUtils {
};
public static PsiElement getCallHierarchyElement(PsiElement element) {
return JetPsiUtil.getParentByTypeAndPredicate(element, PsiElement.class, IS_CALL_HIERARCHY_ELEMENT, false);
return PsiUtilPackage.getParentByTypeAndPredicate(element, PsiElement.class, false, IS_CALL_HIERARCHY_ELEMENT);
}
}
@@ -1,6 +1,5 @@
package org.jetbrains.jet.plugin.hierarchy.calls;
import com.google.common.base.Predicate;
import com.intellij.ide.hierarchy.HierarchyNodeDescriptor;
import com.intellij.ide.hierarchy.HierarchyTreeStructure;
import com.intellij.ide.hierarchy.call.CallHierarchyNodeDescriptor;
@@ -9,10 +8,12 @@ import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiMethod;
import com.intellij.util.containers.HashMap;
import jet.Function1;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.asJava.LightClassUtil;
import org.jetbrains.jet.lang.psi.*;
import org.jetbrains.jet.lang.psi.psiUtil.PsiUtilPackage;
import java.util.List;
import java.util.Map;
@@ -44,9 +45,9 @@ public abstract class KotlinCallTreeStructure extends HierarchyTreeStructure {
: ((KotlinCallHierarchyNodeDescriptor)descriptor).getTargetElement();
}
private static final Predicate<PsiElement> IS_NON_LOCAL_DECLARATION = new Predicate<PsiElement>() {
private static final Function1<PsiElement, Boolean> IS_NON_LOCAL_DECLARATION = new Function1<PsiElement, Boolean>() {
@Override
public boolean apply(@javax.annotation.Nullable PsiElement input) {
public Boolean invoke(@javax.annotation.Nullable PsiElement input) {
return input instanceof PsiMethod
|| ((input instanceof JetNamedFunction || input instanceof JetClassOrObject || input instanceof JetProperty)
&& !JetPsiUtil.isLocal((JetNamedDeclaration) input));
@@ -56,7 +57,7 @@ public abstract class KotlinCallTreeStructure extends HierarchyTreeStructure {
@Nullable
protected static PsiMethod getRepresentativePsiMethod(PsiElement element) {
while (true) {
element = JetPsiUtil.getParentByTypeAndPredicate(element, PsiElement.class, IS_NON_LOCAL_DECLARATION, false);
element = PsiUtilPackage.getParentByTypeAndPredicate(element, PsiElement.class, false, IS_NON_LOCAL_DECLARATION);
if (element == null) return null;
PsiMethod method = getRepresentativePsiMethodForNonLocalDeclaration(element);
@@ -16,25 +16,25 @@
package org.jetbrains.jet.plugin.intentions;
import com.google.common.base.Predicate;
import com.intellij.codeInsight.intention.impl.BaseIntentionAction;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.util.IncorrectOperationException;
import jet.Function1;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.psi.JetElement;
import org.jetbrains.jet.lang.psi.JetFile;
import org.jetbrains.jet.lang.psi.JetPsiUtil;
import org.jetbrains.jet.lang.psi.psiUtil.PsiUtilPackage;
import org.jetbrains.jet.plugin.JetBundle;
public abstract class AbstractCodeTransformationIntention extends BaseIntentionAction {
private final Transformer transformer;
private final Predicate<PsiElement> isApplicable;
private final Function1<PsiElement, Boolean> isApplicable;
protected AbstractCodeTransformationIntention(@NotNull Transformer transformer, @NotNull Predicate<PsiElement> isApplicable) {
protected AbstractCodeTransformationIntention(@NotNull Transformer transformer, @NotNull Function1<PsiElement, Boolean> isApplicable) {
this.transformer = transformer;
this.isApplicable = isApplicable;
setText(JetBundle.message(transformer.getKey()));
@@ -43,7 +43,7 @@ public abstract class AbstractCodeTransformationIntention extends BaseIntentionA
@Nullable
private PsiElement getTarget(@NotNull Editor editor, @NotNull PsiFile file) {
PsiElement element = file.findElementAt(editor.getCaretModel().getOffset());
return JetPsiUtil.getParentByTypeAndPredicate(element, JetElement.class, isApplicable, false);
return PsiUtilPackage.getParentByTypeAndPredicate(element, JetElement.class, false, isApplicable);
}
@NotNull
@@ -1,8 +1,8 @@
package org.jetbrains.jet.plugin.intentions.branchedTransformations.intentions;
import com.google.common.base.Predicate;
import com.intellij.openapi.editor.Editor;
import com.intellij.psi.PsiElement;
import jet.Function1;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.psi.JetFile;
@@ -25,9 +25,9 @@ public class EliminateWhenSubjectIntention extends AbstractCodeTransformationInt
}
};
private static final Predicate<PsiElement> IS_APPLICABLE = new Predicate<PsiElement>() {
private static final Function1<PsiElement, Boolean> IS_APPLICABLE = new Function1<PsiElement, Boolean>() {
@Override
public boolean apply(@Nullable PsiElement input) {
public Boolean invoke(@Nullable PsiElement input) {
return input instanceof JetWhenExpression && WhenUtils.checkEliminateWhenSubject((JetWhenExpression) input);
}
};
@@ -1,8 +1,8 @@
package org.jetbrains.jet.plugin.intentions.branchedTransformations.intentions;
import com.google.common.base.Predicate;
import com.intellij.openapi.editor.Editor;
import com.intellij.psi.PsiElement;
import jet.Function1;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.psi.JetFile;
@@ -25,9 +25,9 @@ public class FlattenWhenIntention extends AbstractCodeTransformationIntention {
}
};
private static final Predicate<PsiElement> IS_APPLICABLE = new Predicate<PsiElement>() {
private static final Function1<PsiElement, Boolean> IS_APPLICABLE = new Function1<PsiElement, Boolean>() {
@Override
public boolean apply(@Nullable PsiElement input) {
public Boolean invoke(@Nullable PsiElement input) {
return input instanceof JetWhenExpression && WhenUtils.checkFlattenWhen((JetWhenExpression) input);
}
};
@@ -16,8 +16,8 @@
package org.jetbrains.jet.plugin.intentions.branchedTransformations.intentions;
import com.google.common.base.Predicate;
import com.intellij.psi.PsiElement;
import jet.Function1;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.psi.JetExpression;
@@ -29,9 +29,9 @@ public abstract class FoldBranchedExpressionIntention extends AbstractCodeTransf
protected FoldBranchedExpressionIntention(@NotNull final FoldableKind foldableKind) {
super(
foldableKind,
new Predicate<PsiElement>() {
new Function1<PsiElement, Boolean>() {
@Override
public boolean apply(@Nullable PsiElement input) {
public Boolean invoke(@Nullable PsiElement input) {
return (input instanceof JetExpression) && BranchedFoldingUtils.getFoldableExpressionKind((JetExpression) input) == foldableKind;
}
}
@@ -1,8 +1,8 @@
package org.jetbrains.jet.plugin.intentions.branchedTransformations.intentions;
import com.google.common.base.Predicate;
import com.intellij.openapi.editor.Editor;
import com.intellij.psi.PsiElement;
import jet.Function1;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.psi.JetFile;
@@ -25,9 +25,9 @@ public class IfToWhenIntention extends AbstractCodeTransformationIntention {
}
};
private static final Predicate<PsiElement> IS_APPLICABLE = new Predicate<PsiElement>() {
private static final Function1<PsiElement, Boolean> IS_APPLICABLE = new Function1<PsiElement, Boolean>() {
@Override
public boolean apply(@Nullable PsiElement input) {
public Boolean invoke(@Nullable PsiElement input) {
return (input instanceof JetIfExpression) && IfWhenUtils.checkIfToWhen((JetIfExpression) input);
}
};
@@ -1,8 +1,8 @@
package org.jetbrains.jet.plugin.intentions.branchedTransformations.intentions;
import com.google.common.base.Predicate;
import com.intellij.openapi.editor.Editor;
import com.intellij.psi.PsiElement;
import jet.Function1;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.psi.JetFile;
@@ -25,9 +25,9 @@ public class IntroduceWhenSubjectIntention extends AbstractCodeTransformationInt
}
};
private static final Predicate<PsiElement> IS_APPLICABLE = new Predicate<PsiElement>() {
private static final Function1<PsiElement, Boolean> IS_APPLICABLE = new Function1<PsiElement, Boolean>() {
@Override
public boolean apply(@Nullable PsiElement input) {
public Boolean invoke(@Nullable PsiElement input) {
return input instanceof JetWhenExpression && WhenUtils.checkIntroduceWhenSubject((JetWhenExpression) input);
}
};
@@ -16,8 +16,8 @@
package org.jetbrains.jet.plugin.intentions.branchedTransformations.intentions;
import com.google.common.base.Predicate;
import com.intellij.psi.PsiElement;
import jet.Function1;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.psi.JetExpression;
@@ -28,9 +28,9 @@ public abstract class UnfoldBranchedExpressionIntention extends AbstractCodeTran
protected UnfoldBranchedExpressionIntention(@NotNull final UnfoldableKind unfoldableKind) {
super(
unfoldableKind,
new Predicate<PsiElement>() {
new Function1<PsiElement, Boolean>() {
@Override
public boolean apply(@Nullable PsiElement input) {
public Boolean invoke(@Nullable PsiElement input) {
return (input instanceof JetExpression) && BranchedUnfoldingUtils.getUnfoldableExpressionKind((JetExpression) input) == unfoldableKind;
}
}
@@ -16,9 +16,9 @@
package org.jetbrains.jet.plugin.intentions.branchedTransformations.intentions;
import com.google.common.base.Predicate;
import com.intellij.openapi.editor.Editor;
import com.intellij.psi.PsiElement;
import jet.Function1;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.psi.JetFile;
@@ -41,9 +41,9 @@ public class WhenToIfIntention extends AbstractCodeTransformationIntention {
}
};
private static final Predicate<PsiElement> IS_APPLICABLE = new Predicate<PsiElement>() {
private static final Function1<PsiElement, Boolean> IS_APPLICABLE = new Function1<PsiElement, Boolean>() {
@Override
public boolean apply(@Nullable PsiElement input) {
public Boolean invoke(@Nullable PsiElement input) {
return (input instanceof JetWhenExpression) && IfWhenUtils.checkWhenToIf((JetWhenExpression) input);
}
};
@@ -1,18 +1,19 @@
package org.jetbrains.jet.plugin.intentions.declarations;
import com.google.common.base.Predicate;
import com.intellij.codeInsight.editorActions.JoinRawLinesHandlerDelegate;
import com.intellij.openapi.editor.Document;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
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;
public class JetDeclarationJoinLinesHandler implements JoinRawLinesHandlerDelegate {
private static final Predicate<PsiElement> IS_APPLICABLE = new Predicate<PsiElement>() {
private static final Function1<PsiElement, Boolean> IS_APPLICABLE = new Function1<PsiElement, Boolean>() {
@Override
public boolean apply(@Nullable PsiElement input) {
public Boolean invoke(@Nullable PsiElement input) {
return input != null && DeclarationUtils.checkAndGetPropertyAndInitializer(input) != null;
}
};
@@ -21,7 +22,7 @@ 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 = JetPsiUtil.getParentByTypeAndPredicate(element, JetElement.class, IS_APPLICABLE, false);
PsiElement target = PsiUtilPackage.getParentByTypeAndPredicate(element, JetElement.class, false, IS_APPLICABLE);
if (target == null) return -1;
return DeclarationUtils.joinPropertyDeclarationWithInitializer(target).getTextRange().getStartOffset();
@@ -16,9 +16,9 @@
package org.jetbrains.jet.plugin.intentions.declarations;
import com.google.common.base.Predicate;
import com.intellij.openapi.editor.Editor;
import com.intellij.psi.PsiElement;
import jet.Function1;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.psi.JetFile;
@@ -40,9 +40,9 @@ public class SplitPropertyDeclarationIntention extends AbstractCodeTransformatio
}
};
private static final Predicate<PsiElement> IS_APPLICABLE = new Predicate<PsiElement>() {
private static final Function1<PsiElement, Boolean> IS_APPLICABLE = new Function1<PsiElement, Boolean>() {
@Override
public boolean apply(@Nullable PsiElement input) {
public Boolean invoke(@Nullable PsiElement input) {
return (input instanceof JetProperty) && DeclarationUtils.checkSplitProperty((JetProperty) input);
}
};