use of com.jetbrains.python.psi.PyParameterList in project intellij-community by JetBrains.
the class AddSelfQuickFix method applyFix.
public void applyFix(@NotNull final Project project, @NotNull final ProblemDescriptor descriptor) {
PsiElement element = descriptor.getPsiElement();
if (element instanceof PyParameterList) {
final PyParameterList parameterList = (PyParameterList) element;
PyNamedParameter newParameter = PyElementGenerator.getInstance(project).createParameter(myParamName);
parameterList.addParameter(newParameter);
}
}
use of com.jetbrains.python.psi.PyParameterList in project intellij-community by JetBrains.
the class PyDeclarationRangeHandler method getDeclarationRange.
@NotNull
@Override
public TextRange getDeclarationRange(@NotNull PsiElement container) {
int start = container.getTextRange().getStartOffset();
if (container instanceof PyFunction) {
PyParameterList parameterList = ((PyFunction) container).getParameterList();
return new TextRange(start, parameterList.getTextRange().getEndOffset());
}
if (container instanceof PyClass) {
PyArgumentList argumentList = ((PyClass) container).getSuperClassExpressionList();
if (argumentList != null) {
return new TextRange(start, argumentList.getTextRange().getEndOffset());
}
ASTNode nameNode = ((PyClass) container).getNameNode();
if (nameNode != null) {
return new TextRange(start, nameNode.getStartOffset() + nameNode.getTextLength());
}
}
return container.getTextRange();
}
use of com.jetbrains.python.psi.PyParameterList in project intellij-community by JetBrains.
the class PyFunctionFixer method doApply.
@Override
public void doApply(@NotNull Editor editor, @NotNull PySmartEnterProcessor processor, @NotNull PyFunction function) throws IncorrectOperationException {
final PsiElement colon = PyPsiUtils.getFirstChildOfType(function, PyTokenTypes.COLON);
if (!isFakeFunction(function) && colon == null) {
final PyParameterList parameterList = function.getParameterList();
if (function.getNameNode() == null) {
processor.registerUnresolvedError(parameterList.getTextOffset());
}
final int colonOffset;
final PyAnnotation annotation = function.getAnnotation();
if (annotation != null) {
colonOffset = annotation.getTextRange().getEndOffset();
} else {
colonOffset = parameterList.getTextRange().getEndOffset();
}
editor.getDocument().insertString(colonOffset, ":");
}
}
use of com.jetbrains.python.psi.PyParameterList in project intellij-community by JetBrains.
the class UnresolvedReferenceAddParameterQuickFix method applyFix.
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
final PsiElement element = descriptor.getPsiElement();
final PyElementGenerator elementGenerator = PyElementGenerator.getInstance(project);
final PyNamedParameter parameter = elementGenerator.createParameter(element.getText() + "=None");
final PyFunction function = PsiTreeUtil.getParentOfType(element, PyFunction.class);
if (function != null) {
final PyParameterList parameterList = function.getParameterList();
parameterList.addParameter(parameter);
CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement(parameterList);
final TemplateBuilder builder = TemplateBuilderFactory.getInstance().createTemplateBuilder(parameter);
builder.replaceRange(TextRange.create(parameter.getTextLength() - 4, parameter.getTextLength()), "None");
final VirtualFile virtualFile = function.getContainingFile().getVirtualFile();
if (virtualFile == null)
return;
final Editor editor = FileEditorManager.getInstance(project).openTextEditor(new OpenFileDescriptor(project, virtualFile), true);
if (editor == null)
return;
builder.run(editor, false);
}
}
use of com.jetbrains.python.psi.PyParameterList in project intellij-community by JetBrains.
the class PyJavaTypeProvider method getParameterType.
public Ref<PyType> getParameterType(@NotNull final PyNamedParameter param, @NotNull final PyFunction func, @NotNull TypeEvalContext context) {
if (!(param.getParent() instanceof PyParameterList))
return null;
List<PyNamedParameter> params = ParamHelper.collectNamedParameters((PyParameterList) param.getParent());
final int index = params.indexOf(param);
if (index < 0)
return null;
final List<PyType> superMethodParameterTypes = new ArrayList<>();
PySuperMethodsSearch.search(func, context).forEach(psiElement -> {
if (psiElement instanceof PsiMethod) {
final PsiMethod method = (PsiMethod) psiElement;
final PsiParameter[] psiParameters = method.getParameterList().getParameters();
int javaIndex = method.hasModifierProperty(PsiModifier.STATIC) ? index : index - 1;
if (javaIndex < psiParameters.length) {
PsiType paramType = psiParameters[javaIndex].getType();
if (paramType instanceof PsiClassType) {
final PsiClass psiClass = ((PsiClassType) paramType).resolve();
if (psiClass != null) {
superMethodParameterTypes.add(new PyJavaClassType(psiClass, false));
}
}
}
}
return true;
});
if (superMethodParameterTypes.size() > 0) {
final PyType type = superMethodParameterTypes.get(0);
if (type != null) {
return Ref.create(type);
}
}
return null;
}
Aggregations