Search in sources :

Example 71 with PyClass

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

the class PyClassRefactoringTest method findClass.

protected PyClass findClass(final String name) {
    final Project project = myFixture.getProject();
    final Collection<PyClass> classes = PyClassNameIndex.find(name, project, false);
    Assert.assertThat(String.format("Expected one class named %s", name), classes, Matchers.hasSize(1));
    return classes.iterator().next();
}
Also used : PyClass(com.jetbrains.python.psi.PyClass) Project(com.intellij.openapi.project.Project)

Example 72 with PyClass

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

the class PyClassNameMacro method calculateResult.

@Nullable
@Override
public Result calculateResult(@NotNull Expression[] params, ExpressionContext context) {
    PsiElement place = context.getPsiElementAtStartOffset();
    PyClass pyClass = PsiTreeUtil.getParentOfType(place, PyClass.class);
    if (pyClass == null) {
        return null;
    }
    String name = pyClass.getName();
    return name == null ? null : new TextResult(name);
}
Also used : PyClass(com.jetbrains.python.psi.PyClass) PsiElement(com.intellij.psi.PsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 73 with PyClass

use of com.jetbrains.python.psi.PyClass 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 74 with PyClass

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

the class PyClassTreeChooserDialog method getClassesByName.

@NotNull
@Override
protected List<PyClass> getClassesByName(String name, boolean checkBoxState, String pattern, GlobalSearchScope searchScope) {
    final Collection<PyClass> classes = PyClassNameIndex.find(name, getProject(), searchScope.isSearchInLibraries());
    final List<PyClass> result = Lists.newArrayList();
    for (PyClass c : classes) {
        if (getFilter().isAccepted(c)) {
            result.add(c);
        }
    }
    return result;
}
Also used : PyClass(com.jetbrains.python.psi.PyClass) NotNull(org.jetbrains.annotations.NotNull)

Example 75 with PyClass

use of com.jetbrains.python.psi.PyClass 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)

Aggregations

PyClass (com.jetbrains.python.psi.PyClass)88 PyFunction (com.jetbrains.python.psi.PyFunction)32 PsiElement (com.intellij.psi.PsiElement)22 NotNull (org.jetbrains.annotations.NotNull)17 ArrayList (java.util.ArrayList)15 Nullable (org.jetbrains.annotations.Nullable)15 PyElement (com.jetbrains.python.psi.PyElement)12 PyMemberInfo (com.jetbrains.python.refactoring.classes.membersManager.PyMemberInfo)9 PyFile (com.jetbrains.python.psi.PyFile)8 PsiFile (com.intellij.psi.PsiFile)7 Project (com.intellij.openapi.project.Project)6 Editor (com.intellij.openapi.editor.Editor)5 VirtualFile (com.intellij.openapi.vfs.VirtualFile)5 MultiMap (com.intellij.util.containers.MultiMap)5 PyMethodMember (com.jetbrains.python.codeInsight.override.PyMethodMember)4 PyMemberInfoStorage (com.jetbrains.python.refactoring.classes.PyMemberInfoStorage)4 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)3 PsiNamedElement (com.intellij.psi.PsiNamedElement)3 PyHierarchyNodeDescriptor (com.jetbrains.python.hierarchy.PyHierarchyNodeDescriptor)3 ControlFlow (com.intellij.codeInsight.controlflow.ControlFlow)2