Search in sources :

Example 56 with PyFunction

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

the class PyTestFinder method findTestsForClass.

@NotNull
@Override
public Collection<PsiElement> findTestsForClass(@NotNull PsiElement element) {
    PyDocStringOwner source = findSourceElement(element);
    if (source == null)
        return Collections.emptySet();
    String sourceName = source.getName();
    if (sourceName == null)
        return Collections.emptySet();
    List<Pair<? extends PsiNamedElement, Integer>> classesWithProximities = new ArrayList<>();
    if (source instanceof PyClass) {
        Collection<String> names = PyClassNameIndex.allKeys(element.getProject());
        for (String eachName : names) {
            if (eachName.contains(sourceName)) {
                for (PyClass eachClass : PyClassNameIndex.find(eachName, element.getProject(), GlobalSearchScope.projectScope(element.getProject()))) {
                    if (PythonUnitTestUtil.isTestCaseClass(eachClass, null) || PythonDocTestUtil.isDocTestClass(eachClass)) {
                        classesWithProximities.add(new Pair<PsiNamedElement, Integer>(eachClass, TestFinderHelper.calcTestNameProximity(sourceName, eachName)));
                    }
                }
            }
        }
    } else {
        Collection<String> names = PyFunctionNameIndex.allKeys(element.getProject());
        for (String eachName : names) {
            if (eachName.contains(sourceName)) {
                for (PyFunction eachFunction : PyFunctionNameIndex.find(eachName, element.getProject(), GlobalSearchScope.projectScope(element.getProject()))) {
                    if (PythonUnitTestUtil.isTestCaseFunction(eachFunction) || PythonDocTestUtil.isDocTestFunction(eachFunction)) {
                        classesWithProximities.add(new Pair<PsiNamedElement, Integer>(eachFunction, TestFinderHelper.calcTestNameProximity(sourceName, eachName)));
                    }
                }
            }
        }
    }
    return TestFinderHelper.getSortedElements(classesWithProximities, true);
}
Also used : PyClass(com.jetbrains.python.psi.PyClass) PyFunction(com.jetbrains.python.psi.PyFunction) PsiNamedElement(com.intellij.psi.PsiNamedElement) ArrayList(java.util.ArrayList) PyDocStringOwner(com.jetbrains.python.psi.PyDocStringOwner) Pair(com.intellij.openapi.util.Pair) NotNull(org.jetbrains.annotations.NotNull)

Example 57 with PyFunction

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

the class PyTestFinder method findClassesForTest.

@NotNull
@Override
public Collection<PsiElement> findClassesForTest(@NotNull PsiElement element) {
    final PyFunction sourceFunction = PsiTreeUtil.getParentOfType(element, PyFunction.class);
    final PyClass source = PsiTreeUtil.getParentOfType(element, PyClass.class);
    if (sourceFunction == null && source == null)
        return Collections.emptySet();
    List<Pair<? extends PsiNamedElement, Integer>> classesWithWeights = new ArrayList<>();
    final List<Pair<String, Integer>> possibleNames = new ArrayList<>();
    if (source != null)
        possibleNames.addAll(TestFinderHelper.collectPossibleClassNamesWithWeights(source.getName()));
    if (sourceFunction != null)
        possibleNames.addAll(TestFinderHelper.collectPossibleClassNamesWithWeights(sourceFunction.getName()));
    for (Pair<String, Integer> eachNameWithWeight : possibleNames) {
        for (PyClass eachClass : PyClassNameIndex.find(eachNameWithWeight.first, element.getProject(), GlobalSearchScope.projectScope(element.getProject()))) {
            if (!PyTestUtil.isPyTestClass(eachClass, null))
                classesWithWeights.add(new Pair<PsiNamedElement, Integer>(eachClass, eachNameWithWeight.second));
        }
        for (PyFunction function : PyFunctionNameIndex.find(eachNameWithWeight.first, element.getProject(), GlobalSearchScope.projectScope(element.getProject()))) {
            if (!PyTestUtil.isPyTestFunction(function))
                classesWithWeights.add(new Pair<PsiNamedElement, Integer>(function, eachNameWithWeight.second));
        }
    }
    return TestFinderHelper.getSortedElements(classesWithWeights, false);
}
Also used : PyClass(com.jetbrains.python.psi.PyClass) PyFunction(com.jetbrains.python.psi.PyFunction) PsiNamedElement(com.intellij.psi.PsiNamedElement) ArrayList(java.util.ArrayList) Pair(com.intellij.openapi.util.Pair) NotNull(org.jetbrains.annotations.NotNull)

Example 58 with PyFunction

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

the class PyDecoratorTest method testDecoCall.

public void testDecoCall() throws Exception {
    PsiElement targetElement = find().getParent();
    assertTrue(targetElement instanceof PyDecorator);
    PyDecorator deco = (PyDecorator) targetElement;
    PyFunction decofun = deco.getTarget();
    assertNotNull(decofun);
    assertEquals("foo", decofun.getName());
    assertFalse(deco.isBuiltin());
    assertFalse(deco.hasArgumentList());
}
Also used : PyFunction(com.jetbrains.python.psi.PyFunction) PyDecorator(com.jetbrains.python.psi.PyDecorator) PsiElement(com.intellij.psi.PsiElement)

Example 59 with PyFunction

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

the class PyDecoratorTest method testDecoParamCall.

public void testDecoParamCall() throws Exception {
    PsiElement targetElement = find().getParent();
    assertTrue(targetElement instanceof PyDecorator);
    PyDecorator deco = (PyDecorator) targetElement;
    PyFunction decofun = deco.getTarget();
    assertNotNull(decofun);
    assertEquals("foo", decofun.getName());
    assertFalse(deco.isBuiltin());
    assertTrue(deco.hasArgumentList());
    PyArgumentList arglist = deco.getArgumentList();
    assertNotNull(arglist);
    PyExpression[] args = arglist.getArguments();
    assertEquals("argument count", 1, args.length);
    assertEquals("argument value", "1", args[0].getText());
}
Also used : PyArgumentList(com.jetbrains.python.psi.PyArgumentList) PyFunction(com.jetbrains.python.psi.PyFunction) PyExpression(com.jetbrains.python.psi.PyExpression) PyDecorator(com.jetbrains.python.psi.PyDecorator) PsiElement(com.intellij.psi.PsiElement)

Example 60 with PyFunction

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

the class PyStatementListTest method testOneLineList.

public void testOneLineList() {
    PyElementGenerator generator = PyElementGenerator.getInstance(myFixture.getProject());
    PyFunction function = generator.createPhysicalFromText(LanguageLevel.PYTHON27, PyFunction.class, "def foo(): print 1");
    PyFunction function2 = generator.createPhysicalFromText(LanguageLevel.PYTHON27, PyFunction.class, "def foo(): print 2");
    final PyStatementList list1 = function.getStatementList();
    final PyStatementList list2 = function2.getStatementList();
    new WriteCommandAction.Simple(myFixture.getProject()) {

        @Override
        protected void run() throws Throwable {
            list1.add(list2.getStatements()[0]);
        }
    }.execute();
    assertEquals("def foo():\n    print 1\n    print 2", function.getText());
}
Also used : WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) PyFunction(com.jetbrains.python.psi.PyFunction) PyElementGenerator(com.jetbrains.python.psi.PyElementGenerator) PyStatementList(com.jetbrains.python.psi.PyStatementList)

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