use of com.jetbrains.python.psi.PyFunction 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.PyFunction in project intellij-community by JetBrains.
the class PyElementNode method getChildrenImpl.
@Override
protected Collection<AbstractTreeNode> getChildrenImpl() {
PyElement value = getValue();
// for performance reasons, we don't show nested functions here
if (value instanceof PyClass) {
final PyClass pyClass = (PyClass) value;
List<AbstractTreeNode> result = new ArrayList<>();
for (PyClass aClass : pyClass.getNestedClasses()) {
result.add(new PyElementNode(myProject, aClass, getSettings()));
}
for (PyFunction function : pyClass.getMethods()) {
result.add(new PyElementNode(myProject, function, getSettings()));
}
return result;
}
return Collections.emptyList();
}
use of com.jetbrains.python.psi.PyFunction in project intellij-community by JetBrains.
the class PyFileNode method getChildrenImpl.
@Override
public Collection<AbstractTreeNode> getChildrenImpl() {
PyFile value = (PyFile) getValue();
List<AbstractTreeNode> children = new ArrayList<>();
for (PyClass child : value.getTopLevelClasses()) {
children.add(new PyElementNode(myProject, child, getSettings()));
}
for (PyFunction function : value.getTopLevelFunctions()) {
children.add(new PyElementNode(myProject, function, getSettings()));
}
return children;
}
use of com.jetbrains.python.psi.PyFunction in project intellij-community by JetBrains.
the class PyTreeStructureProvider method getTopLevelElement.
@Override
public PsiElement getTopLevelElement(PsiElement element) {
PyPsiUtils.assertValid(element);
final Ref<PsiFile> containingFileRef = Ref.create();
ApplicationManager.getApplication().runReadAction(() -> containingFileRef.set(element.getContainingFile()));
final PsiFile containingFile = containingFileRef.get();
if (!(containingFile instanceof PyFile)) {
return null;
}
List<PsiElement> parents = new ArrayList<>();
PyDocStringOwner container = PsiTreeUtil.getParentOfType(element, PyDocStringOwner.class);
while (container != null) {
if (container instanceof PyFile) {
break;
}
parents.add(0, container);
container = PsiTreeUtil.getParentOfType(container, PyDocStringOwner.class);
}
for (PsiElement parent : parents) {
if (parent instanceof PyFunction) {
// we don't display any nodes under functions
return parent;
}
}
if (parents.size() > 0) {
return parents.get(parents.size() - 1);
}
return element.getContainingFile();
}
use of com.jetbrains.python.psi.PyFunction in project intellij-community by JetBrains.
the class RenamePyClassProcessor method findReferences.
@NotNull
@Override
public Collection<PsiReference> findReferences(final PsiElement element) {
if (element instanceof PyClass) {
final PyFunction initMethod = ((PyClass) element).findMethodByName(PyNames.INIT, true, null);
if (initMethod != null) {
final List<PsiReference> allRefs = Collections.synchronizedList(new ArrayList<PsiReference>());
allRefs.addAll(super.findReferences(element));
ReferencesSearch.search(initMethod, GlobalSearchScope.projectScope(element.getProject())).forEach(psiReference -> {
if (psiReference.getCanonicalText().equals(((PyClass) element).getName())) {
allRefs.add(psiReference);
}
return true;
});
return allRefs;
}
}
return super.findReferences(element);
}
Aggregations