Convert to Kotlin: HierarchyUtils

This commit is contained in:
Alexey Sedunov
2016-08-18 20:59:32 +03:00
parent 91cc22e599
commit 4622f08b5e
4 changed files with 38 additions and 41 deletions
@@ -14,53 +14,50 @@
* limitations under the License. * limitations under the License.
*/ */
package org.jetbrains.kotlin.idea.hierarchy; package org.jetbrains.kotlin.idea.hierarchy
import com.intellij.codeInsight.TargetElementUtilBase; import com.intellij.codeInsight.TargetElementUtilBase
import com.intellij.openapi.actionSystem.CommonDataKeys; import com.intellij.openapi.actionSystem.CommonDataKeys
import com.intellij.openapi.actionSystem.DataContext; import com.intellij.openapi.actionSystem.DataContext
import com.intellij.openapi.editor.Editor; import com.intellij.openapi.project.Project
import com.intellij.openapi.project.Project; import com.intellij.psi.PsiClass
import com.intellij.psi.*; import com.intellij.psi.PsiDocumentManager
import com.intellij.util.ArrayUtil; import com.intellij.psi.PsiElement
import kotlin.jvm.functions.Function1; import com.intellij.psi.PsiMethod
import org.jetbrains.annotations.Nullable; import com.intellij.util.ArrayUtil
import org.jetbrains.kotlin.idea.util.ProjectRootsUtil; import org.jetbrains.kotlin.idea.util.ProjectRootsUtil
import org.jetbrains.kotlin.psi.*; import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.PsiUtilsKt; import org.jetbrains.kotlin.psi.psiUtil.getParentOfTypesAndPredicate
public class HierarchyUtils { object HierarchyUtils {
public static final Function1<PsiElement, Boolean> IS_CALL_HIERARCHY_ELEMENT = new Function1<PsiElement, Boolean>() { val IS_CALL_HIERARCHY_ELEMENT: Function1<PsiElement, Boolean> = fun(input: PsiElement?): Boolean {
@Override return input is PsiMethod ||
public Boolean invoke(@Nullable PsiElement input) { input is PsiClass ||
return input instanceof PsiMethod || input is KtFile ||
input instanceof PsiClass || input is KtNamedFunction ||
input instanceof KtFile || input is KtSecondaryConstructor ||
input instanceof KtNamedFunction || input is KtObjectDeclaration ||
input instanceof KtSecondaryConstructor || input is KtClass && !input.isInterface() ||
input instanceof KtObjectDeclaration || input is KtProperty
(input instanceof KtClass && !((KtClass) input).isInterface()) || }
input instanceof KtProperty;
}
};
public static PsiElement getCurrentElement(DataContext dataContext, Project project) { fun getCurrentElement(dataContext: DataContext, project: Project): PsiElement? {
Editor editor = CommonDataKeys.EDITOR.getData(dataContext); val editor = CommonDataKeys.EDITOR.getData(dataContext)
if (editor != null) { if (editor != null) {
PsiFile file = PsiDocumentManager.getInstance(project).getPsiFile(editor.getDocument()); val file = PsiDocumentManager.getInstance(project).getPsiFile(editor.document) ?: return null
if (file == null) return null;
if (!ProjectRootsUtil.isInProjectOrLibSource(file)) return null; if (!ProjectRootsUtil.isInProjectOrLibSource(file)) return null
return TargetElementUtilBase.findTargetElement(editor, TargetElementUtilBase.getInstance().getAllAccepted()); return TargetElementUtilBase.findTargetElement(editor, TargetElementUtilBase.getInstance().allAccepted)
} }
return CommonDataKeys.PSI_ELEMENT.getData(dataContext); return CommonDataKeys.PSI_ELEMENT.getData(dataContext)
} }
public static PsiElement getCallHierarchyElement(PsiElement element) { fun getCallHierarchyElement(element: PsiElement): PsiElement? {
//noinspection unchecked //noinspection unchecked
return PsiUtilsKt.getParentOfTypesAndPredicate(element, false, ArrayUtil.EMPTY_CLASS_ARRAY, IS_CALL_HIERARCHY_ELEMENT); return element.getParentOfTypesAndPredicate<PsiElement>(strict = false,
parentClasses = *(ArrayUtil.EMPTY_CLASS_ARRAY as Array<Class<PsiElement>>),
predicate = IS_CALL_HIERARCHY_ELEMENT)
} }
} }
@@ -76,7 +76,7 @@ public class KotlinCallHierarchyBrowser extends CallHierarchyBrowserBase {
@Override @Override
protected boolean isApplicableElement(@NotNull PsiElement element) { protected boolean isApplicableElement(@NotNull PsiElement element) {
if (element instanceof PsiClass) return false; // PsiClass is not allowed at the hierarchy root if (element instanceof PsiClass) return false; // PsiClass is not allowed at the hierarchy root
return HierarchyUtils.IS_CALL_HIERARCHY_ELEMENT.invoke(element); return HierarchyUtils.INSTANCE.getIS_CALL_HIERARCHY_ELEMENT().invoke(element);
} }
@Override @Override
@@ -42,7 +42,7 @@ public class KotlinCallHierarchyProvider implements HierarchyProvider {
PsiElement element = getCurrentElement(dataContext, project); PsiElement element = getCurrentElement(dataContext, project);
if (element == null) return null; if (element == null) return null;
element = HierarchyUtils.getCallHierarchyElement(element); element = HierarchyUtils.INSTANCE.getCallHierarchyElement(element);
if (element instanceof KtFile || element == null || element.getLanguage() != KotlinLanguage.INSTANCE) return null; if (element instanceof KtFile || element == null || element.getLanguage() != KotlinLanguage.INSTANCE) return null;
return element; return element;
@@ -234,7 +234,7 @@ public class KotlinCallerMethodsTreeStructure extends KotlinCallTreeStructure {
PsiElement refElement = ref.getElement(); PsiElement refElement = ref.getElement();
if (PsiTreeUtil.getParentOfType(refElement, KtImportDirective.class, true) != null) return true; if (PsiTreeUtil.getParentOfType(refElement, KtImportDirective.class, true) != null) return true;
PsiElement element = HierarchyUtils.getCallHierarchyElement(refElement); PsiElement element = HierarchyUtils.INSTANCE.getCallHierarchyElement(refElement);
if (kotlinOnly && !(element instanceof KtNamedDeclaration)) return true; if (kotlinOnly && !(element instanceof KtNamedDeclaration)) return true;
@@ -242,7 +242,7 @@ public class KotlinCallerMethodsTreeStructure extends KotlinCallTreeStructure {
if (element instanceof KtProperty) { if (element instanceof KtProperty) {
KtProperty property = (KtProperty) element; KtProperty property = (KtProperty) element;
if (PsiTreeUtil.isAncestor(property.getInitializer(), refElement, false)) { if (PsiTreeUtil.isAncestor(property.getInitializer(), refElement, false)) {
element = HierarchyUtils.getCallHierarchyElement(element.getParent()); element = HierarchyUtils.INSTANCE.getCallHierarchyElement(element.getParent());
} }
} }