Search in sources :

Example 51 with PyFunction

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);
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) PyFunction(com.jetbrains.python.psi.PyFunction) TemplateBuilder(com.intellij.codeInsight.template.TemplateBuilder) OpenFileDescriptor(com.intellij.openapi.fileEditor.OpenFileDescriptor) PyElementGenerator(com.jetbrains.python.psi.PyElementGenerator) PyNamedParameter(com.jetbrains.python.psi.PyNamedParameter) Editor(com.intellij.openapi.editor.Editor) PyParameterList(com.jetbrains.python.psi.PyParameterList) PsiElement(com.intellij.psi.PsiElement)

Example 52 with PyFunction

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();
}
Also used : PyClass(com.jetbrains.python.psi.PyClass) PyFunction(com.jetbrains.python.psi.PyFunction) ArrayList(java.util.ArrayList) AbstractTreeNode(com.intellij.ide.util.treeView.AbstractTreeNode) PyElement(com.jetbrains.python.psi.PyElement)

Example 53 with PyFunction

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;
}
Also used : PyClass(com.jetbrains.python.psi.PyClass) PyFunction(com.jetbrains.python.psi.PyFunction) ArrayList(java.util.ArrayList) AbstractTreeNode(com.intellij.ide.util.treeView.AbstractTreeNode) PyFile(com.jetbrains.python.psi.PyFile)

Example 54 with PyFunction

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();
}
Also used : PyFunction(com.jetbrains.python.psi.PyFunction) ArrayList(java.util.ArrayList) PyDocStringOwner(com.jetbrains.python.psi.PyDocStringOwner) PsiFile(com.intellij.psi.PsiFile) PyFile(com.jetbrains.python.psi.PyFile) PsiElement(com.intellij.psi.PsiElement)

Example 55 with PyFunction

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);
}
Also used : PyClass(com.jetbrains.python.psi.PyClass) PyFunction(com.jetbrains.python.psi.PyFunction) PsiReference(com.intellij.psi.PsiReference) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

PyFunction (com.jetbrains.python.psi.PyFunction)61 PyClass (com.jetbrains.python.psi.PyClass)33 PsiElement (com.intellij.psi.PsiElement)24 NotNull (org.jetbrains.annotations.NotNull)10 Nullable (org.jetbrains.annotations.Nullable)10 PyFile (com.jetbrains.python.psi.PyFile)9 ArrayList (java.util.ArrayList)8 PyMethodMember (com.jetbrains.python.codeInsight.override.PyMethodMember)7 PsiFile (com.intellij.psi.PsiFile)5 Editor (com.intellij.openapi.editor.Editor)4 VirtualFile (com.intellij.openapi.vfs.VirtualFile)4 Document (com.intellij.openapi.editor.Document)3 Project (com.intellij.openapi.project.Project)3 PsiDirectory (com.intellij.psi.PsiDirectory)3 PsiNamedElement (com.intellij.psi.PsiNamedElement)3 TypeEvalContext (com.jetbrains.python.psi.types.TypeEvalContext)3 AbstractTreeNode (com.intellij.ide.util.treeView.AbstractTreeNode)2 ASTNode (com.intellij.lang.ASTNode)2 ItemPresentation (com.intellij.navigation.ItemPresentation)2 Pair (com.intellij.openapi.util.Pair)2