use of com.jetbrains.python.psi.PyNamedParameter 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.PyNamedParameter 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.PyNamedParameter in project intellij-community by JetBrains.
the class PyRenameElementQuickFix method applyFix.
@Override
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
PsiElement element = descriptor.getPsiElement();
if (element instanceof PyReferenceExpression) {
final PsiReference reference = element.getReference();
if (reference == null)
return;
element = reference.resolve();
}
final PsiNameIdentifierOwner nameOwner = element instanceof PsiNameIdentifierOwner ? (PsiNameIdentifierOwner) element : PsiTreeUtil.getParentOfType(element, PsiNameIdentifierOwner.class, true);
if (nameOwner != null) {
final VirtualFile virtualFile = nameOwner.getContainingFile().getVirtualFile();
if (virtualFile != null) {
final Editor editor = FileEditorManager.getInstance(project).openTextEditor(new OpenFileDescriptor(project, virtualFile), true);
if (ApplicationManager.getApplication().isUnitTestMode()) {
renameInUnitTestMode(project, nameOwner, editor);
} else {
if (checkLocalScope(element) != null && (nameOwner instanceof PyNamedParameter || nameOwner instanceof PyTargetExpression)) {
new VariableInplaceRenamer(nameOwner, editor).performInplaceRename();
} else {
PsiElementRenameHandler.invoke(nameOwner, project, ScopeUtil.getScopeOwner(nameOwner), editor);
}
}
}
}
}
use of com.jetbrains.python.psi.PyNamedParameter in project intellij-community by JetBrains.
the class PyMethodDescriptor method getParameters.
@Override
public List<PyParameterInfo> getParameters() {
List<PyParameterInfo> parameterInfos = new ArrayList<>();
PyParameter[] parameters = myFunction.getParameterList().getParameters();
for (int i = 0; i < parameters.length; i++) {
PyParameter parameter = parameters[i];
final PyExpression defaultValue = parameter.getDefaultValue();
final String name;
if (parameter instanceof PyNamedParameter) {
if (((PyNamedParameter) parameter).isPositionalContainer()) {
name = "*" + parameter.getName();
} else if (((PyNamedParameter) parameter).isKeywordContainer()) {
name = "**" + parameter.getName();
} else {
name = parameter.getName();
}
} else {
name = parameter.getText();
}
parameterInfos.add(new PyParameterInfo(i, name, defaultValue == null ? null : defaultValue.getText(), defaultValue != null && !StringUtil.isEmptyOrSpaces(defaultValue.getText())));
}
return parameterInfos;
}
use of com.jetbrains.python.psi.PyNamedParameter 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