All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.jetbrains.python.psi.search.PySuperMethodsSearch Maven / Gradle / Ivy

Go to download

A packaging of the IntelliJ Community Edition python-community library. This is release number 1 of trunk branch 142.

The newest version!
/*
 * Copyright 2000-2014 JetBrains s.r.o.
 *
 * 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 com.jetbrains.python.psi.search;

import com.intellij.psi.PsiElement;
import com.intellij.psi.search.searches.ExtensibleQueryFactory;
import com.intellij.util.Query;
import com.jetbrains.python.psi.PyClass;
import com.jetbrains.python.psi.PyFunction;

import java.util.ArrayList;
import java.util.List;

/**
 * @author yole
 */
public class PySuperMethodsSearch extends ExtensibleQueryFactory {
  public static final PySuperMethodsSearch INSTANCE = new PySuperMethodsSearch();

  private static PyFunction getBaseMethod(List superMethods,
                                         PyClass containingClass) {

    for (PyClass ancestor : containingClass.getAncestorClasses()) {
      for (PsiElement method : superMethods) {
        if (ancestor.equals(((PyFunction)method).getContainingClass()))
          return (PyFunction)method;
      }
    }
    return (PyFunction) superMethods.get(superMethods.size()-1);
  }

  public static PyFunction findDeepestSuperMethod(PyFunction function) {
    List superMethods = new ArrayList(search(function, true).findAll());
    while (superMethods.size() > 0) {
      function = getBaseMethod(superMethods, function.getContainingClass());
      superMethods = new ArrayList(search(function, true).findAll());
    }
    return function;
  }

  public static class SearchParameters {
    private final PyFunction myDerivedMethod;
    private final boolean myDeepSearch;

    public SearchParameters(final PyFunction derivedMethod, boolean deepSearch) {
      myDerivedMethod = derivedMethod;
      myDeepSearch = deepSearch;
    }

    public PyFunction getDerivedMethod() {
      return myDerivedMethod;
    }

    public boolean isDeepSearch() {
      return myDeepSearch;
    }
  }

  private PySuperMethodsSearch() {
    super("Pythonid");
  }

  public static Query search(final PyFunction derivedMethod) {
    final SearchParameters parameters = new SearchParameters(derivedMethod, false);
    return INSTANCE.createUniqueResultsQuery(parameters);
  }

  public static Query search(final PyFunction derivedMethod, boolean deepSearch) {
    final SearchParameters parameters = new SearchParameters(derivedMethod, deepSearch);
    return INSTANCE.createUniqueResultsQuery(parameters);
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy