Search in sources :

Example 46 with PyFunction

use of com.jetbrains.python.psi.PyFunction 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();
}
Also used : PyClass(com.jetbrains.python.psi.PyClass) PyArgumentList(com.jetbrains.python.psi.PyArgumentList) PyFunction(com.jetbrains.python.psi.PyFunction) ASTNode(com.intellij.lang.ASTNode) TextRange(com.intellij.openapi.util.TextRange) PyParameterList(com.jetbrains.python.psi.PyParameterList) NotNull(org.jetbrains.annotations.NotNull)

Example 47 with PyFunction

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

the class PyLineMarkerProvider method collectSlowLineMarkers.

public void collectSlowLineMarkers(@NotNull final List<PsiElement> elements, @NotNull final Collection<LineMarkerInfo> result) {
    Set<PyFunction> functions = new HashSet<PyFunction>();
    for (PsiElement element : elements) {
        if (element instanceof PyClass) {
            collectInheritingClasses((PyClass) element, result);
        } else if (element instanceof PyFunction) {
            functions.add((PyFunction) element);
        }
    }
    collectOverridingMethods(functions, result);
}
Also used : PyClass(com.jetbrains.python.psi.PyClass) PyFunction(com.jetbrains.python.psi.PyFunction) PsiElement(com.intellij.psi.PsiElement) HashSet(com.intellij.util.containers.HashSet)

Example 48 with PyFunction

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

the class PyLineMarkerProvider method collectOverridingMethods.

private static void collectOverridingMethods(final Set<PyFunction> functions, final Collection<LineMarkerInfo> result) {
    Set<PyClass> classes = new HashSet<PyClass>();
    final MultiMap<PyClass, PyFunction> candidates = new MultiMap<PyClass, PyFunction>();
    for (PyFunction function : functions) {
        PyClass pyClass = function.getContainingClass();
        if (pyClass != null && function.getName() != null) {
            classes.add(pyClass);
            candidates.putValue(pyClass, function);
        }
    }
    final Set<PyFunction> overridden = new HashSet<PyFunction>();
    for (final PyClass pyClass : classes) {
        PyClassInheritorsSearch.search(pyClass, true).forEach(inheritor -> {
            for (Iterator<PyFunction> it = candidates.get(pyClass).iterator(); it.hasNext(); ) {
                PyFunction func = it.next();
                if (inheritor.findMethodByName(func.getName(), false, null) != null) {
                    overridden.add(func);
                    it.remove();
                }
            }
            return !candidates.isEmpty();
        });
        if (candidates.isEmpty())
            break;
    }
    for (PyFunction func : overridden) {
        result.add(new LineMarkerInfo<PyFunction>(func, func.getTextOffset(), AllIcons.Gutter.OverridenMethod, Pass.LINE_MARKERS, ourOverridingMethodTooltipProvider, ourOverridingMethodNavigator));
    }
}
Also used : PyClass(com.jetbrains.python.psi.PyClass) MultiMap(com.intellij.util.containers.MultiMap) PyFunction(com.jetbrains.python.psi.PyFunction) HashSet(com.intellij.util.containers.HashSet)

Example 49 with PyFunction

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

the class PyMethodNameTypedHandler method beforeCharTyped.

@Override
public Result beforeCharTyped(char character, Project project, Editor editor, PsiFile file, FileType fileType) {
    // else we'd mess up with other file types!
    if (DumbService.isDumb(project) || !(fileType instanceof PythonFileType))
        return Result.CONTINUE;
    if (character == '(') {
        if (!PyCodeInsightSettings.getInstance().INSERT_SELF_FOR_METHODS) {
            return Result.CONTINUE;
        }
        final Document document = editor.getDocument();
        final PsiDocumentManager documentManager = PsiDocumentManager.getInstance(project);
        final int offset = editor.getCaretModel().getOffset();
        PsiElement token = file.findElementAt(offset - 1);
        // sanity check: beyond EOL
        if (token == null)
            return Result.CONTINUE;
        final ASTNode token_node = token.getNode();
        if (token_node != null && token_node.getElementType() == PyTokenTypes.IDENTIFIER) {
            PsiElement maybe_def = PyPsiUtils.getPrevNonCommentSibling(token.getPrevSibling(), false);
            if (maybe_def != null) {
                ASTNode def_node = maybe_def.getNode();
                if (def_node != null && def_node.getElementType() == PyTokenTypes.DEF_KEYWORD) {
                    PsiElement maybe_func = token.getParent();
                    if (maybe_func instanceof PyFunction) {
                        PyFunction func = (PyFunction) maybe_func;
                        PyUtil.MethodFlags flags = PyUtil.MethodFlags.of(func);
                        if (flags != null) {
                            // we're in a method
                            // TODO: all string constants go to Settings
                            String pname = flags.isClassMethod() || flags.isMetaclassMethod() ? "cls" : "self";
                            final boolean is_new = PyNames.NEW.equals(func.getName());
                            if (flags.isMetaclassMethod() && is_new) {
                                pname = "typ";
                            } else if (flags.isClassMethod() || is_new) {
                                pname = "cls";
                            } else if (flags.isStaticMethod())
                                pname = "";
                            documentManager.commitDocument(document);
                            // TODO: only print the ")" if Settings require it
                            int caretOffset = editor.getCaretModel().getOffset();
                            String textToType = "(" + pname + ")";
                            CharSequence chars = editor.getDocument().getCharsSequence();
                            if (caretOffset == chars.length() || chars.charAt(caretOffset) != ':') {
                                textToType += ':';
                            }
                            // right after param name
                            EditorModificationUtil.insertStringAtCaret(editor, textToType, true, 1 + pname.length());
                            return Result.STOP;
                        }
                    }
                }
            }
        }
    }
    // the default
    return Result.CONTINUE;
}
Also used : PyFunction(com.jetbrains.python.psi.PyFunction) PythonFileType(com.jetbrains.python.PythonFileType) ASTNode(com.intellij.lang.ASTNode) Document(com.intellij.openapi.editor.Document) PsiElement(com.intellij.psi.PsiElement) PyUtil(com.jetbrains.python.psi.PyUtil) PsiDocumentManager(com.intellij.psi.PsiDocumentManager)

Example 50 with PyFunction

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

the class PythonPsiManager method isOutOfCodeBlock.

protected boolean isOutOfCodeBlock(@NotNull PsiElement element) {
    while (true) {
        if (element instanceof PyFile) {
            return true;
        }
        if (element instanceof PsiFile || element instanceof PsiDirectory || element == null) {
            return false;
        }
        PsiElement pparent = element.getParent();
        if (pparent instanceof PyFunction) {
            PyFunction pyFunction = (PyFunction) pparent;
            return element == pyFunction.getParameterList() || element == pyFunction.getNameIdentifier();
        }
        element = pparent;
    }
}
Also used : PyFunction(com.jetbrains.python.psi.PyFunction) PsiDirectory(com.intellij.psi.PsiDirectory) PsiFile(com.intellij.psi.PsiFile) PyFile(com.jetbrains.python.psi.PyFile) PsiElement(com.intellij.psi.PsiElement)

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