@@ -0,0 +1,2466 @@
+/*%0A * Copyright (C) 2016 Reece H. Dunn%0A *%0A * Licensed under the Apache License, Version 2.0 (the %22License%22);%0A * you may not use this file except in compliance with the License.%0A * You may obtain a copy of the License at%0A *%0A * http://www.apache.org/licenses/LICENSE-2.0%0A *%0A * Unless required by applicable law or agreed to in writing, software%0A * distributed under the License is distributed on an %22AS IS%22 BASIS,%0A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.%0A * See the License for the specific language governing permissions and%0A * limitations under the License.%0A */%0Apackage uk.co.reecedunn.intellij.plugin.xquery.functional;%0A%0Aimport com.intellij.psi.PsiElement;%0A%0Aimport java.util.function.Consumer;%0Aimport java.util.function.Function;%0Aimport java.util.function.Predicate;%0A%0Apublic final class PsiTreeWalker %7B%0A    public void each(Consumer%3CPsiElement%3E consumer) %7B%0A        PsiElement element = mElement;%0A        while (element != null) %7B%0A            consumer.accept(element);%0A            element = mNextElementFunction.apply(element);%0A        %7D%0A    %7D%0A%0A    public Option%3CPsiElement%3E find(Predicate%3CPsiElement%3E matcher) %7B%0A        PsiElement element = mElement;%0A        while (element != null) %7B%0A            if (matcher.test(element)) %7B%0A                return Option.some(element);%0A            %7D%0A            element = mNextElementFunction.apply(element);%0A        %7D%0A        return Option.none();%0A    %7D%0A%0A    // region Value Constructors%0A%0A    public static PsiTreeWalker ancestors(PsiElement element) %7B%0A        return new PsiTreeWalker(element.getParent(), PsiElement::getParent);%0A    %7D%0A%0A    public static PsiTreeWalker descendants(PsiElement element) %7B%0A        return new PsiTreeWalker(element.getFirstChild(), PsiElement::getFirstChild);%0A    %7D%0A%0A    public static PsiTreeWalker children(PsiElement element) %7B%0A        return new PsiTreeWalker(element.getFirstChild(), PsiElement::getNextSibling);%0A    %7D%0A%0A    public static PsiTreeWalker reversedChildren(PsiElement element) %7B%0A        return new PsiTreeWalker(element.getLastChild(), PsiElement::getPrevSibling);%0A    %7D%0A%0A    // endregion%0A    // region Implementation Details%0A%0A    private final PsiElement mElement;%0A    private final Function%3CPsiElement, PsiElement%3E mNextElementFunction;%0A%0A    private PsiTreeWalker(PsiElement element, Function%3CPsiElement, PsiElement%3E nextElementFunction) %7B%0A        mElement = element;%0A        mNextElementFunction = nextElementFunction;%0A    %7D%0A%0A    // endregion%0A%7D%0A

Create a PsiTreeWalker helper class as a replacement for the PsiNavigation utilities.