use of com.jetbrains.python.psi.PyClass in project intellij-community by JetBrains.
the class PyJavaSuperMethodsSearchExecutor method execute.
public boolean execute(@NotNull final PySuperMethodsSearch.SearchParameters queryParameters, @NotNull final Processor<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;
}
use of com.jetbrains.python.psi.PyClass in project intellij-community by JetBrains.
the class PyCustomMember method resolve.
@Nullable
public PsiElement resolve(@NotNull final PsiElement context) {
if (myTarget != null) {
return myTarget;
}
PyClass targetClass = null;
if (myTypeName != null) {
final ParameterizedCachedValueProvider<PyClass, PsiElement> provider = new ParameterizedCachedValueProvider<PyClass, PsiElement>() {
@Nullable
@Override
public CachedValueProvider.Result<PyClass> compute(final PsiElement param) {
final PyClass result = PyPsiFacade.getInstance(param.getProject()).createClassByQName(myTypeName, param);
return CachedValueProvider.Result.create(result, PsiModificationTracker.MODIFICATION_COUNT);
}
};
targetClass = CachedValuesManager.getManager(context.getProject()).getParameterizedCachedValue(this, RESOLVE, provider, false, context);
}
final PsiElement resolveTarget = findResolveTarget(context);
if (resolveTarget instanceof PyFunction && !myAlwaysResolveToCustomElement) {
return resolveTarget;
}
if (resolveTarget != null || targetClass != null) {
return new MyInstanceElement(targetClass, context, resolveTarget);
}
return null;
}
use of com.jetbrains.python.psi.PyClass in project intellij-community by JetBrains.
the class PyChangeSignatureQuickFix method applyFix.
public void applyFix(@NotNull final Project project, @NotNull final ProblemDescriptor descriptor) {
final PyFunction function = PsiTreeUtil.getParentOfType(descriptor.getPsiElement(), PyFunction.class);
if (function == null)
return;
final PyClass cls = function.getContainingClass();
assert cls != null;
final String functionName = function.getName();
final String complementaryName = PyNames.NEW.equals(functionName) ? PyNames.INIT : PyNames.NEW;
final TypeEvalContext context = TypeEvalContext.userInitiated(project, descriptor.getEndElement().getContainingFile());
final PyFunction complementaryMethod = myOverridenMethod ? (PyFunction) PySuperMethodsSearch.search(function, context).findFirst() : cls.findMethodByName(complementaryName, true, null);
assert complementaryMethod != null;
final PyMethodDescriptor methodDescriptor = new PyMethodDescriptor(function) {
@Override
public List<PyParameterInfo> getParameters() {
final List<PyParameterInfo> parameterInfos = super.getParameters();
final int paramLength = function.getParameterList().getParameters().length;
final int complementaryParamLength = complementaryMethod.getParameterList().getParameters().length;
if (complementaryParamLength > paramLength)
parameterInfos.add(new PyParameterInfo(-1, "**kwargs", "", false));
return parameterInfos;
}
};
final PyChangeSignatureDialog dialog = new PyChangeSignatureDialog(project, methodDescriptor);
dialog.show();
}
use of com.jetbrains.python.psi.PyClass in project intellij-community by JetBrains.
the class CreateClassQuickFix method applyFix.
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
PsiElement anchor = myAnchor.getElement();
if (anchor == null || !anchor.isValid()) {
return;
}
if (!(anchor instanceof PyFile)) {
anchor = PyPsiUtils.getParentRightBefore(anchor, anchor.getContainingFile());
assert anchor != null;
}
PyClass pyClass = PyElementGenerator.getInstance(project).createFromText(LanguageLevel.getDefault(), PyClass.class, "class " + myClassName + "(object):\n pass");
if (anchor instanceof PyFile) {
pyClass = (PyClass) anchor.add(pyClass);
} else {
pyClass = (PyClass) anchor.getParent().addBefore(pyClass, anchor);
}
pyClass = CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement(pyClass);
final TemplateBuilder builder = TemplateBuilderFactory.getInstance().createTemplateBuilder(pyClass);
builder.replaceElement(pyClass.getSuperClassExpressions()[0], "object");
builder.replaceElement(pyClass.getStatementList(), PyNames.PASS);
final FileEditor editor = FileEditorManager.getInstance(project).getSelectedEditor(anchor.getContainingFile().getVirtualFile());
if (!(editor instanceof TextEditor)) {
return;
}
builder.run(((TextEditor) editor).getEditor(), false);
}
use of com.jetbrains.python.psi.PyClass in project intellij-community by JetBrains.
the class PyControlFlowBuilderTest method testSelf.
public void testSelf() {
final String testName = getTestName(false).toLowerCase();
configureByFile(testName + ".py");
final String fullPath = getTestDataPath() + testName + ".txt";
final PyClass pyClass = ((PyFile) myFile).getTopLevelClasses().get(0);
final ControlFlow flow = ControlFlowCache.getControlFlow(pyClass.getMethods()[0]);
check(fullPath, flow);
}
Aggregations