Search in sources :

Example 41 with PyFunction

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

the class PyImplementMethodsQuickFix method invoke.

@Override
public void invoke(@NotNull Project project, @NotNull PsiFile file, @NotNull PsiElement startElement, @NotNull PsiElement endElement) {
    final Editor editor = PyQuickFixUtil.getEditor(file);
    if (editor != null && startElement instanceof PyClass) {
        if (ApplicationManager.getApplication().isUnitTestMode()) {
            ArrayList<PyMethodMember> list = new ArrayList<>();
            for (PyFunction function : myToImplement) {
                list.add(new PyMethodMember(function));
            }
            PyOverrideImplementUtil.overrideMethods(editor, (PyClass) startElement, list, true);
        } else {
            PyOverrideImplementUtil.chooseAndOverrideOrImplementMethods(project, editor, (PyClass) startElement, myToImplement, "Select Methods to Implement", true);
        }
    }
}
Also used : PyClass(com.jetbrains.python.psi.PyClass) PyFunction(com.jetbrains.python.psi.PyFunction) PyMethodMember(com.jetbrains.python.codeInsight.override.PyMethodMember) ArrayList(java.util.ArrayList) Editor(com.intellij.openapi.editor.Editor)

Example 42 with PyFunction

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

the class PyiRelatedItemLineMarkerProvider method collectNavigationMarkers.

@Override
protected void collectNavigationMarkers(@NotNull PsiElement element, Collection<? super RelatedItemLineMarkerInfo> result) {
    if (element instanceof PyFunction || element instanceof PyTargetExpression || element instanceof PyClass) {
        final PsiElement pythonStub = PyiUtil.getPythonStub((PyElement) element);
        if (pythonStub != null) {
            result.add(createLineMarkerInfo(element, pythonStub, "Has stub item"));
        }
        final PsiElement originalElement = PyiUtil.getOriginalElement((PyElement) element);
        if (originalElement != null) {
            result.add(createLineMarkerInfo(element, originalElement, "Stub for item"));
        }
    }
}
Also used : PyClass(com.jetbrains.python.psi.PyClass) PyTargetExpression(com.jetbrains.python.psi.PyTargetExpression) PyFunction(com.jetbrains.python.psi.PyFunction) PsiElement(com.intellij.psi.PsiElement)

Example 43 with PyFunction

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

the class PyChangeSignatureTest method testAnnotationsForStarredParametersAreNotShownInDialog.

// PY-14774
public void testAnnotationsForStarredParametersAreNotShownInDialog() throws Exception {
    runWithLanguageLevel(LanguageLevel.PYTHON30, () -> {
        myFixture.configureByText(PythonFileType.INSTANCE, "def func(a, b:int, *args: tuple, c:list, d:str='foo', ** kwargs:dict):\n" + "    pass");
        final PyFunction function = (PyFunction) new PyChangeSignatureHandler().findTargetMember(myFixture.getFile(), myFixture.getEditor());
        assertNotNull(function);
        final List<String> expected = Arrays.asList("a", "b", "*args", "c", "d", "**kwargs");
        final List<PyParameterInfo> parameters = new PyMethodDescriptor(function).getParameters();
        assertEquals(expected, ContainerUtil.map(parameters, info -> info.getOldName()));
    });
}
Also used : Arrays(java.util.Arrays) PyBundle(com.jetbrains.python.PyBundle) BaseRefactoringProcessor(com.intellij.refactoring.BaseRefactoringProcessor) ContainerUtil(com.intellij.util.containers.ContainerUtil) LanguageLevel(com.jetbrains.python.psi.LanguageLevel) TestDataPath(com.intellij.testFramework.TestDataPath) PyTestCase(com.jetbrains.python.fixtures.PyTestCase) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) DocStringFormat(com.jetbrains.python.documentation.docstrings.DocStringFormat) Function(com.intellij.util.Function) Disposer(com.intellij.openapi.util.Disposer) Project(com.intellij.openapi.project.Project) PythonFileType(com.jetbrains.python.PythonFileType) PyFunction(com.jetbrains.python.psi.PyFunction) PyFunction(com.jetbrains.python.psi.PyFunction)

Example 44 with PyFunction

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

the class PyStdlibDocumentationLinkProvider method getStdlibUrlFor.

private String getStdlibUrlFor(PsiElement element, QualifiedName moduleName, Sdk sdk) {
    StringBuilder urlBuilder = new StringBuilder(getExternalDocumentationRoot(sdk));
    String qnameString = moduleName.toString();
    if (qnameString.equals("ntpath") || qnameString.equals("posixpath")) {
        qnameString = "os.path";
    } else if (qnameString.equals("nt")) {
        qnameString = "os";
    } else if (qnameString.equals("cPickle")) {
        qnameString = "pickle";
    } else if (qnameString.equals("pyexpat")) {
        qnameString = "xml.parsers.expat";
    }
    final String pyVersion = PythonDocumentationProvider.pyVersion(sdk.getVersionString());
    List<String> modules = pyVersion != null && pyVersion.startsWith("3") ? py3LibraryModules : py2LibraryModules;
    boolean foundModule = false;
    for (String module : modules) {
        if (qnameString.startsWith(module)) {
            urlBuilder.append(module.toLowerCase());
            urlBuilder.append(".html");
            foundModule = true;
            break;
        }
    }
    if (foundModule && element instanceof PsiNamedElement && !(element instanceof PyFile)) {
        urlBuilder.append('#').append(moduleName).append(".");
        if (element instanceof PyFunction) {
            final PyClass containingClass = ((PyFunction) element).getContainingClass();
            if (containingClass != null) {
                urlBuilder.append(containingClass.getName()).append('.');
            }
        }
        urlBuilder.append(((PsiNamedElement) element).getName());
    }
    return urlBuilder.toString();
}
Also used : PyClass(com.jetbrains.python.psi.PyClass) PyFunction(com.jetbrains.python.psi.PyFunction) PsiNamedElement(com.intellij.psi.PsiNamedElement) PyFile(com.jetbrains.python.psi.PyFile)

Example 45 with PyFunction

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

the class PyOverrideTest method testQualified.

public void testQualified() {
    // PY-2171
    myFixture.configureByFile("override/" + getTestName(true) + ".py");
    PyClass dateClass = PyClassNameIndex.findClass("datetime.date", myFixture.getProject());
    assertNotNull(dateClass);
    PyFunction initMethod = dateClass.findMethodByName(PyNames.INIT, false, null);
    assertNotNull(initMethod);
    PyOverrideImplementUtil.overrideMethods(myFixture.getEditor(), getTopLevelClass(0), Collections.singletonList(new PyMethodMember(initMethod)), false);
    myFixture.checkResultByFile("override/" + getTestName(true) + "_after.py", true);
}
Also used : PyClass(com.jetbrains.python.psi.PyClass) PyFunction(com.jetbrains.python.psi.PyFunction) PyMethodMember(com.jetbrains.python.codeInsight.override.PyMethodMember)

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