Search in sources :

Example 31 with PyFunction

use of com.jetbrains.python.psi.PyFunction in project intellij-community by JetBrains.

the class PyInitReferenceSearchExecutor method processQuery.

public void processQuery(@NotNull ReferencesSearch.SearchParameters queryParameters, @NotNull final Processor<PsiReference> consumer) {
    PsiElement element = queryParameters.getElementToSearch();
    if (!(element instanceof PyFunction)) {
        return;
    }
    final AccessToken accessToken = ApplicationManager.getApplication().acquireReadActionLock();
    String className;
    SearchScope searchScope;
    PyFunction function;
    try {
        function = (PyFunction) element;
        if (!PyNames.INIT.equals(function.getName())) {
            return;
        }
        final PyClass pyClass = function.getContainingClass();
        if (pyClass == null) {
            return;
        }
        className = pyClass.getName();
        if (className == null) {
            return;
        }
        searchScope = queryParameters.getEffectiveSearchScope();
        if (searchScope instanceof GlobalSearchScope) {
            searchScope = GlobalSearchScope.getScopeRestrictedByFileTypes((GlobalSearchScope) searchScope, PythonFileType.INSTANCE);
        }
    } finally {
        accessToken.finish();
    }
    queryParameters.getOptimizer().searchWord(className, searchScope, UsageSearchContext.IN_CODE, true, function);
}
Also used : PyClass(com.jetbrains.python.psi.PyClass) PyFunction(com.jetbrains.python.psi.PyFunction) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) AccessToken(com.intellij.openapi.application.AccessToken) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) SearchScope(com.intellij.psi.search.SearchScope) PsiElement(com.intellij.psi.PsiElement)

Example 32 with PyFunction

use of com.jetbrains.python.psi.PyFunction in project intellij-community by JetBrains.

the class PyTargetReference method multiResolve.

@NotNull
@Override
public ResolveResult[] multiResolve(boolean incompleteCode) {
    final ResolveResult[] results = super.multiResolve(incompleteCode);
    boolean shadowed = false;
    for (ResolveResult result : results) {
        final PsiElement element = result.getElement();
        if (element != null && (element.getContainingFile() != myElement.getContainingFile() || element instanceof PyFunction || element instanceof PyClass)) {
            shadowed = true;
            break;
        }
    }
    if (results.length > 0 && !shadowed) {
        return results;
    }
    // resolve to self if no other target found
    return new ResolveResult[] { new PsiElementResolveResult(myElement) };
}
Also used : PyClass(com.jetbrains.python.psi.PyClass) PyFunction(com.jetbrains.python.psi.PyFunction) PsiElementResolveResult(com.intellij.psi.PsiElementResolveResult) PsiElementResolveResult(com.intellij.psi.PsiElementResolveResult) ResolveResult(com.intellij.psi.ResolveResult) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 33 with PyFunction

use of com.jetbrains.python.psi.PyFunction in project intellij-community by JetBrains.

the class PyCallHierarchyProvider method getTarget.

@Nullable
@Override
public PsiElement getTarget(@NotNull DataContext dataContext) {
    Project project = CommonDataKeys.PROJECT.getData(dataContext);
    if (project == null)
        return null;
    PsiElement element = CommonDataKeys.PSI_ELEMENT.getData(dataContext);
    if (element == null) {
        Editor editor = CommonDataKeys.EDITOR.getData(dataContext);
        if (editor != null) {
            PsiFile file = PsiDocumentManager.getInstance(project).getPsiFile(editor.getDocument());
            if (file == null)
                return null;
            element = TargetElementUtil.findTargetElement(editor, TargetElementUtil.ELEMENT_NAME_ACCEPTED | TargetElementUtil.REFERENCED_ELEMENT_ACCEPTED | TargetElementUtil.LOOKUP_ITEM_ACCEPTED);
            if (element instanceof PyFunction || element instanceof PyClass || element instanceof PyFile) {
                return element;
            }
            element = file.findElementAt(editor.getCaretModel().getOffset());
        }
    }
    return PsiTreeUtil.getNonStrictParentOfType(element, PyFunction.class, PyClass.class, PyFile.class);
}
Also used : PyClass(com.jetbrains.python.psi.PyClass) Project(com.intellij.openapi.project.Project) PyFunction(com.jetbrains.python.psi.PyFunction) PsiFile(com.intellij.psi.PsiFile) PyFile(com.jetbrains.python.psi.PyFile) Editor(com.intellij.openapi.editor.Editor) PsiElement(com.intellij.psi.PsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 34 with PyFunction

use of com.jetbrains.python.psi.PyFunction in project intellij-community by JetBrains.

the class PyCallHierarchyTreeStructureBase method buildChildren.

@NotNull
@Override
protected Object[] buildChildren(@NotNull HierarchyNodeDescriptor descriptor) {
    final List<PyHierarchyNodeDescriptor> descriptors = new ArrayList<>();
    if (descriptor instanceof PyHierarchyNodeDescriptor) {
        final PyHierarchyNodeDescriptor pyDescriptor = (PyHierarchyNodeDescriptor) descriptor;
        final PsiElement element = pyDescriptor.getPsiElement();
        final boolean isCallable = element instanceof PyFunction || element instanceof PyClass || element instanceof PyFile;
        HierarchyNodeDescriptor nodeDescriptor = getBaseDescriptor();
        if (!(element instanceof PyElement) || !isCallable || nodeDescriptor == null) {
            return ArrayUtil.EMPTY_OBJECT_ARRAY;
        }
        final List<PsiElement> children = getChildren((PyElement) element);
        final HashMap<PsiElement, PyHierarchyNodeDescriptor> callerToDescriptorMap = new HashMap<>();
        PsiElement baseClass = element instanceof PyFunction ? ((PyFunction) element).getContainingClass() : null;
        for (PsiElement caller : children) {
            if (isInScope(baseClass, caller, myScopeType)) {
                PyHierarchyNodeDescriptor callerDescriptor = callerToDescriptorMap.get(caller);
                if (callerDescriptor == null) {
                    callerDescriptor = new PyHierarchyNodeDescriptor(descriptor, caller, false);
                    callerToDescriptorMap.put(caller, callerDescriptor);
                    descriptors.add(callerDescriptor);
                }
            }
        }
    }
    return ArrayUtil.toObjectArray(descriptors);
}
Also used : PyClass(com.jetbrains.python.psi.PyClass) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) PyFile(com.jetbrains.python.psi.PyFile) PyHierarchyNodeDescriptor(com.jetbrains.python.hierarchy.PyHierarchyNodeDescriptor) PyFunction(com.jetbrains.python.psi.PyFunction) HierarchyNodeDescriptor(com.intellij.ide.hierarchy.HierarchyNodeDescriptor) PyHierarchyNodeDescriptor(com.jetbrains.python.hierarchy.PyHierarchyNodeDescriptor) PsiElement(com.intellij.psi.PsiElement) PyElement(com.jetbrains.python.psi.PyElement) NotNull(org.jetbrains.annotations.NotNull)

Example 35 with PyFunction

use of com.jetbrains.python.psi.PyFunction in project intellij-community by JetBrains.

the class AbstractPythonLegacyTestRunConfiguration method getRefactoringElementListener.

@Override
public RefactoringElementListener getRefactoringElementListener(PsiElement element) {
    if (element instanceof PsiDirectory) {
        VirtualFile vFile = ((PsiDirectory) element).getVirtualFile();
        if ((myTestType == TestType.TEST_FOLDER && pathsEqual(vFile, myFolderName)) || pathsEqual(vFile, getWorkingDirectory())) {
            return new RefactoringElementAdapter() {

                @Override
                protected void elementRenamedOrMoved(@NotNull PsiElement newElement) {
                    String newPath = FileUtil.toSystemDependentName(((PsiDirectory) newElement).getVirtualFile().getPath());
                    setWorkingDirectory(newPath);
                    if (myTestType == TestType.TEST_FOLDER) {
                        myFolderName = newPath;
                    }
                }

                @Override
                public void undoElementMovedOrRenamed(@NotNull PsiElement newElement, @NotNull String oldQualifiedName) {
                    final String systemDependant = FileUtil.toSystemDependentName(oldQualifiedName);
                    setWorkingDirectory(systemDependant);
                    if (myTestType == TestType.TEST_FOLDER) {
                        myFolderName = systemDependant;
                    }
                }
            };
        }
        return null;
    }
    if (myTestType == TestType.TEST_FOLDER) {
        return null;
    }
    File scriptFile = new File(myScriptName);
    if (!scriptFile.isAbsolute()) {
        scriptFile = new File(getWorkingDirectory(), myScriptName);
    }
    PsiFile containingFile = element.getContainingFile();
    VirtualFile vFile = containingFile == null ? null : containingFile.getVirtualFile();
    if (vFile != null && Comparing.equal(new File(vFile.getPath()).getAbsolutePath(), scriptFile.getAbsolutePath())) {
        if (element instanceof PsiFile) {
            return new RefactoringElementAdapter() {

                @Override
                protected void elementRenamedOrMoved(@NotNull PsiElement newElement) {
                    VirtualFile virtualFile = ((PsiFile) newElement).getVirtualFile();
                    if (virtualFile != null) {
                        myScriptName = FileUtil.toSystemDependentName(virtualFile.getPath());
                    }
                }

                @Override
                public void undoElementMovedOrRenamed(@NotNull PsiElement newElement, @NotNull String oldQualifiedName) {
                    myScriptName = FileUtil.toSystemDependentName(oldQualifiedName);
                }
            };
        }
        if (element instanceof PyClass && (myTestType == TestType.TEST_CLASS || myTestType == TestType.TEST_METHOD) && Comparing.equal(((PyClass) element).getName(), myClassName)) {
            return new RefactoringElementAdapter() {

                @Override
                protected void elementRenamedOrMoved(@NotNull PsiElement newElement) {
                    myClassName = ((PyClass) newElement).getName();
                }

                @Override
                public void undoElementMovedOrRenamed(@NotNull PsiElement newElement, @NotNull String oldQualifiedName) {
                    myClassName = oldQualifiedName;
                }
            };
        }
        if (element instanceof PyFunction && Comparing.equal(((PyFunction) element).getName(), myMethodName)) {
            ScopeOwner scopeOwner = PsiTreeUtil.getParentOfType(element, ScopeOwner.class);
            if ((myTestType == TestType.TEST_FUNCTION && scopeOwner instanceof PyFile) || (myTestType == TestType.TEST_METHOD && scopeOwner instanceof PyClass && Comparing.equal(scopeOwner.getName(), myClassName))) {
                return new RefactoringElementAdapter() {

                    @Override
                    protected void elementRenamedOrMoved(@NotNull PsiElement newElement) {
                        myMethodName = ((PyFunction) newElement).getName();
                    }

                    @Override
                    public void undoElementMovedOrRenamed(@NotNull PsiElement newElement, @NotNull String oldQualifiedName) {
                        final int methodIdx = oldQualifiedName.indexOf("#") + 1;
                        if (methodIdx > 0 && methodIdx < oldQualifiedName.length()) {
                            myMethodName = oldQualifiedName.substring(methodIdx);
                        }
                    }
                };
            }
        }
    }
    return null;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) PyClass(com.jetbrains.python.psi.PyClass) PyFile(com.jetbrains.python.psi.PyFile) NotNull(org.jetbrains.annotations.NotNull) ScopeOwner(com.jetbrains.python.codeInsight.controlflow.ScopeOwner) PyFunction(com.jetbrains.python.psi.PyFunction) PsiDirectory(com.intellij.psi.PsiDirectory) PsiFile(com.intellij.psi.PsiFile) PyFile(com.jetbrains.python.psi.PyFile) VirtualFile(com.intellij.openapi.vfs.VirtualFile) PsiFile(com.intellij.psi.PsiFile) File(java.io.File) PsiElement(com.intellij.psi.PsiElement) RefactoringElementAdapter(com.intellij.refactoring.listeners.RefactoringElementAdapter)

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