/*
 * Copyright (C) 2016 Reece H. Dunn
 *
 * 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 uk.co.reecedunn.intellij.plugin.xquery.functional;

import com.intellij.psi.PsiElement;

import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;

public final class PsiTreeWalker {
    public void each(Consumer<PsiElement> consumer) {
        PsiElement element = mElement;
        while (element != null) {
            consumer.accept(element);
            element = mNextElementFunction.apply(element);
        }
    }

    public Option<PsiElement> find(Predicate<PsiElement> matcher) {
        PsiElement element = mElement;
        while (element != null) {
            if (matcher.test(element)) {
                return Option.some(element);
            }
            element = mNextElementFunction.apply(element);
        }
        return Option.none();
    }

    // region Value Constructors

    public static PsiTreeWalker ancestors(PsiElement element) {
        return new PsiTreeWalker(element.getParent(), PsiElement::getParent);
    }

    public static PsiTreeWalker descendants(PsiElement element) {
        return new PsiTreeWalker(element.getFirstChild(), PsiElement::getFirstChild);
    }

    public static PsiTreeWalker children(PsiElement element) {
        return new PsiTreeWalker(element.getFirstChild(), PsiElement::getNextSibling);
    }

    public static PsiTreeWalker reversedChildren(PsiElement element) {
        return new PsiTreeWalker(element.getLastChild(), PsiElement::getPrevSibling);
    }

    // endregion
    // region Implementation Details

    private final PsiElement mElement;
    private final Function<PsiElement, PsiElement> mNextElementFunction;

    private PsiTreeWalker(PsiElement element, Function<PsiElement, PsiElement> nextElementFunction) {
        mElement = element;
        mNextElementFunction = nextElementFunction;
    }

    // endregion
}

@@ -1107,16 +1107,21 @@
 nt%3E find
+First
 (Predica
@@ -1421,24 +1421,358 @@
 e();%0A    %7D%0A%0A
+    public void findAll(Predicate%3CPsiElement%3E matcher, Consumer%3CPsiElement%3E consumer) %7B%0A        PsiElement element = mElement;%0A        while (element != null) %7B%0A            if (matcher.test(element)) %7B%0A                consumer.accept(element);%0A            %7D%0A            element = mNextElementFunction.apply(element);%0A        %7D%0A    %7D%0A%0A
     // regio

Split the PsiTreeWalker find function into findFirst and findAll.