@Nullable public static PsiElement skipSiblingsBackwardByPredicate( @Nullable PsiElement element, Predicate<PsiElement> elementsToSkip) { if (element == null) return null; for (PsiElement e = element.getPrevSibling(); e != null; e = e.getPrevSibling()) { if (elementsToSkip.apply(e)) continue; return e; } return null; }
public static String getPrevSiblingAsTextUntil( PsiElement psiElement, ElementPattern pattern, boolean includeMatching) { String prevText = ""; for (PsiElement child = psiElement.getPrevSibling(); child != null; child = child.getPrevSibling()) { if (pattern.accepts(child)) { if (includeMatching) { return child.getText() + prevText; } return prevText; } else { prevText = child.getText() + prevText; } } return prevText; }
// Delete given element and all the elements separating it from the neighboring elements of the // same class public static void deleteElementWithDelimiters(@NotNull PsiElement element) { PsiElement paramBefore = PsiTreeUtil.getPrevSiblingOfType(element, element.getClass()); PsiElement from; PsiElement to; if (paramBefore != null) { from = paramBefore.getNextSibling(); to = element; } else { PsiElement paramAfter = PsiTreeUtil.getNextSiblingOfType(element, element.getClass()); from = element; to = paramAfter != null ? paramAfter.getPrevSibling() : element; } PsiElement parent = element.getParent(); parent.deleteChildRange(from, to); }