Search in sources :

Example 16 with PyFunction

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

the class PyBlockEvaluatorTest method testFunction.

public void testFunction() {
    PyBlockEvaluator eval = new PyBlockEvaluator();
    PyFile file = (PyFile) PsiFileFactory.getInstance(myFixture.getProject()).createFileFromText("a.py", PythonFileType.INSTANCE, "def foo(): return 'a'");
    PyFunction foo = file.findTopLevelFunction("foo");
    eval.evaluate(foo);
    assertEquals("a", eval.getReturnValue());
}
Also used : PyFunction(com.jetbrains.python.psi.PyFunction) PyBlockEvaluator(com.jetbrains.python.psi.impl.blockEvaluator.PyBlockEvaluator) PyFile(com.jetbrains.python.psi.PyFile)

Example 17 with PyFunction

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

the class PyHierarchyNodeDescriptor method update.

@Override
public boolean update() {
    boolean changes = super.update();
    final CompositeAppearance oldText = myHighlightedText;
    myHighlightedText = new CompositeAppearance();
    NavigatablePsiElement element = (NavigatablePsiElement) getPsiElement();
    if (element == null) {
        final String invalidPrefix = IdeBundle.message("node.hierarchy.invalid");
        if (!myHighlightedText.getText().startsWith(invalidPrefix)) {
            myHighlightedText.getBeginning().addText(invalidPrefix, HierarchyNodeDescriptor.getInvalidPrefixAttributes());
        }
        return true;
    }
    final ItemPresentation presentation = element.getPresentation();
    if (presentation != null) {
        if (element instanceof PyFunction) {
            final PyClass cls = ((PyFunction) element).getContainingClass();
            if (cls != null) {
                myHighlightedText.getEnding().addText(cls.getName() + ".");
            }
        }
        myHighlightedText.getEnding().addText(presentation.getPresentableText());
        myHighlightedText.getEnding().addText(" " + presentation.getLocationString(), HierarchyNodeDescriptor.getPackageNameAttributes());
    }
    myName = myHighlightedText.getText();
    if (!Comparing.equal(myHighlightedText, oldText)) {
        changes = true;
    }
    return changes;
}
Also used : PyClass(com.jetbrains.python.psi.PyClass) PyFunction(com.jetbrains.python.psi.PyFunction) ItemPresentation(com.intellij.navigation.ItemPresentation) CompositeAppearance(com.intellij.openapi.roots.ui.util.CompositeAppearance) NavigatablePsiElement(com.intellij.psi.NavigatablePsiElement)

Example 18 with PyFunction

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

the class PythonDocumentationMap method transformPattern.

@Nullable
private static String transformPattern(@NotNull String urlPattern, QualifiedName moduleQName, @Nullable PsiNamedElement element, String pyVersion) {
    Map<String, String> macros = new HashMap<>();
    macros.put("element.name", element == null ? null : element.getName());
    PyClass pyClass = element == null ? null : PsiTreeUtil.getParentOfType(element, PyClass.class, false);
    macros.put("class.name", pyClass == null ? null : pyClass.getName());
    if (element != null) {
        StringBuilder qName = new StringBuilder(moduleQName.toString()).append(".");
        if (element instanceof PyFunction && ((PyFunction) element).getContainingClass() != null) {
            qName.append(((PyFunction) element).getContainingClass().getName()).append(".");
        }
        qName.append(element.getName());
        macros.put("element.qname", qName.toString());
    } else {
        macros.put("element.qname", "");
    }
    macros.put("function.name", element instanceof PyFunction ? element.getName() : "");
    macros.put("module.name", moduleQName.toString());
    macros.put("python.version", pyVersion);
    final String pattern = transformPattern(urlPattern, macros);
    if (pattern == null) {
        return rootForPattern(urlPattern);
    }
    return pattern;
}
Also used : PyClass(com.jetbrains.python.psi.PyClass) PyFunction(com.jetbrains.python.psi.PyFunction) HashMap(java.util.HashMap) Nullable(org.jetbrains.annotations.Nullable)

Example 19 with PyFunction

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

the class PyQualifiedNameProvider method qualifiedNameToElement.

@Nullable
@Override
public PsiElement qualifiedNameToElement(String fqn, Project project) {
    PyClass aClass = PyClassNameIndex.findClass(fqn, project);
    if (aClass != null) {
        return aClass;
    }
    final Collection<PyFunction> functions = PyFunctionNameIndex.find(fqn, project);
    if (!functions.isEmpty()) {
        return ContainerUtil.getFirstItem(functions);
    }
    final int sharpIdx = fqn.indexOf("#");
    if (sharpIdx > -1) {
        final String className = StringUtil.getPackageName(fqn, '#');
        aClass = PyClassNameIndex.findClass(className, project);
        if (aClass != null) {
            final String memberName = StringUtil.getShortName(fqn, '#');
            final PyClass nestedClass = aClass.findNestedClass(memberName, false);
            if (nestedClass != null)
                return nestedClass;
            final PyFunction methodByName = aClass.findMethodByName(memberName, false, null);
            if (methodByName != null)
                return methodByName;
        }
    }
    return null;
}
Also used : PyClass(com.jetbrains.python.psi.PyClass) PyFunction(com.jetbrains.python.psi.PyFunction) Nullable(org.jetbrains.annotations.Nullable)

Example 20 with PyFunction

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

the class PyParameterListFixer method doApply.

@Override
public void doApply(@NotNull Editor editor, @NotNull PySmartEnterProcessor processor, @NotNull PyParameterList parameters) throws IncorrectOperationException {
    final PsiElement lBrace = PyPsiUtils.getChildByFilter(parameters, PyTokenTypes.OPEN_BRACES, 0);
    final PsiElement rBrace = PyPsiUtils.getChildByFilter(parameters, PyTokenTypes.CLOSE_BRACES, 0);
    final PyFunction pyFunction = as(parameters.getParent(), PyFunction.class);
    if (pyFunction != null && !PyFunctionFixer.isFakeFunction(pyFunction) && (lBrace == null || rBrace == null)) {
        final Document document = editor.getDocument();
        if (lBrace == null) {
            final String textToInsert = pyFunction.getNameNode() == null ? " (" : "(";
            document.insertString(parameters.getTextOffset(), textToInsert);
        } else if (parameters.getParameters().length == 0) {
            document.insertString(lBrace.getTextRange().getEndOffset(), ")");
        } else {
            document.insertString(parameters.getTextRange().getEndOffset(), ")");
        }
    }
}
Also used : PyFunction(com.jetbrains.python.psi.PyFunction) Document(com.intellij.openapi.editor.Document) 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