Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,35 +16,36 @@

package consulo.ironPython.psi.impl;

import com.jetbrains.python.impl.psi.search.PySuperMethodsSearch;
import com.jetbrains.python.psi.PyClass;
import com.jetbrains.python.psi.PyFunction;
import com.jetbrains.python.impl.psi.search.PySuperMethodsSearch;
import consulo.application.util.function.Processor;
import consulo.application.util.query.QueryExecutor;
import consulo.language.psi.PsiElement;

import jakarta.annotation.Nonnull;

import java.util.function.Predicate;

/**
* @author yole
*/
public class PyDotNetSuperMethodsSearchExecutor implements QueryExecutor<PsiElement, PySuperMethodsSearch.SearchParameters>
{
@Override
public boolean execute(@Nonnull final PySuperMethodsSearch.SearchParameters queryParameters, @Nonnull final Processor<? super PsiElement> consumer)
{
PyFunction func = queryParameters.getDerivedMethod();
PyClass containingClass = func.getContainingClass();
/*if (containingClass != null) {
for (PyClassLikeType type : containingClass.getSuperClassTypes(TypeEvalContext.codeInsightFallback())) {
if (type instanceof PyDotNetClassType) {
final DotNetTypeDeclaration psiClass = ((PyDotNetClassType)type).getPsiClass();
PsiMethod[] methods = psiClass.findMethodsByName(func.getName(), true);
// the Python method actually does override/implement all of Java super methods with the same name
if (!ContainerUtil.process(methods, consumer)) return false;
}
}
} */
return true;
}
public class PyDotNetSuperMethodsSearchExecutor implements QueryExecutor<PsiElement, PySuperMethodsSearch.SearchParameters> {
@Override
public boolean execute(
@Nonnull final PySuperMethodsSearch.SearchParameters queryParameters,
@Nonnull final Predicate<? super PsiElement> consumer
) {
PyFunction func = queryParameters.getDerivedMethod();
PyClass containingClass = func.getContainingClass();
/*if (containingClass != null) {
for (PyClassLikeType type : containingClass.getSuperClassTypes(TypeEvalContext.codeInsightFallback())) {
if (type instanceof PyDotNetClassType) {
final DotNetTypeDeclaration psiClass = ((PyDotNetClassType)type).getPsiClass();
PsiMethod[] methods = psiClass.findMethodsByName(func.getName(), true);
// the Python method actually does override/implement all of Java super methods with the same name
if (!ContainerUtil.process(methods, consumer)) return false;
}
}
}*/
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,32 +30,32 @@

import jakarta.annotation.Nonnull;

import java.util.function.Predicate;

/**
* @author yole
*/
@ExtensionImpl
public class PyJavaSuperMethodsSearchExecutor implements PySuperMethodsSearchExecutor
{
public boolean execute(@Nonnull final PySuperMethodsSearch.SearchParameters queryParameters, @Nonnull final Processor<? super PsiElement> consumer)
{
PyFunction func = queryParameters.getDerivedMethod();
PyClass containingClass = func.getContainingClass();
if(containingClass != null)
{
for(PyClassLikeType type : containingClass.getSuperClassTypes(TypeEvalContext.codeInsightFallback(containingClass.getProject())))
{
if(type instanceof PyJavaClassType)
{
final PsiClass psiClass = ((PyJavaClassType) type).getPsiClass();
PsiMethod[] methods = psiClass.findMethodsByName(func.getName(), true);
// the Python method actually does override/implement all of Java super methods with the same name
if(!ContainerUtil.process(methods, consumer))
{
return false;
}
}
}
}
return true;
}
public class PyJavaSuperMethodsSearchExecutor implements PySuperMethodsSearchExecutor {
@Override
public boolean execute(
@Nonnull PySuperMethodsSearch.SearchParameters queryParameters,
@Nonnull Predicate<? super PsiElement> consumer
) {
PyFunction func = queryParameters.getDerivedMethod();
PyClass containingClass = func.getContainingClass();
if (containingClass != null) {
for (PyClassLikeType type : containingClass.getSuperClassTypes(TypeEvalContext.codeInsightFallback(containingClass.getProject()))) {
if (type instanceof PyJavaClassType javaClassType) {
PsiClass psiClass = javaClassType.getPsiClass();
PsiMethod[] methods = psiClass.findMethodsByName(func.getName(), true);
// the Python method actually does override/implement all of Java super methods with the same name
if (!ContainerUtil.process(methods, consumer)) {
return false;
}
}
}
}
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,68 +18,73 @@
import com.google.common.collect.ImmutableSet;
import com.jetbrains.python.impl.psi.stubs.PySuperClassIndex;
import com.jetbrains.python.psi.PyClass;
import consulo.annotation.access.RequiredReadAction;
import consulo.annotation.component.ExtensionImpl;
import consulo.application.ReadAction;
import consulo.application.util.function.Processor;
import consulo.application.AccessRule;
import consulo.language.psi.stub.StubIndex;
import consulo.project.Project;
import consulo.project.content.scope.ProjectScopes;

import jakarta.annotation.Nonnull;

import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import java.util.function.Predicate;

/**
* @author yole
*/
@ExtensionImpl
public class DefaultPyClassInheritorsSearchExecutor implements PyClassInheritorsSearchExecutor {
/**
* These base classes are to general to look for inheritors list.
*/
protected static final ImmutableSet<String> IGNORED_BASES = ImmutableSet.of("object", "BaseException", "Exception");

/**
* These base classes are to general to look for inheritors list.
*/
protected static final ImmutableSet<String> IGNORED_BASES = ImmutableSet.of("object", "BaseException", "Exception");

public boolean execute(@Nonnull final PyClassInheritorsSearch.SearchParameters queryParameters,
@Nonnull final Processor<? super PyClass> consumer) {
Set<PyClass> processed = new HashSet<>();

return ReadAction.compute(() -> processDirectInheritors(queryParameters.getSuperClass(),
consumer,
queryParameters.isCheckDeepInheritance(),
processed));
}
@Override
public boolean execute(
@Nonnull PyClassInheritorsSearch.SearchParameters queryParameters,
@Nonnull Predicate<? super PyClass> consumer
) {
Set<PyClass> processed = new HashSet<>();

private static boolean processDirectInheritors(final PyClass superClass,
final Processor<? super PyClass> consumer,
final boolean checkDeep,
final Set<PyClass> processed) {
final String superClassName = superClass.getName();
if (superClassName == null || IGNORED_BASES.contains(superClassName)) {
return true; // we don't want to look for inheritors of overly general classes
return AccessRule.read(() -> processDirectInheritors(
queryParameters.getSuperClass(),
consumer,
queryParameters.isCheckDeepInheritance(),
processed
));
}
if (processed.contains(superClass)) {
return true;
}
processed.add(superClass);
Project project = superClass.getProject();
final Collection<PyClass> candidates =
StubIndex.getElements(PySuperClassIndex.KEY, superClassName, project, ProjectScopes.getAllScope(project), PyClass.class);
for (PyClass candidate : candidates) {
final PyClass[] classes = candidate.getSuperClasses(null);
for (PyClass superClassCandidate : classes) {
if (superClassCandidate.isEquivalentTo(superClass)) {
if (!consumer.process(candidate)) {
return false;
}
if (checkDeep && !processDirectInheritors(candidate, consumer, checkDeep, processed)) {
return false;
}
break;

@RequiredReadAction
private static boolean processDirectInheritors(
PyClass superClass,
Predicate<? super PyClass> consumer,
boolean checkDeep,
Set<PyClass> processed
) {
String superClassName = superClass.getName();
if (superClassName == null || IGNORED_BASES.contains(superClassName)) {
return true; // we don't want to look for inheritors of overly general classes
}
if (processed.contains(superClass)) {
return true;
}
processed.add(superClass);
Project project = superClass.getProject();
Collection<PyClass> candidates =
StubIndex.getElements(PySuperClassIndex.KEY, superClassName, project, ProjectScopes.getAllScope(project), PyClass.class);
for (PyClass candidate : candidates) {
PyClass[] classes = candidate.getSuperClasses(null);
for (PyClass superClassCandidate : classes) {
if (superClassCandidate.isEquivalentTo(superClass)) {
if (!consumer.test(candidate) || checkDeep && !processDirectInheritors(candidate, consumer, checkDeep, processed)) {
return false;
}
break;
}
}
}
}
return true;
}
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,44 +18,42 @@
import com.jetbrains.python.impl.psi.PyUtil;
import com.jetbrains.python.psi.*;
import consulo.annotation.component.ExtensionImpl;
import consulo.application.ReadAction;
import consulo.application.util.function.Processor;

import consulo.application.AccessRule;
import jakarta.annotation.Nonnull;

import java.util.function.Predicate;

/**
* @author yole
*/
@ExtensionImpl
public class DefaultPyOverridingMethodsSearchExecutor implements PyOverridingMethodsSearchExecutor {
@Override
public boolean execute(@Nonnull final PyOverridingMethodsSearch.SearchParameters queryParameters,
@Nonnull final Processor<? super PyFunction> consumer) {
final PyFunction baseMethod = queryParameters.getFunction();
@Override
public boolean execute(
@Nonnull PyOverridingMethodsSearch.SearchParameters queryParameters,
@Nonnull Predicate<? super PyFunction> consumer
) {
PyFunction baseMethod = queryParameters.getFunction();

final PyClass containingClass = ReadAction.compute(baseMethod::getContainingClass);
PyClass containingClass = AccessRule.read(baseMethod::getContainingClass);

return PyClassInheritorsSearch.search(containingClass, queryParameters.isCheckDeep()).forEach(pyClass -> {
PyFunction overridingMethod = ReadAction.compute(() -> {
PyFunction func = pyClass.findMethodByName(baseMethod.getName(), false, null);
if (func != null) {
final Property baseProperty = baseMethod.getProperty();
final Property overridingProperty = func.getProperty();
if (baseProperty != null && overridingProperty != null) {
final AccessDirection direction = PyUtil.getPropertyAccessDirection(baseMethod);
final PyCallable callable = overridingProperty.getByDirection(direction).valueOrNull();
func = (callable instanceof PyFunction) ? (PyFunction)callable : null;
}
}
return PyClassInheritorsSearch.search(containingClass, queryParameters.isCheckDeep()).forEach(pyClass -> {
PyFunction overridingMethod = AccessRule.read(() -> {
PyFunction func = pyClass.findMethodByName(baseMethod.getName(), false, null);
if (func != null) {
Property baseProperty = baseMethod.getProperty();
Property overridingProperty = func.getProperty();
if (baseProperty != null && overridingProperty != null) {
AccessDirection direction = PyUtil.getPropertyAccessDirection(baseMethod);
PyCallable callable = overridingProperty.getByDirection(direction).valueOrNull();
func = callable instanceof PyFunction function ? function : null;
}
}

return func;
});
return func;
});

//noinspection SimplifiableIfStatement
if (overridingMethod != null) {
return consumer.process(overridingMethod);
}
return true;
});
}
return overridingMethod == null || consumer.test(overridingMethod);
});
}
}
Loading
Loading