package com.intellij.structuralsearch.impl.matcher;

import com.intellij.psi.PsiElement;
import com.intellij.structuralsearch.MatchOptions;
import com.intellij.structuralsearch.MatchResultSink;
import com.intellij.util.containers.Stack;

import java.util.Collection;
import java.util.List;

/**
 * Global context of matching process
 */
public class MatchContext {
  private MatchResultSink sink;
  private final Stack<MatchResultImpl> previousResults = new Stack<>();
  private MatchResultImpl result;
  private CompiledPattern pattern;
  private MatchOptions options;
  private GlobalMatchingVisitor matcher;
  private boolean shouldRecursivelyMatch = true;
  private boolean myWithAlternativePatternRoots = true;

  private List<PsiElement> myMatchedNodes;

  public List<PsiElement> getMatchedNodes() {
    return myMatchedNodes;
  }

  public void setMatchedNodes(final List<PsiElement> matchedNodes) {
    myMatchedNodes = matchedNodes;
  }

  public boolean isWithAlternativePatternRoots() {
    return myWithAlternativePatternRoots;
  }

  public void setWithAlternativePatternRoots(boolean withAlternativePatternRoots) {
    myWithAlternativePatternRoots = withAlternativePatternRoots;
  }

  public interface MatchedElementsListener {
    void matchedElements(Collection<PsiElement> matchedElements);
    void commitUnmatched();
  }

  private MatchedElementsListener myMatchedElementsListener;

  public void setMatcher(GlobalMatchingVisitor matcher) {
    this.matcher = matcher;
  }

  public GlobalMatchingVisitor getMatcher() {
    return matcher;
  }

  public MatchOptions getOptions() {
    return options;
  }

  public void setOptions(MatchOptions options) {
    this.options = options;
  }

  public MatchResultImpl getPreviousResult() {
    return previousResults.isEmpty() ? null : previousResults.peek();
  }

  public MatchResultImpl getResult() {
    if (result==null) result = new MatchResultImpl();
    return result;
  }

  public void pushResult() {
    previousResults.push(result);
    result = null;
  }
  
  public void popResult() {
    result = previousResults.pop();
  }
  
  public void setResult(MatchResultImpl result) {
    this.result = result;
    if (result == null) {
      pattern.clearHandlersState();
    }
  }

  public boolean hasResult() {
    return result!=null;
  }

  public CompiledPattern getPattern() {
    return pattern;
  }

  public void setPattern(CompiledPattern pattern) {
    this.pattern = pattern;
  }

  public MatchResultSink getSink() {
    return sink;
  }

  public void setSink(MatchResultSink sink) {
    this.sink = sink;
  }

  void clear() {
    result = null;
    pattern = null;
  }

  public boolean shouldRecursivelyMatch() {
    return shouldRecursivelyMatch;
  }

  public void setShouldRecursivelyMatch(boolean shouldRecursivelyMatch) {
    this.shouldRecursivelyMatch = shouldRecursivelyMatch;
  }

  public void setMatchedElementsListener(MatchedElementsListener _matchedElementsListener) {
    myMatchedElementsListener = _matchedElementsListener;
  }

  public MatchedElementsListener getMatchedElementsListener() {
    return myMatchedElementsListener;
  }
}

@@ -1,8 +1,609 @@
+/*%0A * Copyright 2000-2017 JetBrains s.r.o.%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 */%0A
 package 
@@ -1257,64 +1257,8 @@
 rue;
-%0A  private boolean myWithAlternativePatternRoots = true;
 %0A%0A  
@@ -1442,24 +1442,24 @@
 hedNodes) %7B%0A
+
     myMatche
@@ -1490,260 +1490,8 @@
  %7D%0A%0A
-  public boolean isWithAlternativePatternRoots() %7B%0A    return myWithAlternativePatternRoots;%0A  %7D%0A%0A  public void setWithAlternativePatternRoots(boolean withAlternativePatternRoots) %7B%0A    myWithAlternativePatternRoots = withAlternativePatternRoots;%0A  %7D%0A%0A
   pu

remove unused methods