add method to PsiTreeUtil

This commit is contained in:
Bas Leijdekkers
2016-04-08 10:59:05 +02:00
parent 3c6b40759b
commit fc585ef433
2 changed files with 18 additions and 8 deletions
@@ -63,13 +63,8 @@ public class JavaMoveLeftRightHandler extends MoveElementLeftRightHandler {
return ((PsiTypeParameterList)element).getTypeParameters();
}
else if (element instanceof PsiModifierList) {
final List<PsiElement> result = ContainerUtil.newSmartList();
for (PsiElement child = element.getFirstChild(); child != null; child = child.getNextSibling()) {
if (child instanceof PsiKeyword || child instanceof PsiAnnotation) {
result.add(child);
}
}
return result.toArray(PsiElement.EMPTY_ARRAY);
final List<PsiElement> elements = PsiTreeUtil.getChildrenOfAnyType(element, PsiKeyword.class, PsiAnnotation.class);
return elements.toArray(PsiElement.EMPTY_ARRAY);
}
return PsiElement.EMPTY_ARRAY;
}
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2015 JetBrains s.r.o.
* Copyright 2000-2016 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.
@@ -346,6 +346,21 @@ public class PsiTreeUtil {
return result == null ? null : ArrayUtil.toObjectArray(result, aClass);
}
@Nullable
public static <T extends PsiElement> List<T> getChildrenOfAnyType(@Nullable PsiElement element, @NotNull Class<? extends T>... classes) {
if (element == null) return ContainerUtil.emptyList();
List<T> result = null;
for (PsiElement child = element.getFirstChild(); child != null; child = child.getNextSibling()) {
if (instanceOf(child, classes)) {
if (result == null) result = ContainerUtil.newSmartList();
//noinspection unchecked
result.add((T)child);
}
}
return result;
}
@NotNull
public static <T extends PsiElement> List<T> getChildrenOfTypeAsList(@Nullable PsiElement element, @NotNull Class<T> aClass) {
if (element == null) return Collections.emptyList();