// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.jetbrains.python.psi.search;

import com.intellij.openapi.application.ReadAction;
import com.intellij.psi.PsiElement;
import com.intellij.util.Processor;
import com.intellij.util.Query;
import com.intellij.util.QueryExecutor;
import com.jetbrains.python.psi.PyAssignmentStatement;
import com.jetbrains.python.psi.PyClass;
import com.jetbrains.python.psi.PyFunction;
import com.jetbrains.python.psi.PyTargetExpression;
import com.jetbrains.python.pyi.PyiFile;
import com.jetbrains.python.pyi.PyiUtil;
import org.jetbrains.annotations.NotNull;

/**
 * @author yole
 */
public class PyDefinitionsSearch implements QueryExecutor<PsiElement, PsiElement> {
  @Override
  public boolean execute(@NotNull final PsiElement queryParameters, @NotNull final Processor<? super PsiElement> consumer) {
    if (queryParameters instanceof PyClass) {
      final Query<PyClass> query = PyClassInheritorsSearch.search((PyClass)queryParameters, true);
      return query.forEach(consumer);
    }
    else if (queryParameters instanceof PyFunction) {
      final Query<PyFunction> query =
        ReadAction.compute(() -> PyOverridingMethodsSearch.search((PyFunction)queryParameters, true));

      return query.forEach(consumer);
    }
    else if (queryParameters instanceof PyTargetExpression) {  // PY-237
      final PsiElement parent = ReadAction.compute(() -> queryParameters.getParent());

      if (parent instanceof PyAssignmentStatement) {
        return consumer.process(parent);
      }
    }
    else if (queryParameters instanceof PyiFile) {
      final PsiElement originalElement = ReadAction.compute(() -> PyiUtil.getOriginalElement((PyiFile)queryParameters));
      if (originalElement != null) {
        consumer.process(originalElement);
      }
    }
    return true;
  }
}

@@ -860,31 +860,17 @@
 Element 
-queryParameters
+e
 , @NotNu
@@ -924,39 +924,25 @@
 ) %7B%0A    if (
-queryParameters
+e
  instanceof 
@@ -1027,31 +1027,17 @@
 PyClass)
-queryParameters
+e
 , true);
@@ -1090,39 +1090,25 @@
    else if (
-queryParameters
+e
  instanceof 
@@ -1237,31 +1237,17 @@
 unction)
-queryParameters
+e
 , true))
@@ -1302,39 +1302,25 @@
    else if (
-queryParameters
+e
  instanceof 
@@ -1409,31 +1409,17 @@
 e(() -%3E 
-queryParameters
+e
 .getPare
@@ -1547,31 +1547,17 @@
 lse if (
-queryParameters
+e
  instanc
@@ -1677,23 +1677,9 @@
 ile)
-queryParameters
+e
 ));%0A

rename after an old refactoring